Class: OptionalLogger::Logger
- Inherits:
-
Object
- Object
- OptionalLogger::Logger
- Defined in:
- lib/optional_logger/logger.rb
Overview
Proxy logger to handle making the proxied logger optional
The original intent for this optional proxy logger was specifically to be used inside of gems. This would enable them to receive a logger from the parenting application/gem and log within their gem without having to worry about spreading conditionals all over their gem.
Instance Method Summary collapse
-
#add(severity, message = nil, progname_or_message = nil, &block) ⇒ Object
(also: #log)
Log a message at the given level if the logger is present.
-
#debug(progname_or_message = nil, &block) ⇒ Object
Log a message at the debug level if the logger is present.
-
#debug? ⇒ Boolean
Check if debug level messages are allowed by loggers current configuration.
-
#error(progname_or_message = nil, &block) ⇒ Object
Log a message at the error level if the logger is present.
-
#error? ⇒ Boolean
Check if error level messages are allowed by loggers current configuration.
-
#fatal(progname_or_message = nil, &block) ⇒ Object
Log a message at the fatal level if the logger is present.
-
#fatal? ⇒ Boolean
Check if fatal level messages are allowed by loggers current configuration.
-
#info(progname_or_message = nil, &block) ⇒ Object
Log a message at the info level if the logger is present.
-
#info? ⇒ Boolean
Check if info level messages are allowed by loggers current configuration.
-
#initialize(logger) ⇒ Object
constructor
Construct an instance of the proxy logger.
-
#unknown(progname_or_message = nil, &block) ⇒ Object
Log a message at the unknown level if the logger is present.
-
#warn(progname_or_message = nil, &block) ⇒ Object
Log a message at the warn level if the logger is present.
-
#warn? ⇒ Boolean
Check if warn level messages are allowed by loggers current configuration.
-
#wrapped_logger ⇒ Object
Get the ruby logger instance this proxy logger wraps.
Constructor Details
#initialize(logger) ⇒ Object
Construct an instance of the proxy logger
16 17 18 |
# File 'lib/optional_logger/logger.rb', line 16 def initialize(logger) @logger = logger end |
Instance Method Details
#add(severity, message = nil, progname_or_message = nil, &block) ⇒ Object Also known as: log
Log a message at the given level if the logger is present
This isn’t generally a recommended method to use while logging in your libraries. Instead see #debug, #info, #warn, #error, #fatal, and #unknown as they simplify messaging considerably.
If you don’t want to use the recommended methods and want more control for some reason. There are a few ways to log a message depending on your usecase.
The first is probably the most generally useful, which is logging messages that aren’t costly to build. This is accomplished as follows:
add(::Logger::INFO, 'some message', nil)
The second is for use cases in which it is costly to build the log message as it avoids executing the block until the logger knows that the message level will be displayed. If the message level would not be displayed then the block is not executed, saving the performance cost of building the message.
add(::Logger::INFO) { 'some expensive message' }
The third gives you the benefits of preventing uneeded expensive messages but also allows you to override the program name that will be prefixed on that log message. This is accomplished as follows.
add(::Logger::INFO, nil, 'overridden progname') { 'some expensive message' }
It is important to realize that if the wrapped logger is nil then this method will simply do nothing.
67 68 69 |
# File 'lib/optional_logger/logger.rb', line 67 def add(severity, = nil, = nil, &block) @logger.add(severity, , , &block) if @logger end |
#debug(progname_or_message = nil, &block) ⇒ Object
Log a message at the debug level if the logger is present
See #info for details on specifics around various scenarios
114 115 116 |
# File 'lib/optional_logger/logger.rb', line 114 def debug( = nil, &block) add(::Logger::DEBUG, nil, , &block) end |
#debug? ⇒ Boolean
Check if debug level messages are allowed by loggers current configuration
164 165 166 167 |
# File 'lib/optional_logger/logger.rb', line 164 def debug? return @logger.debug? if @logger false end |
#error(progname_or_message = nil, &block) ⇒ Object
Log a message at the error level if the logger is present
See #info for details on specifics around various scenarios
121 122 123 |
# File 'lib/optional_logger/logger.rb', line 121 def error( = nil, &block) add(::Logger::ERROR, nil, , &block) end |
#error? ⇒ Boolean
Check if error level messages are allowed by loggers current configuration
184 185 186 187 |
# File 'lib/optional_logger/logger.rb', line 184 def error? return @logger.error? if @logger false end |
#fatal(progname_or_message = nil, &block) ⇒ Object
Log a message at the fatal level if the logger is present
See #info for details on specifics around various scenarios
128 129 130 |
# File 'lib/optional_logger/logger.rb', line 128 def fatal( = nil, &block) add(::Logger::FATAL, nil, , &block) end |
#fatal? ⇒ Boolean
Check if fatal level messages are allowed by loggers current configuration
174 175 176 177 |
# File 'lib/optional_logger/logger.rb', line 174 def fatal? return @logger.fatal? if @logger false end |
#info(progname_or_message = nil, &block) ⇒ Object
Log a message at the info level if the logger is present
There are a few ways to log a message depending on your usecase.
The first is probably the most generally useful, which is logging messages that aren’t costly to build. This is accomplished as follows:
info('some message')
The second is for use cases in which it is costly to build the log message as it avoids executing the block until the logger knows that the message level will be displayed. If the message level would not be displayed then the block is not executed, saving the performance cost of building the message.
info { 'some expensive message' }
The third gives you the benefits of preventing uneeded expensive messages but also allows you to override the program name that will be prefixed on that log message. This is accomplished as follows.
info('overridden progname') { 'some expensive message' }
It is important to realize that if the wrapped logger is nil then this method will simply do nothing.
100 101 102 |
# File 'lib/optional_logger/logger.rb', line 100 def info( = nil, &block) add(::Logger::INFO, nil, , &block) end |
#info? ⇒ Boolean
Check if info level messages are allowed by loggers current configuration
144 145 146 147 |
# File 'lib/optional_logger/logger.rb', line 144 def info? return @logger.info? if @logger false end |
#unknown(progname_or_message = nil, &block) ⇒ Object
Log a message at the unknown level if the logger is present
See #info for details on specifics around various scenarios
135 136 137 |
# File 'lib/optional_logger/logger.rb', line 135 def unknown( = nil, &block) add(::Logger::UNKNOWN, nil, , &block) end |
#warn(progname_or_message = nil, &block) ⇒ Object
Log a message at the warn level if the logger is present
See #info for details on specifics around various scenarios
107 108 109 |
# File 'lib/optional_logger/logger.rb', line 107 def warn( = nil, &block) add(::Logger::WARN, nil, , &block) end |
#warn? ⇒ Boolean
Check if warn level messages are allowed by loggers current configuration
154 155 156 157 |
# File 'lib/optional_logger/logger.rb', line 154 def warn? return @logger.warn? if @logger false end |
#wrapped_logger ⇒ Object
Get the ruby logger instance this proxy logger wraps
23 24 25 |
# File 'lib/optional_logger/logger.rb', line 23 def wrapped_logger @logger end |