Class: SmartLoggerWrapper
- Inherits:
-
Logger
- Object
- Logger
- SmartLoggerWrapper
- Includes:
- Logger::Severity
- Defined in:
- lib/smart_logger_wrapper.rb,
lib/smart_logger_wrapper/options.rb,
lib/smart_logger_wrapper/version.rb,
lib/smart_logger_wrapper/options/to.rb,
lib/smart_logger_wrapper/utils/path.rb,
lib/smart_logger_wrapper/options/base.rb,
lib/smart_logger_wrapper/utils/backtrace.rb,
lib/smart_logger_wrapper/options/with_position.rb,
lib/smart_logger_wrapper/options/append_backtrace.rb
Defined Under Namespace
Constant Summary collapse
- BASE_OFFSET =
3
- NESTED_WRAPPER_OFFSET =
6
- SEVERITY_MAPPING =
{ debug: DEBUG, info: INFO, warn: WARN, error: ERROR, fatal: FATAL, unknown: UNKNOWN }.freeze
- DELEGETING_METHODS =
%i(<< reopen close log add level debug? level= progname datetime_format= datetime_format formatter sev_threshold sev_threshold= info? warn? error? fatal? progname= formatter=).freeze
- VERSION =
"0.5.2"
Instance Attribute Summary collapse
-
#base_offset ⇒ Object
readonly
Returns the value of attribute base_offset.
-
#loggers ⇒ Object
readonly
Returns the value of attribute loggers.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #depth ⇒ Object
- #format_message(severity, datetime, progname, msg) ⇒ Object
- #format_severity(severity) ⇒ Object
-
#initialize(logger = Logger.new(STDOUT), *loggers, base_offset: nil, parent: nil, **options) ⇒ SmartLoggerWrapper
constructor
A new instance of SmartLoggerWrapper.
- #offset ⇒ Object
- #with_option(option_name, *args) ⇒ Object
Constructor Details
#initialize(logger = Logger.new(STDOUT), *loggers, base_offset: nil, parent: nil, **options) ⇒ SmartLoggerWrapper
Returns a new instance of SmartLoggerWrapper.
23 24 25 26 27 28 29 |
# File 'lib/smart_logger_wrapper.rb', line 23 def initialize(logger = Logger.new(STDOUT), *loggers, base_offset: nil, parent: nil, **) @base_offset = base_offset || BASE_OFFSET @parent = parent @loggers = be_parent_of!(logger, *loggers).freeze @options = .freeze @_loggers_cache = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/smart_logger_wrapper.rb', line 122 def method_missing(method_name, *args, &block) if root? && Options.defined_option?(method_name) # When a root wrapper receives an defined option with the same name as the method name, # return a new logger wrapper with the option. @_loggers_cache[method_name] = {} unless @_loggers_cache.include?(method_name) logger_with_option = @_loggers_cache[method_name][args] ||= with_option(method_name, *args) return block.(logger_with_option) if block_given? logger_with_option else super end end |
Instance Attribute Details
#base_offset ⇒ Object (readonly)
Returns the value of attribute base_offset.
21 22 23 |
# File 'lib/smart_logger_wrapper.rb', line 21 def base_offset @base_offset end |
#loggers ⇒ Object (readonly)
Returns the value of attribute loggers.
21 22 23 |
# File 'lib/smart_logger_wrapper.rb', line 21 def loggers @loggers end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/smart_logger_wrapper.rb', line 21 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
21 22 23 |
# File 'lib/smart_logger_wrapper.rb', line 21 def parent @parent end |
Instance Method Details
#depth ⇒ Object
57 58 59 60 |
# File 'lib/smart_logger_wrapper.rb', line 57 def depth return 0 if root? parent.depth + 1 end |
#format_message(severity, datetime, progname, msg) ⇒ Object
66 67 68 |
# File 'lib/smart_logger_wrapper.rb', line 66 def (severity, datetime, progname, msg) loggers.first.send(:format_message, severity, datetime, progname, msg) end |
#format_severity(severity) ⇒ Object
62 63 64 |
# File 'lib/smart_logger_wrapper.rb', line 62 def format_severity(severity) loggers.first.send(:format_severity, severity) end |
#offset ⇒ Object
53 54 55 |
# File 'lib/smart_logger_wrapper.rb', line 53 def offset @base_offset + depth * NESTED_WRAPPER_OFFSET end |
#with_option(option_name, *args) ⇒ Object
70 71 72 73 |
# File 'lib/smart_logger_wrapper.rb', line 70 def with_option(option_name, *args) = .merge(option_name => args) self.class.new(*loggers, base_offset: base_offset, parent: parent, **) end |