Module: Hatchet

Included in:
Middleware
Defined in:
lib/hatchet.rb,
lib/hatchet/message.rb,
lib/hatchet/railtie.rb,
lib/hatchet/version.rb,
lib/hatchet/middleware.rb,
lib/hatchet/configuration.rb,
lib/hatchet/level_manager.rb,
lib/hatchet/hatchet_logger.rb,
lib/hatchet/logger_appender.rb,
lib/hatchet/plain_formatter.rb,
lib/hatchet/simple_formatter.rb,
lib/hatchet/standard_formatter.rb,
lib/hatchet/backtrace_formatter.rb,
lib/hatchet/delegating_formatter.rb,
lib/hatchet/structured_formatter.rb,
lib/hatchet/thread_name_formatter.rb,
lib/hatchet/nested_diagnostic_context.rb

Overview

Public: Hatchet is a library for providing logging facilities whose levels are configurable to the class and module level.

It also provides the facility to have several appenders added to from a single log call with each appender capable of being configured to log at different levels.

Hatchet provides no logging implementations of its own. Instead it delegates to the standard Logger within the reference LoggerAppender implementation.

Defined Under Namespace

Modules: BacktraceFormatter, LevelManager, ThreadNameFormatter Classes: Configuration, DelegatingFormatter, HatchetLogger, LoggerAppender, Message, Middleware, NestedDiagnosticContext, PlainFormatter, Railtie, SimpleFormatter, StandardFormatter, StructuredFormatter

Constant Summary collapse

VERSION =

Public: The version of Hatchet.

'0.2.14'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.appendersObject

Internal: Returns the Array of configured appenders.



156
157
158
# File 'lib/hatchet.rb', line 156

def self.appenders
  configuration.appenders
end

.configurationObject

Internal: Returns the configuration object, initializing it when necessary.



162
163
164
# File 'lib/hatchet.rb', line 162

def self.configuration
  @config ||= Configuration.new
end

.configure(&block) ⇒ Object

Public: Method for configuring Hatchet.

block - Mandatory block which receives a Configuration object that can be

used to setup Hatchet.

Once the block returns each of the configured appenders has its formatter set as a StandardFormatter if one is not already set, and its levels Hash is set to the shared levels Hash if an explicit one has not been provided.

Example

Hatchet.configure do |config|
  # Set the level to use unless overridden (defaults to :info)
  config.level :info
  # Set the level for a specific class/module and its children
  config.level :debug, 'Namespace::Something::Nested'

  # Add as many appenders as you like
  config.appenders << Hatchet::LoggerAppender.new do |appender|
    # Set the logger that this is wrapping (required)
    appender.logger = Logger.new('log/test.log')
  end
end

Returns nothing.



138
139
140
# File 'lib/hatchet.rb', line 138

def self.configure(&block)
  configuration.configure(&block)
end

.included(klass) ⇒ Object

Internal: Hook that extends the class with Hatchet when it is included.

klass - The klass that Hatchet was included into.

Returns nothing.



172
173
174
# File 'lib/hatchet.rb', line 172

def self.included(klass)
  klass.extend Hatchet
end

.registered(app) ⇒ Object

Public: Callback method for when Hatchet is registered as a Sinatra helper.

Example

register Hatchet

Returns nothing.



150
151
152
# File 'lib/hatchet.rb', line 150

def self.registered(app)
  app.helpers Hatchet
end

Instance Method Details

#loggerObject Also known as: log

Public: Returns a HatchetLogger for the object.

The 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

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

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

The 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.

Returns a HatchetLogger for the object.



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

def logger
  @_hatchet_logger ||= HatchetLogger.new(self, Hatchet.configuration, Hatchet::NestedDiagnosticContext.current)
end

#to_yaml_propertiesObject

Internal: Definition to avoid the cache variable from being persisted when an instance including Hatchet is marshalled into YAML.



179
180
181
# File 'lib/hatchet.rb', line 179

def to_yaml_properties
  super - [:@_hatchet_logger]
end