Class: LunaPark::Notifiers::Log
- Inherits:
-
Object
- Object
- LunaPark::Notifiers::Log
- Includes:
- Extensions::SeverityLevels
- Defined in:
- lib/luna_park/notifiers/log.rb,
lib/luna_park/notifiers/log/formatters.rb
Overview
Developers need a tool to help them analyze the application. Mostly they use loggers that display messages in files or in STDOUT. But if you have a lot of microservices, analyzing the logs stored in files on different servers can be a problem. You can use some real-time error tracking and debugging tools such as Bugsnag, Rollbar, etc.
Notifiers are an abstraction that breaks the dependency on a particular implementation of a developer’s tool. Current notifier implement abstraction over ruby logger.
For example output to log file message at json format
# Define logger and output destination file or STDOUT\STDERR
logger = Logger.new('./log/app.log')
# Set output format and minimum level output level
notifier = LunaPark::Notifiers::Logger.new(logger: logger, format: :json, min_lvl: :debug)
notifier.info 'Answer to the Ultimate Question of Life, the Universe and Everything', hint: '21 * 2'
And you will get to ‘./log/app.log’
I, [2020-06-13T21:53:00.824304 #37992] INFO -- : {"message":"Answer to the Ultimate Question of Life,
the Universe and Everything","details":{"hint":"21 * 2"}}
Defined Under Namespace
Modules: Formatters
Constant Summary
Constants included from Extensions::SeverityLevels
Extensions::SeverityLevels::LEVELS
Instance Attribute Summary collapse
-
#formatter ⇒ Object
readonly
Callable object to format log messages.
-
#logger ⇒ Object
readonly
Logger which used for output all notify.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(logger: nil, format: nil, formatter: nil, min_lvl: :debug) ⇒ Log
constructor
Create new log notifier.
-
#post(msg, lvl: :error, **details) ⇒ Object
Send a notification to the log.
Methods included from Extensions::SeverityLevels
#debug, #error, #fatal, #info, #min_lvl, #min_lvl=, #unknown, #warning
Constructor Details
#initialize(logger: nil, format: nil, formatter: nil, min_lvl: :debug) ⇒ Log
Create new log notifier
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/luna_park/notifiers/log.rb', line 65 def initialize(logger: nil, format: nil, formatter: nil, min_lvl: :debug) @logger = logger || self.class.default_logger @formatter = formatter || Formatters::SINGLE # @todo Remove format param in the future version unless format.nil? @formatter = formatter_by_name(format) warn 'warn [DEPRECATION] `format` parameter is deprecated, use `formatter` instead' end self.min_lvl = min_lvl end |
Instance Attribute Details
#formatter ⇒ Object (readonly)
Callable object to format log messages.
pretty_formatter = ->(klass, message, details) { “#klass - #message - #details” } notifier = LunaPark::Notifiers::Log.new(formatter: pretty_formatter) notifier.info(‘You hear’, dog: ‘wow’, cats: ‘mow’, timmy: ‘mow’)
> I, [2022-09-29T10:51:15.753646 #28763] INFO – : String - You hear - :cats=>{:chloe=>“mow”, :timmy=>“mow”}
39 40 41 |
# File 'lib/luna_park/notifiers/log.rb', line 39 def formatter @formatter end |
#logger ⇒ Object (readonly)
Logger which used for output all notify. Should be instance of Logger class. You can define it in two ways.
-
On class class Stderr < LunaPark::Notifier::Log
logger Logger.new('example.log')
end
stderr = Stderr.new stderr.logger # => #<Logger:0x000056445e1e2118 … @filename=“example.log”…
Or in the initialize of the instance
-
On instance stderr = LunaPark::Notifier::Log.new(logger: Logger.new(STDERR)) stderr.logger # => #<Logger:0x000056445e1e2118 … @filename=“example.log”…
At default value is Logger.new(STDOUT)
57 58 59 |
# File 'lib/luna_park/notifiers/log.rb', line 57 def logger @logger end |
Class Method Details
.default_logger ⇒ Object
92 93 94 |
# File 'lib/luna_park/notifiers/log.rb', line 92 def default_logger @default_logger ||= Logger.new(STDOUT) end |
.logger(val) ⇒ Object
88 89 90 |
# File 'lib/luna_park/notifiers/log.rb', line 88 def logger(val) @default_logger = val end |
Instance Method Details
#post(msg, lvl: :error, **details) ⇒ Object
Send a notification to the log.
81 82 83 84 85 |
# File 'lib/luna_park/notifiers/log.rb', line 81 def post(msg, lvl: :error, **details) severity = severity(lvl) = serialize(msg, **details) logger.add severity, end |