Class: Appstats::Logger
- Inherits:
-
Object
- Object
- Appstats::Logger
- Defined in:
- lib/appstats/logger.rb
Class Method Summary collapse
- .default_contexts ⇒ Object
- .entry(action, contexts = {}) ⇒ Object
- .entry_to_hash(action_and_contexts) ⇒ Object
- .entry_to_s(action, contexts = {}) ⇒ Object
- .exception_entry(error, contexts = {}) ⇒ Object
- .filename ⇒ Object
- .filename_template ⇒ Object
- .filename_template=(value) ⇒ Object
- .now ⇒ Object
- .raw_read ⇒ Object
- .raw_write(text) ⇒ Object
- .reset ⇒ Object
- .today ⇒ Object
Class Method Details
.default_contexts ⇒ Object
10 11 12 13 |
# File 'lib/appstats/logger.rb', line 10 def self.default_contexts @@default_contexts ||= {} @@default_contexts end |
.entry(action, contexts = {}) ⇒ Object
81 82 83 |
# File 'lib/appstats/logger.rb', line 81 def self.entry(action,contexts = {}) raw_write(entry_to_s(action,contexts)) end |
.entry_to_hash(action_and_contexts) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/appstats/logger.rb', line 54 def self.entry_to_hash(action_and_contexts) hash = { :action => "UNKNOWN_ACTION", :raw_input => action_and_contexts } return hash if action_and_contexts.nil? setup = action_and_contexts.match(/(.*?) setup\[(.*?),(.*?),(.*?)\] (.*? .*?) (.*)/) return hash if setup.nil? hash.delete(:action) hash.delete(:raw_input) full, version, section_delimiter, assign_delimiter, newline_delimiter, , input = setup.to_a hash[:timestamp] = input.split(section_delimiter).each do |pair| key,value = pair.strip.split(assign_delimiter) key_symbol = key.to_sym if hash[key_symbol].nil? hash[key.to_sym] = value elsif hash[key_symbol].kind_of?(String) hash[key.to_sym] = [ hash[key_symbol], value ] else all_values = hash[key_symbol] all_values<< value hash[key.to_sym] = all_values end end hash[:action] = "UNKNOWN_ACTION" if hash[:action].nil? hash end |
.entry_to_s(action, contexts = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/appstats/logger.rb', line 40 def self.entry_to_s(action,contexts = {}) contexts = contexts.merge(default_contexts) section_delimiter, assign_delimiter, newline_delimiter = determine_delimiters(contexts.merge(:action => action)) answer = "#{Appstats::VERSION} setup[#{section_delimiter},#{assign_delimiter},#{newline_delimiter}] " answer += "#{now} action#{assign_delimiter}#{format_input(action,newline_delimiter)}" contexts.keys.sort.each do |key| all_val = contexts[key].kind_of?(Array) ? contexts[key] : [contexts[key]] all_val.each do |value| answer += " #{section_delimiter} #{key}#{assign_delimiter}#{format_input(value,newline_delimiter)}" end end answer end |
.exception_entry(error, contexts = {}) ⇒ Object
85 86 87 |
# File 'lib/appstats/logger.rb', line 85 def self.exception_entry(error,contexts = {}) raw_write(entry_to_s("appstats-exception",contexts.merge({:error => error.}))) end |
.filename ⇒ Object
27 28 29 |
# File 'lib/appstats/logger.rb', line 27 def self.filename "#{filename_template}_#{today}.log" end |
.filename_template ⇒ Object
22 23 24 25 |
# File 'lib/appstats/logger.rb', line 22 def self.filename_template @@filename_template ||= 'appstats' @@filename_template end |
.filename_template=(value) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/appstats/logger.rb', line 15 def self.filename_template=(value) @@filename_template = value dir = File.dirname(@@filename_template) FileUtils.mkdir_p(dir) unless File.exists?(dir) @@filename_template end |
.now ⇒ Object
93 94 95 |
# File 'lib/appstats/logger.rb', line 93 def self.now "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}" end |
.raw_read ⇒ Object
35 36 37 38 |
# File 'lib/appstats/logger.rb', line 35 def self.raw_read return [] unless File.exists?(filename) File.open(filename,"r").readlines.collect { |line| line.strip } end |
.raw_write(text) ⇒ Object
31 32 33 |
# File 'lib/appstats/logger.rb', line 31 def self.raw_write(text) File.open(filename, "a") { |f| f.write("#{text}\n") } end |
.reset ⇒ Object
5 6 7 8 |
# File 'lib/appstats/logger.rb', line 5 def self.reset @@filename_template = nil @@default_contexts = nil end |
.today ⇒ Object
89 90 91 |
# File 'lib/appstats/logger.rb', line 89 def self.today "#{Time.now.strftime('%Y-%m-%d')}" end |