Module: YetiLogger
- Extended by:
- ActiveSupport::Concern, Configuration
- Includes:
- LogMethods
- Defined in:
- lib/yeti_logger.rb,
lib/yeti_logger/version.rb,
lib/yeti_logger/constants.rb,
lib/yeti_logger/configuration.rb,
lib/yeti_logger/wrapped_logger.rb
Overview
Mixin module providing Yesware logging functionality including formatting of log message data (exceptions, hashes, etc). Refer to the Readme for further information and examples.
Module you can include in your class to get logging with class name prefixing. This will also log hashes as key=value pairs and exception backtraces. These methods are added via metaprogramming. When it’s done, you’ll have methods to log at each level:
-
debug
-
info
-
warn
-
error
Each method will have a signature that looks like:
log_info(obj = nil, exception = nil, &block)
Each of these arguments is optional. Pass no arguments results in a blank line being logged (with a classname prefix).
The ideal usage for YetiLogger is to pass in blocks:
log_info { "Here is my message with #{some} class #{values}" }
This will defer evaluation of the string until the logger has determined if the current log level is high enough to warrant evaluating the string and logging it.
Exceptions will be logged as a combination of the message, the class and some number of lines of the backtrace. If you pass in a value for obj that is a Hash, then the exception will be injected into the hash.
log_info("My message", exception)
If you only need the exception, then use the block form above:
log_info { exception }
The value passed in for obj or returned by the block will be formatted depending on the content of it. If it is a hash, we will format into “key=value” pairs separated by whitespace. If the value is an exception, the message will be logged along with the backtrace. All other objects are converted to string via the to_s method.
For hash logging, each key and value are converted to strings which means nested hashes might not serialize like you would think. Additionally, no quotes are provided around keys or values, meaning for hashes that contain data that may include whitespace, it might make sense to pass in a serialized form of the hash instead of the hash itself. If you would like to override this behavior, pass in the serialized format for the hash, such as:
log_info { hash.to_json }
log_info { hash.to_s }
log_info { hash.to_my_log_format }
Defined Under Namespace
Modules: ClassMethods, Configuration, LogMethods, MessageFormatters, TestHelper Classes: WrappedLogger
Constant Summary collapse
- VERSION =
"3.2.0"
- DEFAULT_OBJ_ARG =
Object.new
- LEVELS =
[ :fatal, :error, :warn, :info, :debug ]
- NUM_LINES_OF_EXCEPTIONS =
50
Instance Attribute Summary
Attributes included from Configuration
Instance Method Summary collapse
-
#as_logger ⇒ YetiLogger::WrapperLogger
Wrap self in an object that responds to :info, :warn, :error, :debug, etc.
- #log_class_name ⇒ Object
- #log_time(action, level = :info) ⇒ Object
Methods included from Configuration
Methods included from LogMethods
#log_debug, #log_error, #log_fatal, #log_info, #log_warn
Instance Method Details
#as_logger ⇒ YetiLogger::WrapperLogger
Wrap self in an object that responds to :info, :warn, :error, :debug, etc.
165 166 167 |
# File 'lib/yeti_logger.rb', line 165 def as_logger YetiLogger::WrappedLogger.new(self) end |
#log_class_name ⇒ Object
149 150 151 |
# File 'lib/yeti_logger.rb', line 149 def log_class_name self.class.name end |
#log_time(action, level = :info) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/yeti_logger.rb', line 153 def log_time(action, level = :info) ms = Benchmark.ms do yield end YetiLogger.logger.send(level, MessageFormatters.(self.class.name, { action: action, time_ms: ms.to_i })) end |