Class: TestaLogger::Logger
- Inherits:
-
Object
- Object
- TestaLogger::Logger
- Includes:
- Persistence
- Defined in:
- lib/testa_logger/logger.rb,
lib/testa_logger/logger/options.rb,
lib/testa_logger/logger/dispatcher.rb,
lib/testa_logger/logger/persistence.rb
Overview
noinspection RubyTooManyMethodsInspection
Defined Under Namespace
Modules: Persistence Classes: Dispatcher, Options
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- NO_TAG =
"NO-TAG"
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #create_log_file ⇒ Object
- #debug(tag = NO_TAG, *args, &block) ⇒ Object
-
#debug! ⇒ Object
Sets the severity to DEBUG.
-
#debug? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofDEBUGmessages. - #error(tag = NO_TAG, *args, &block) ⇒ Object
-
#error! ⇒ Object
Sets the severity to ERROR.
-
#error? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofERRORmessages. - #fatal(tag = NO_TAG, *args, &block) ⇒ Object
-
#fatal! ⇒ Object
Sets the severity to FATAL.
-
#fatal? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofFATALmessages. - #info(tag = NO_TAG, *args, &block) ⇒ Object
-
#info! ⇒ Object
Sets the severity to INFO.
-
#info? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofINFOmessages. -
#initialize(app, group, subgroup = nil, options = {}) ⇒ Logger
constructor
A new instance of Logger.
- #level ⇒ Object
- #level=(severity) ⇒ Object
- #setup_dispatcher ⇒ Object
- #silence(severity = ERROR, &block) ⇒ Object
- #start_write_thread ⇒ Object
- #unknown(tag = NO_TAG, *args, &block) ⇒ Object
- #warn(tag = NO_TAG, *args, &block) ⇒ Object
-
#warn! ⇒ Object
Sets the severity to WARN.
-
#warn? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofWARNmessages.
Methods included from Persistence
#init_s3_client, #persist, #persist_with_record
Constructor Details
#initialize(app, group, subgroup = nil, options = {}) ⇒ Logger
Returns a new instance of Logger.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/testa_logger/logger.rb', line 36 def initialize(app, group, subgroup = nil, = {}) @app = app @group = group @subgroup = subgroup = Options.new(app, group, subgroup, ) create_log_file setup_dispatcher start_write_thread init_s3_client if .persist end |
Instance Attribute Details
#app ⇒ String
13 14 15 |
# File 'lib/testa_logger/logger.rb', line 13 def app @app end |
#group ⇒ String
13 14 15 |
# File 'lib/testa_logger/logger.rb', line 13 def group @group end |
#subgroup ⇒ String
13 14 15 |
# File 'lib/testa_logger/logger.rb', line 13 def subgroup @subgroup end |
#write_thread ⇒ Thread
19 20 21 |
# File 'lib/testa_logger/logger.rb', line 19 def write_thread @write_thread end |
Class Method Details
.default_options ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/testa_logger/logger.rb', line 181 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
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/testa_logger/logger.rb', line 47 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 = NO_TAG, *args, &block) ⇒ Object
81 82 83 |
# File 'lib/testa_logger/logger.rb', line 81 def debug(tag = NO_TAG, *args, &block) add_log_to_queue(DEBUG, tag, args, &block) end |
#debug! ⇒ Object
Sets the severity to DEBUG.
120 121 122 |
# File 'lib/testa_logger/logger.rb', line 120 def debug! .level = DEBUG end |
#debug? ⇒ Boolean
Returns true iff the current severity level allows for the printing of DEBUG messages.
115 116 117 |
# File 'lib/testa_logger/logger.rb', line 115 def debug? .level <= DEBUG end |
#error(tag = NO_TAG, *args, &block) ⇒ Object
93 94 95 |
# File 'lib/testa_logger/logger.rb', line 93 def error(tag = NO_TAG, *args, &block) add_log_to_queue(ERROR, tag, args, &block) end |
#error! ⇒ Object
Sets the severity to ERROR.
153 154 155 |
# File 'lib/testa_logger/logger.rb', line 153 def error! .level = ERROR end |
#error? ⇒ Boolean
Returns true iff the current severity level allows for the printing of ERROR messages.
148 149 150 |
# File 'lib/testa_logger/logger.rb', line 148 def error? .level <= ERROR end |
#fatal(tag = NO_TAG, *args, &block) ⇒ Object
97 98 99 |
# File 'lib/testa_logger/logger.rb', line 97 def fatal(tag = NO_TAG, *args, &block) add_log_to_queue(FATAL, tag, args, &block) end |
#fatal! ⇒ Object
Sets the severity to FATAL.
164 165 166 |
# File 'lib/testa_logger/logger.rb', line 164 def fatal! .level = FATAL end |
#fatal? ⇒ Boolean
Returns true iff the current severity level allows for the printing of FATAL messages.
159 160 161 |
# File 'lib/testa_logger/logger.rb', line 159 def fatal? .level <= FATAL end |
#info(tag = NO_TAG, *args, &block) ⇒ Object
85 86 87 |
# File 'lib/testa_logger/logger.rb', line 85 def info(tag = NO_TAG, *args, &block) add_log_to_queue(INFO, tag, args, &block) end |
#info! ⇒ Object
Sets the severity to INFO.
131 132 133 |
# File 'lib/testa_logger/logger.rb', line 131 def info! .level = INFO end |
#info? ⇒ Boolean
Returns true iff the current severity level allows for the printing of INFO messages.
126 127 128 |
# File 'lib/testa_logger/logger.rb', line 126 def info? .level <= INFO end |
#level ⇒ Object
168 169 170 |
# File 'lib/testa_logger/logger.rb', line 168 def level .level end |
#level=(severity) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/testa_logger/logger.rb', line 105 def level=(severity) if severity.is_a?(Integer) .level = severity else SEV_LABEL.index(severity.to_s.downcase.upcase).to_i end end |
#setup_dispatcher ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/testa_logger/logger.rb', line 59 def setup_dispatcher @dispatcher = Dispatcher.new( .faye_url, .faye_token, app, group, subgroup ) end |
#silence(severity = ERROR, &block) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/testa_logger/logger.rb', line 172 def silence(severity = ERROR, &block) old_level = .level .level = severity result = block&.call .level = old_level result end |
#start_write_thread ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/testa_logger/logger.rb', line 69 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 = NO_TAG, *args, &block) ⇒ Object
101 102 103 |
# File 'lib/testa_logger/logger.rb', line 101 def unknown(tag = NO_TAG, *args, &block) add_log_to_queue(UNKNOWN, tag, args, &block) end |
#warn(tag = NO_TAG, *args, &block) ⇒ Object
89 90 91 |
# File 'lib/testa_logger/logger.rb', line 89 def warn(tag = NO_TAG, *args, &block) add_log_to_queue(WARN, tag, args, &block) end |
#warn! ⇒ Object
Sets the severity to WARN.
142 143 144 |
# File 'lib/testa_logger/logger.rb', line 142 def warn! .level = WARN end |
#warn? ⇒ Boolean
Returns true iff the current severity level allows for the printing of WARN messages.
137 138 139 |
# File 'lib/testa_logger/logger.rb', line 137 def warn? .level <= WARN end |