Class: Ougai::Logger
- Inherits:
-
Logger
- Object
- Logger
- Ougai::Logger
- Includes:
- Logging
- Defined in:
- lib/ougai/logger.rb
Overview
Main Logger
Constant Summary
Constants included from Ougai::Logging::Severity
Ougai::Logging::Severity::SEV_LABEL, Ougai::Logging::Severity::TRACE
Instance Attribute Summary collapse
-
#before_log ⇒ Proc
Hook before logging.
-
#default_message ⇒ String
Use this if log message is not specified (by default this is ‘No message’).
-
#exc_key ⇒ String
The field name of Exception (by default this is :err).
-
#with_fields ⇒ Hash
The fields appending to all logs.
Class Method Summary collapse
-
.broadcast(logger) ⇒ Object
Broadcasts the same logs to the another logger.
- .child_class ⇒ Object
- .child_class=(klass) ⇒ Object
- .inherited(subclass) ⇒ Object
Instance Method Summary collapse
- #chain(severity, args, fields, hooks) ⇒ Object
-
#child(fields = {}) ⇒ ChildLogger
Creates a child logger and returns it.
-
#initialize ⇒ Logger
constructor
A new instance of Logger.
- #level=(severity) ⇒ Object
Methods included from Logging
#debug, #error, #fatal, #info, #trace, #trace?, #unknown, #warn
Methods included from Ougai::Logging::Severity
Constructor Details
#initialize ⇒ Logger
Returns a new instance of Logger.
14 15 16 17 18 19 20 21 |
# File 'lib/ougai/logger.rb', line 14 def initialize(*, **) super @before_log = nil @default_message = 'No message' @exc_key = :err @with_fields = {} @formatter = create_formatter if @formatter.nil? end |
Instance Attribute Details
#before_log ⇒ Proc
Hook before logging.
9 10 11 |
# File 'lib/ougai/logger.rb', line 9 def before_log @before_log end |
#default_message ⇒ String
Use this if log message is not specified (by default this is ‘No message’).
9 10 11 |
# File 'lib/ougai/logger.rb', line 9 def @default_message end |
#exc_key ⇒ String
The field name of Exception (by default this is :err).
9 10 11 |
# File 'lib/ougai/logger.rb', line 9 def exc_key @exc_key end |
#with_fields ⇒ Hash
The fields appending to all logs.
9 10 11 |
# File 'lib/ougai/logger.rb', line 9 def with_fields @with_fields end |
Class Method Details
.broadcast(logger) ⇒ Object
Broadcasts the same logs to the another logger
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ougai/logger.rb', line 39 def self.broadcast(logger) Module.new do |mdl| define_method(:log) do |*args| logger.log(*args) super(*args) end define_method(:level=) do |level| logger.level = level super(level) end define_method(:close) do logger.close super() end end end |
.child_class ⇒ Object
24 25 26 |
# File 'lib/ougai/logger.rb', line 24 def child_class @child_class ||= ChildLogger end |
.child_class=(klass) ⇒ Object
28 29 30 |
# File 'lib/ougai/logger.rb', line 28 def child_class=(klass) @child_class = klass end |
.inherited(subclass) ⇒ Object
32 33 34 |
# File 'lib/ougai/logger.rb', line 32 def inherited(subclass) subclass.child_class = Class.new(ChildLogger) end |
Instance Method Details
#chain(severity, args, fields, hooks) ⇒ Object
86 87 88 89 |
# File 'lib/ougai/logger.rb', line 86 def chain(severity, args, fields, hooks) hooks.push(@before_log) if @before_log write(severity, args, weak_merge!(fields, @with_fields), hooks) end |
#child(fields = {}) ⇒ ChildLogger
Creates a child logger and returns it.
75 76 77 78 79 80 81 82 83 |
# File 'lib/ougai/logger.rb', line 75 def child(fields = {}) ch = self.class.child_class.new(self, fields) if !block_given? ch else yield ch end end |
#level=(severity) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ougai/logger.rb', line 58 def level=(severity) if severity.is_a?(Integer) @level = severity return end if severity.to_s.downcase == 'trace' @level = TRACE return end super end |