Class: Hatchet::HatchetLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/hatchet_logger.rb

Overview

Public: Class that handles logging calls and distributes them to all its appenders.

Each logger has 5 logging methods. Those are, in decreasing order of severity:

* fatal
* error
* warn
* info
* debug

All these methods have the same signature. You can either provide a message as a direct string, or as a block to the method is lazily evaluated (this is the recommended option).

Examples

log.info "Informational message"
log.info { "Informational message #{potentially_expensive}" }

It is also possible to pass an error to associate with a message. It is down to the appender what it will do with the error (such as including the stack trace) so it is recommended you still include basic information within the message you pass.

Examples

log.error "Something bad happened - #{error.message}", error

Log messages are sent to each appender where they will be filtered and invoked as configured.

Each logger also has 5 inspection methods. Those are, in decreasing order of severity:

* fatal?
* error?
* warn?
* info?
* debug?

All these methods take no arguments and return true if any of the loggers’ appenders will log a message at that level for the current context, otherwise they will return false.

Constant Summary collapse

STANDARD_TO_SYMBOL =

Internal: Map from standard library levels to Symbols.

{
  Logger::DEBUG => :debug,
  Logger::INFO  => :info,
  Logger::WARN  => :warn,
  Logger::ERROR => :error,
  Logger::FATAL => :fatal
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, configuration, ndc) ⇒ HatchetLogger

Internal: Creates a new logger.

host - The object the logger gains its context from. configuration - The configuration of Hatchet. ndc - The nested diagnostic context of the logger.



75
76
77
78
79
# File 'lib/hatchet/hatchet_logger.rb', line 75

def initialize(host, configuration, ndc)
  @context = host_name(host)
  @configuration = configuration
  @ndc = ndc
end

Instance Attribute Details

#ndcObject (readonly)

Public: Gets the NestedDiagnosticContext for the logger.



67
68
69
# File 'lib/hatchet/hatchet_logger.rb', line 67

def ndc
  @ndc
end

Instance Method Details

#<<(msg) ⇒ Object

Public: No-op, exists for greater compatibility with things expecting a standard Logger.



348
349
350
# File 'lib/hatchet/hatchet_logger.rb', line 348

def <<(msg)
  nil
end

#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log

Public: Adds a message to each appender at the specified level.

level - The level of the message. One of, in decreasing order of

severity:

  * Logger::FATAL
  * Logger::ERROR
  * Logger::WARN
  * Logger::INFO
  * Logger::DEBUG
  * :fatal
  * :error
  * :warn
  * :info
  * :debug

message - The message that will be logged by an appender when it is

configured to log at the given level or lower.

block - An optional block which will provide a message when invoked.

Writes messages to STDOUT if any appender fails to complete the enabled check or log the message.

Also aliased as log.

Returns nothing.



338
339
340
341
# File 'lib/hatchet/hatchet_logger.rb', line 338

def add(severity, message = nil, progname = nil, &block)
  level = STANDARD_TO_SYMBOL[severity] || severity
  add_to_appenders(level, message, nil, &block)
end

#debug(message = nil, error = nil, &block) ⇒ Object

Public: Logs a message at debug level.

message - An already evaluated message, usually a String (default: nil). error - An error which is associated with the message (default: nil). block - An optional block which will provide a message when invoked.

One of message or block must be provided. If both are provided then the block is preferred as it is assumed to provide more detail.

In general, you should use the block style for any message not related to an error. This is because any unneccessary String interpolation is avoided making unwritten debug calls, for example, less expensive.

When logging errors it is advised that you include some details of the error within the regular message, perhaps the error’s message, but leave the inclusion of the stack trace up to your appenders and their formatters.

Examples

debug { "A fine grained message" }
debug "A message relating to an exception", e

Returns nothing.



106
107
108
# File 'lib/hatchet/hatchet_logger.rb', line 106

def debug(message = nil, error = nil, &block)
  add_to_appenders(:debug, message, error, &block)
end

#debug?Boolean

Public: Returns true if any of the appenders will log messages for the current context at debug level, otherwise returns false.

Writes messages to STDOUT if any appender fails to complete the check.

Returns:

  • (Boolean)


115
116
117
# File 'lib/hatchet/hatchet_logger.rb', line 115

def debug?
  enabled? :debug
end

#error(message = nil, error = nil, &block) ⇒ Object

Public: Logs a message at error level.

message - An already evaluated message, usually a String (default: nil). error - An error which is associated with the message (default: nil). block - An optional block which will provide a message when invoked.

One of message or block must be provided. If both are provided then the block is preferred as it is assumed to provide more detail.

In general, you should use the block style for any message not related to an error. This is because any unneccessary String interpolation is avoided making unwritten error calls, for example, less expensive.

When logging errors it is advised that you include some details of the error within the regular message, perhaps the error’s message, but leave the inclusion of the stack trace up to your appenders and their formatters.

Examples

error { "A fine grained message" }
error "A message relating to an exception", e

Returns nothing.



220
221
222
# File 'lib/hatchet/hatchet_logger.rb', line 220

def error(message = nil, error = nil, &block)
  add_to_appenders(:error, message, error, &block)
end

#error?Boolean

Public: Returns true if any of the appenders will log messages for the current context at error level, otherwise returns false.

Writes messages to STDOUT if any appender fails to complete the check.

Returns:

  • (Boolean)


229
230
231
# File 'lib/hatchet/hatchet_logger.rb', line 229

def error?
  enabled? :error
end

#fatal(message = nil, error = nil, &block) ⇒ Object

Public: Logs a message at fatal level.

message - An already evaluated message, usually a String (default: nil). error - An error which is associated with the message (default: nil). block - An optional block which will provide a message when invoked.

One of message or block must be provided. If both are provided then the block is preferred as it is assumed to provide more detail.

In general, you should use the block style for any message not related to an error. This is because any unneccessary String interpolation is avoided making unwritten fatal calls, for example, less expensive.

When logging errors it is advised that you include some details of the error within the regular message, perhaps the error’s message, but leave the inclusion of the stack trace up to your appenders and their formatters.

Examples

fatal { "A fine grained message" }
fatal "A message relating to an exception", e

Returns nothing.



258
259
260
# File 'lib/hatchet/hatchet_logger.rb', line 258

def fatal(message = nil, error = nil, &block)
  add_to_appenders(:fatal, message, error, &block)
end

#fatal?Boolean

Public: Returns true if any of the appenders will log messages for the current context at fatal level, otherwise returns false.

Writes messages to STDOUT if any appender fails to complete the check.

Returns:

  • (Boolean)


267
268
269
# File 'lib/hatchet/hatchet_logger.rb', line 267

def fatal?
  enabled? :fatal
end

#formatterObject

Public: Returns nil, exists for greater compatibility with things expecting a standard Logger.



300
301
302
# File 'lib/hatchet/hatchet_logger.rb', line 300

def formatter
  nil
end

#formatter=(formatter) ⇒ Object

Public: No-op, exists for greater compatibility with things expecting a standard Logger.



307
308
309
# File 'lib/hatchet/hatchet_logger.rb', line 307

def formatter=(formatter)
  # no-op for Logger protocol compatibility
end

#info(message = nil, error = nil, &block) ⇒ Object

Public: Logs a message at info level.

message - An already evaluated message, usually a String (default: nil). error - An error which is associated with the message (default: nil). block - An optional block which will provide a message when invoked.

One of message or block must be provided. If both are provided then the block is preferred as it is assumed to provide more detail.

In general, you should use the block style for any message not related to an error. This is because any unneccessary String interpolation is avoided making unwritten info calls, for example, less expensive.

When logging errors it is advised that you include some details of the error within the regular message, perhaps the error’s message, but leave the inclusion of the stack trace up to your appenders and their formatters.

Examples

info { "A fine grained message" }
info "A message relating to an exception", e

Returns nothing.



144
145
146
# File 'lib/hatchet/hatchet_logger.rb', line 144

def info(message = nil, error = nil, &block)
  add_to_appenders(:info, message, error, &block)
end

#info?Boolean

Public: Returns true if any of the appenders will log messages for the current context at info level, otherwise returns false.

Writes messages to STDOUT if any appender fails to complete the check.

Returns:

  • (Boolean)


153
154
155
# File 'lib/hatchet/hatchet_logger.rb', line 153

def info?
  enabled? :info
end

#levelObject

Public: Returns the default level of the logger’s configuration.



273
274
275
# File 'lib/hatchet/hatchet_logger.rb', line 273

def level
  @configuration.default_level
end

#level=(level) ⇒ Object

Public: Set the lowest level of message to log by default.

level - The lowest level of message to log by default.

The use of this method is not recommended as it affects the performance of the logging. It is only provided for compatibility.

Returns nothing.



286
287
288
289
290
291
292
293
294
295
# File 'lib/hatchet/hatchet_logger.rb', line 286

def level=(level)
  level = case level
          when Symbol
            level
          else
            STANDARD_TO_SYMBOL[level] || :info
          end

  @configuration.level level
end

#to_yaml_propertiesObject

Internal: Specifies the instance variables to be serialized when converting the logger to YAML.



355
356
357
# File 'lib/hatchet/hatchet_logger.rb', line 355

def to_yaml_properties
  [:@context]
end

#warn(message = nil, error = nil, &block) ⇒ Object

Public: Logs a message at warn level.

message - An already evaluated message, usually a String (default: nil). error - An error which is associated with the message (default: nil). block - An optional block which will provide a message when invoked.

One of message or block must be provided. If both are provided then the block is preferred as it is assumed to provide more detail.

In general, you should use the block style for any message not related to an error. This is because any unneccessary String interpolation is avoided making unwritten warn calls, for example, less expensive.

When logging errors it is advised that you include some details of the error within the regular message, perhaps the error’s message, but leave the inclusion of the stack trace up to your appenders and their formatters.

Examples

warn { "A fine grained message" }
warn "A message relating to an exception", e

Returns nothing.



182
183
184
# File 'lib/hatchet/hatchet_logger.rb', line 182

def warn(message = nil, error = nil, &block)
  add_to_appenders(:warn, message, error, &block)
end

#warn?Boolean

Public: Returns true if any of the appenders will log messages for the current context at warn level, otherwise returns false.

Writes messages to STDOUT if any appender fails to complete the check.

Returns:

  • (Boolean)


191
192
193
# File 'lib/hatchet/hatchet_logger.rb', line 191

def warn?
  enabled? :warn
end