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 = "", *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 = "", *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 = "", *args, &block) ⇒ Object
-
#fatal! ⇒ Object
Sets the severity to FATAL.
-
#fatal? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofFATALmessages. - #formatter ⇒ Object
- #info(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 = "", *args, &block) ⇒ Object
- #warn(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 = Options.new(app, group, subgroup, ) create_log_file setup_dispatcher start_write_thread init_s3_client if @options.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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/testa_logger/logger.rb', line 183 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 = "", *args, &block) ⇒ Object
83 84 85 |
# File 'lib/testa_logger/logger.rb', line 83 def debug(tag = "", *args, &block) add_log_to_queue(DEBUG, tag, args, &block) end |
#debug! ⇒ Object
Sets the severity to DEBUG.
122 123 124 |
# File 'lib/testa_logger/logger.rb', line 122 def debug! .level = DEBUG end |
#debug? ⇒ Boolean
Returns true iff the current severity level allows for the printing of DEBUG messages.
117 118 119 |
# File 'lib/testa_logger/logger.rb', line 117 def debug? .level <= DEBUG end |
#error(tag = "", *args, &block) ⇒ Object
95 96 97 |
# File 'lib/testa_logger/logger.rb', line 95 def error(tag = "", *args, &block) add_log_to_queue(ERROR, tag, args, &block) end |
#error! ⇒ Object
Sets the severity to ERROR.
155 156 157 |
# File 'lib/testa_logger/logger.rb', line 155 def error! .level = ERROR end |
#error? ⇒ Boolean
Returns true iff the current severity level allows for the printing of ERROR messages.
150 151 152 |
# File 'lib/testa_logger/logger.rb', line 150 def error? .level <= ERROR end |
#fatal(tag = "", *args, &block) ⇒ Object
99 100 101 |
# File 'lib/testa_logger/logger.rb', line 99 def fatal(tag = "", *args, &block) add_log_to_queue(FATAL, tag, args, &block) end |
#fatal! ⇒ Object
Sets the severity to FATAL.
166 167 168 |
# File 'lib/testa_logger/logger.rb', line 166 def fatal! .level = FATAL end |
#fatal? ⇒ Boolean
Returns true iff the current severity level allows for the printing of FATAL messages.
161 162 163 |
# File 'lib/testa_logger/logger.rb', line 161 def fatal? .level <= FATAL end |
#formatter ⇒ Object
79 80 81 |
# File 'lib/testa_logger/logger.rb', line 79 def formatter .formatter end |
#info(tag = "", *args, &block) ⇒ Object
87 88 89 |
# File 'lib/testa_logger/logger.rb', line 87 def info(tag = "", *args, &block) add_log_to_queue(INFO, tag, args, &block) end |
#info! ⇒ Object
Sets the severity to INFO.
133 134 135 |
# File 'lib/testa_logger/logger.rb', line 133 def info! .level = INFO end |
#info? ⇒ Boolean
Returns true iff the current severity level allows for the printing of INFO messages.
128 129 130 |
# File 'lib/testa_logger/logger.rb', line 128 def info? .level <= INFO end |
#level ⇒ Object
170 171 172 |
# File 'lib/testa_logger/logger.rb', line 170 def level .level end |
#level=(severity) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/testa_logger/logger.rb', line 107 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 |
# File 'lib/testa_logger/logger.rb', line 59 def setup_dispatcher @dispatcher = Dispatcher.new( self, .faye_url, .faye_token ) end |
#silence(severity = ERROR, &block) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/testa_logger/logger.rb', line 174 def silence(severity = ERROR, &block) old_level = .level .level = severity result = block&.call .level = old_level result end |
#start_write_thread ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/testa_logger/logger.rb', line 67 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
103 104 105 |
# File 'lib/testa_logger/logger.rb', line 103 def unknown(tag = "", *args, &block) add_log_to_queue(UNKNOWN, tag, args, &block) end |
#warn(tag = "", *args, &block) ⇒ Object
91 92 93 |
# File 'lib/testa_logger/logger.rb', line 91 def warn(tag = "", *args, &block) add_log_to_queue(WARN, tag, args, &block) end |
#warn! ⇒ Object
Sets the severity to WARN.
144 145 146 |
# File 'lib/testa_logger/logger.rb', line 144 def warn! .level = WARN end |
#warn? ⇒ Boolean
Returns true iff the current severity level allows for the printing of WARN messages.
139 140 141 |
# File 'lib/testa_logger/logger.rb', line 139 def warn? .level <= WARN end |