Class: Betterlog::Log
- Inherits:
-
Object
- Object
- Betterlog::Log
- Includes:
- Tins::SexySingleton
- Defined in:
- lib/betterlog/log.rb,
lib/betterlog/log/event.rb,
lib/betterlog/log/severity.rb,
lib/betterlog/log/event_formatter.rb
Defined Under Namespace
Classes: Event, EventFormatter, Severity
Class Method Summary collapse
Instance Method Summary collapse
- #context(data_hash) ⇒ Object
-
#debug(object, **rest) ⇒ Log
Logs a message on severity debug.
-
#error(object, **rest) ⇒ Log
Logs a message on severity error.
-
#fatal(object, **rest) ⇒ Log
Logs a message on severity fatal.
-
#info(object, **rest) ⇒ Log
Logs a message on severity info.
- #logger ⇒ Object
-
#measure(metric:, **rest, &block) ⇒ Log
Logs a time measure on severity debug, by default, this can be changed by passing the severity: keyword.
-
#metric(metric:, type:, value:, **rest) ⇒ Log
Logs a metric on severity debug, by default, this can be changed by passing the severity: keyword.
-
#output(object, **rest) ⇒ Log
Logs a message on severity debug, by default, this can be changed by passing the severity: keyword.
-
#warn(object, **rest) ⇒ Log
Logs a message on severity warn.
Class Method Details
.context(data_hash) ⇒ Object
130 131 132 |
# File 'lib/betterlog/log.rb', line 130 def self.context(data_hash) instance.context(data_hash) end |
Instance Method Details
#context(data_hash) ⇒ Object
125 126 127 128 |
# File 'lib/betterlog/log.rb', line 125 def context(data_hash) GlobalMetadata.add data_hash self end |
#debug(object, **rest) ⇒ Log
Logs a message on severity debug.
45 46 47 48 49 |
# File 'lib/betterlog/log.rb', line 45 def debug(object, **rest) protect do emit Log::Event.ify(object, severity: __method__, rest: rest) end end |
#error(object, **rest) ⇒ Log
Logs a message on severity error.
56 57 58 59 60 |
# File 'lib/betterlog/log.rb', line 56 def error(object, **rest) protect do emit Log::Event.ify(object, severity: __method__, rest: rest) end end |
#fatal(object, **rest) ⇒ Log
Logs a message on severity fatal.
67 68 69 70 71 |
# File 'lib/betterlog/log.rb', line 67 def fatal(object, **rest) protect do emit Log::Event.ify(object, severity: __method__, rest: rest) end end |
#info(object, **rest) ⇒ Log
Logs a message on severity info.
23 24 25 26 27 |
# File 'lib/betterlog/log.rb', line 23 def info(object, **rest) protect do emit Log::Event.ify(object, severity: __method__, rest: rest) end end |
#logger ⇒ Object
14 15 16 |
# File 'lib/betterlog/log.rb', line 14 def logger defined?(Rails) && Rails.logger || self.class.default_logger end |
#measure(metric:, **rest, &block) ⇒ Log
Logs a time measure on severity debug, by default, this can be changed by passing the severity: keyword.
If an error occurs during measurement details about it are added to the metric event.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/betterlog/log.rb', line 110 def measure(metric:, **rest, &block) raise ArgumentError, 'must be called with a block' unless block_given? time_block { yield } rescue => error e = Log::Event.ify(error) rest |= e.as_hash.subhash(:error_class, :backtrace, :message) rest[:message] = "#{rest[:message].inspect} while measuring metric #{metric}" raise error ensure protect do event = build_metric(metric: metric, type: 'seconds', value: timed_duration, **rest) emit event end end |
#metric(metric:, type:, value:, **rest) ⇒ Log
Logs a metric on severity debug, by default, this can be changed by passing the severity: keyword.
93 94 95 96 97 98 |
# File 'lib/betterlog/log.rb', line 93 def metric(metric:, type:, value:, **rest) protect do event = build_metric(metric: metric, type: type, value: value, **rest) emit event end end |
#output(object, **rest) ⇒ Log
Logs a message on severity debug, by default, this can be changed by passing the severity: keyword.
79 80 81 82 83 |
# File 'lib/betterlog/log.rb', line 79 def output(object, **rest) protect do emit Log::Event.ify(object, severity: rest[:severity], rest: rest) end end |
#warn(object, **rest) ⇒ Log
Logs a message on severity warn.
34 35 36 37 38 |
# File 'lib/betterlog/log.rb', line 34 def warn(object, **rest) protect do emit Log::Event.ify(object, severity: __method__, rest: rest) end end |