Class: NRSER::Log::Logger
- Defined in:
- lib/nrser/log/logger.rb
Overview
Extension of SemanticLogger::Logger to add and customize behavior.
Defined Under Namespace
Classes: Catcher
Instance Attribute Summary collapse
-
#awesome_name ⇒ String
readonly
TODO document ‘awesome_name` attribute.
Class Method Summary collapse
-
.args_to_kwds(*args) ⇒ Hash<Symbol, Object>
Normalizes the valid argument formats to SemanticLogger::Logger‘s “standard logging methods” into a keywords hash.
-
.is_exception?(value) ⇒ Boolean
Under JRuby a java exception is not a Ruby Exception Java::JavaLang::ClassCastException.new.is_a?(Exception) => false.
-
.plugin(plugin_class) ⇒ nil
Install a plugin, dynamically adding it’s Plugin.method_name instance method.
Instance Method Summary collapse
-
#build_log(level, index, message = nil, payload = nil, exception = nil, &block) ⇒ Object
Log message at the specified level.
-
#catch(**options) ⇒ Catcher
A sweet way to try something and just log any Exception.
-
#initialize(subject, level: nil, filter: nil) ⇒ Logger
constructor
Overrides SemanticLogger::Logger#initialize¹ to extend “subject” support to instances (as well as modules/classes and strings).
-
#with_level(level, &block) ⇒ RESULT
Set the level for the execution of ‘&block`, restoring it to it’s previous level afterwards.
Constructor Details
#initialize(subject, level: nil, filter: nil) ⇒ Logger
Overrides SemanticLogger::Logger#initialize¹ to extend “subject” support to instances (as well as modules/classes and strings).
> ¹ SemanticLogger::Logger#initialize is just a proxy to > SemanticLogger::Base#initialize, which is what this implementation > is based off.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/nrser/log/logger.rb', line 265 def initialize subject, level: nil, filter: nil # Support filtering all messages to this logger using a Regular Expression # or Proc unless filter.nil? || filter.is_a?( Regexp ) || filter.is_a?( Proc ) raise NRSER::TypeError, ":filter must be a Regexp or Proc", filter: filter, subject: subject, level: level end @filter = filter.is_a?(Regexp) ? filter.freeze : filter # @name = klass.is_a?(String) ? klass : klass.name case subject when String @name = subject @awesome_name = subject @type = :string when Module @name = subject.safe_name @awesome_name = subject.ai multiline: true, raw: true @type = subject.is_a?( Class ) ? :class : :module else @name = subject.to_s @awesome_name = subject.ai multiline: true, raw: true @type = :instance end if level.nil? # Allow the global default level to determine this loggers log level @level_index = nil @level = nil else self.level = level end end |
Instance Attribute Details
#awesome_name ⇒ String (readonly)
TODO document ‘awesome_name` attribute.
231 232 233 |
# File 'lib/nrser/log/logger.rb', line 231 def awesome_name @awesome_name end |
Class Method Details
.args_to_kwds(*args) ⇒ Hash<Symbol, Object>
Normalizes the valid argument formats to SemanticLogger::Logger‘s “standard logging methods” into a keywords hash.
By “standard logging methods”, we mean SemanticLogger::Logger#debug, SemanticLogger::Logger#info, etc., as detailed in the
- Semantic Logger API][
-
documentation.
[Semantic Logger API]: rocketjob.github.io/semantic_logger/api.html
Semantic Logger documents the standard logging methods API as
log.info(, payload_or_exception = nil, exception = nil, &block)
or
logger.info(
message: String?,
payload: Hash?,
exception: Object?,
**metric_keywords
)
Which means that the hash returned by this method can be used as the argument to the standard logging methods:
def proxy_info_call *args, &block
logger.info **NRSER::Log::Logger.args_to_kwds( *args ), &block
end
This makes this method very useful when pre-processing log call arguments, as is done in Plugin before passing the resulting keywords to Plugin#call because the hash this method returns is much easier to work with than the standard logging methods’ multiple valid argument formats.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/nrser/log/logger.rb', line 131 def self.args_to_kwds *args log_kwds = { message: nil, payload: nil, exception: nil, metric: nil, } case args.length when 0 # pass - they all stay `nil` (which it seems SemLog allows, so we'll # stick to it) when 1 case args[0] when Hash if [:message, :payload, :exception, :metric]. any? { |key| args[0].key? key } log_kwds = args[0] else # It's the payload log_kwds[:payload] = args[0] end else if is_exception? args[0] # It's the exception log_kwds[:exception] = args[0] else # It's got to be the message log_kwds[:message] = args[0] end end when 2 if args[0].is_a? Hash log_kwds[:payload] = args[0] log_kwds[:exception] = args[1] else log_kwds[:message] = args[0] if args[1].is_a? Hash log_kwds[:payload] = args[1] else log_kwds[:exception] = args[1] end end when 3 log_kwds[:message] = args[0] log_kwds[:payload] = args[1] log_kwds[:exception] = args[2] else raise NRSER::ArgumentError.new \ "Too many args dude - max 3", args: args end log_kwds end |
.is_exception?(value) ⇒ Boolean
Under JRuby a java exception is not a Ruby Exception
Java::JavaLang::ClassCastException.new.is_a?(Exception) => false
71 72 73 74 |
# File 'lib/nrser/log/logger.rb', line 71 def self.is_exception? value value.is_a?( Exception) || [:backtrace, :message].all? { |name| value.respond_to? name } end |
.plugin(plugin_class) ⇒ nil
Install a plugin, dynamically adding it’s Plugin.method_name instance method.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/nrser/log/logger.rb', line 207 def self.plugin plugin_class method_name = plugin_class.method_name if instance_methods.include? method_name.to_sym raise NRSER::ConflictError.new \ "Can not install", plugin_class.safe_name, "Logger plugin: instance method", method_name, "already defined" end define_method method_name do |*args, &block| plugin_class.new self, *args, &block end nil end |
Instance Method Details
#build_log(level, index, message = nil, payload = nil, exception = nil, &block) ⇒ Object
Log message at the specified level
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/nrser/log/logger.rb', line 311 def build_log level, index, = nil, payload = nil, exception = nil, &block log = SemanticLogger::Log.new name, level, index should_log = if payload.nil? && exception.nil? && .is_a?( Hash ) # Check if someone just logged a hash payload instead of meaning to call semantic logger if .key?( :message ) || .key?( :payload ) || .key?( :exception ) || .key?( :metric ) log.assign else log.assign_positional nil, , nil, &block end else log.assign_positional , payload, exception, &block end # Log level may change during assign due to :on_exception_level [ log, should_log && should_log?( log ) ] end |
#catch(**options) ⇒ Catcher
A sweet way to try something and just log any Exception.
Useful for situations where the operation is question is not necessary or can not be allowed to propagate errors, but you would like to record and/or let the user know that it failed.
Create a new Catcher for this logger that defines the log methods (‘#error`, `warn`, … `:trace`) to also accept blocks that will be executed in a `begin`/`rescue`.
If the block raises, the catcher will call the log method, adding the caught Exception.
385 386 387 |
# File 'lib/nrser/log/logger.rb', line 385 def catch ** Catcher.new self, ** end |
#with_level(level, &block) ⇒ RESULT
Set the level for the execution of ‘&block`, restoring it to it’s previous level afterwards.
Like what SemanticLogger::Logger#silence does (which just forwards to SemanticLogger.silence), but applies only to this logger (where as SemanticLogger::Logger#silence applies on to the global default level).
Useful for quickly turning down the log level to see trace/debug output from a specific section.
409 410 411 412 413 414 415 416 417 418 |
# File 'lib/nrser/log/logger.rb', line 409 def with_level level, &block prior_level = @level self.level = level begin block.call ensure self.level = prior_level end end |