Class: TestaLogger::Logger
- Inherits:
-
Object
- Object
- TestaLogger::Logger
- Defined in:
- lib/testa_logger/logger.rb,
lib/testa_logger/logger/dispatcher.rb,
lib/testa_logger/logger/persistence.rb
Defined Under Namespace
Modules: Persistence Classes: Dispatcher
Constant Summary collapse
- TAG =
"TestaLogger"- DEBUG =
Low-level information, mostly for developers.
0- INFO =
Generic (useful) information about system operation.
1- WARN =
A warning.
2- ERROR =
A handleable error condition.
3- FATAL =
An unhandleable error that results in a program crash.
4- UNKNOWN =
An unknown message that should always be logged.
5
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #create_log_file ⇒ Object
- #debug(tag, *args, &block) ⇒ Object
- #error(tag, *args, &block) ⇒ Object
- #fatal(tag, *args, &block) ⇒ Object
- #handle_options(options) ⇒ Object
- #info(tag, *args, &block) ⇒ Object
-
#initialize(app, group, subgroup = nil, options = {}) ⇒ Logger
constructor
A new instance of Logger.
- #setup_dispatcher ⇒ Object
- #start_write_thread ⇒ Object
- #unknown(tag, *args, &block) ⇒ Object
- #warn(tag, *args, &block) ⇒ Object
Constructor Details
#initialize(app, group, subgroup = nil, options = {}) ⇒ Logger
Returns a new instance of Logger.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/testa_logger/logger.rb', line 30 def initialize(app, group, subgroup = nil, = {}) @app = app @group = group @subgroup = subgroup () create_log_file setup_dispatcher start_write_thread self.extend Persistence if .persist end |
Instance Attribute Details
#app ⇒ String
9 10 11 |
# File 'lib/testa_logger/logger.rb', line 9 def app @app end |
#group ⇒ String
9 10 11 |
# File 'lib/testa_logger/logger.rb', line 9 def group @group end |
#options ⇒ OpenStruct
12 13 14 |
# File 'lib/testa_logger/logger.rb', line 12 def end |
#subgroup ⇒ String
9 10 11 |
# File 'lib/testa_logger/logger.rb', line 9 def subgroup @subgroup end |
#write_thread ⇒ Thread
15 16 17 |
# File 'lib/testa_logger/logger.rb', line 15 def write_thread @write_thread end |
Class Method Details
.default_formatter ⇒ Object
138 139 140 141 142 |
# File 'lib/testa_logger/logger.rb', line 138 def default_formatter proc do |severity, datetime, _, msg| "[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}] #{format('%-5.5s', severity)} -- : #{msg}\n" end end |
.default_options ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/testa_logger/logger.rb', line 118 def OpenStruct.new( shift_age: "daily", level: DEBUG, formatter: default_formatter, live: false, filepath: nil, tag_length: 13, persist: true, # requires aws credentials set in env variables faye_url: ENV["WEB_SOCKET_URL"], faye_token: ENV["FAYE_TOKEN"], s3_creds: { region: ENV["AWS_REGION"], access_key_id: ENV["AWS_ACCESS_KEY_ID"], secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"], bucket_name: ENV["S3_BUCKET_NAME"], } ) end |
Instance Method Details
#create_log_file ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/testa_logger/logger.rb', line 59 def create_log_file .filepath = "/tmp/#{SecureRandom.uuid}.log" if .filepath.nil? FileUtils.mkdir_p(File.dirname(.filepath)) before_shift = .persist ? proc { persist } : nil @log_device = LogDevice.new(.filepath, shift_age: .shift_age, shift_size: 1_048_576, shift_period_suffix: "%d%m%Y", binmode: false, before_shift: before_shift) end |
#debug(tag, *args, &block) ⇒ Object
93 94 95 |
# File 'lib/testa_logger/logger.rb', line 93 def debug(tag, *args, &block) add_log_to_queue(DEBUG, tag, args, &block) end |
#error(tag, *args, &block) ⇒ Object
105 106 107 |
# File 'lib/testa_logger/logger.rb', line 105 def error(tag, *args, &block) add_log_to_queue(ERROR, tag, args, &block) end |
#fatal(tag, *args, &block) ⇒ Object
109 110 111 |
# File 'lib/testa_logger/logger.rb', line 109 def fatal(tag, *args, &block) add_log_to_queue(FATAL, tag, args, &block) end |
#handle_options(options) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/testa_logger/logger.rb', line 41 def () .deep_symbolize_keys! = Logger. .shift_age = [:shift_age] unless [:shift_age].nil? unless [:level].nil? [:level] = SEV_LABEL.index([:level].to_s.upcase).to_i unless [:level].is_a?(Integer) .level = [:level] end .formatter = [:formatter] unless [:formatter].nil? .live = [:live] unless [:live].nil? .filepath = File.([:filepath]) unless [:filepath].nil? .tag_length = [:tag_length] unless [:tag_length].nil? .persist = [:persist] unless [:persist].nil? .faye_url = [:faye_url] unless [:faye_url].nil? .faye_token = [:faye_token] unless [:faye_token].nil? .s3_creds = [:s3_creds].deep_symbolize_keys unless [:s3_creds].blank? end |
#info(tag, *args, &block) ⇒ Object
97 98 99 |
# File 'lib/testa_logger/logger.rb', line 97 def info(tag, *args, &block) add_log_to_queue(INFO, tag, args, &block) end |
#setup_dispatcher ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/testa_logger/logger.rb', line 71 def setup_dispatcher @dispatcher = Dispatcher.new( .faye_url, .faye_token, app, group, subgroup ) end |
#start_write_thread ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/testa_logger/logger.rb', line 81 def start_write_thread # we must use this queue in order to be able to collect logs in trap context @queue = Queue.new @write_thread = Thread.new do loop do text = @queue.pop Thread.stop if Thread.current["stop"] @log_device.write(text) end end end |
#unknown(tag, *args, &block) ⇒ Object
113 114 115 |
# File 'lib/testa_logger/logger.rb', line 113 def unknown(tag, *args, &block) add_log_to_queue(UNKNOWN, tag, args, &block) end |
#warn(tag, *args, &block) ⇒ Object
101 102 103 |
# File 'lib/testa_logger/logger.rb', line 101 def warn(tag, *args, &block) add_log_to_queue(WARN, tag, args, &block) end |