Module: Eymiha::EasyLog

Defined in:
lib/eymiha/util/easylog.rb

Overview

EasyLog is a quick and convenient way to implement logging in Ruby code. By requiring the eymiha/util/easylog code, Easylog is included into module and is subsequently available to all classes.

There is a natural flow to EasyLog logging. First logging is started somewhere on high

start_logging filename

then messages are written to the log with the appropriate severity (here an informational message is logged)

log_info "something happened: #{foo}"

and optionally, when finished,

finish_logging

The upshot is that logging can be done from anywhere, and is as simple and easy as possible.

The module builds five methods for EasyLog logging corresponding to the five Logger severities: log_debug, log_info, log_warn, log_error and log_fatal. Their arguments are a message to be logged, and optionally one or more EasyLoggers that will be the targets of the message.

Note the classify attribute of an EasyLogger stores the decision about adding the class name to the end of the message or not; by default it is true and class names are added.

easylogger.classify = false

will turn this off.

Two other convenience methods for each of the five Logger severities are also provided for checking and adjusting EasyLog threshold levels. The log_debug?, log_info?, log_warn?, log_error? and log_fatal? methods return true if a message at that level will be reported. Respectively, the log_level_debug, log_level_info, log_level_warn, log_level_error and log_level_fatal methods will set the threshold to pass only messages at or above the selected severity.

Instance Method Summary collapse

Instance Method Details

#change_easylogger(easylogger) ⇒ Object

Changes the EasyLogger used for EasyLog logging to the given EasyLogger.



180
181
182
# File 'lib/eymiha/util/easylog.rb', line 180

def change_easylogger(easylogger)
  EasyLogger.change_logger(easylogger)
end

#easyloggerObject

Returns the EasyLogger that is being used for EasyLog logging.



175
176
177
# File 'lib/eymiha/util/easylog.rb', line 175

def easylogger
  EasyLogger.default_logger
end

#finish_loggingObject

Turns off EasyLog logging until restarted.



185
186
187
# File 'lib/eymiha/util/easylog.rb', line 185

def finish_logging
  EasyLogger.finish_logging
end

#log_levelObject

Returns the threshold of the EasyLog, or nil if no Easylogger is set.



190
191
192
# File 'lib/eymiha/util/easylog.rb', line 190

def log_level
  EasyLogger.level
end

#start_logging(logdev, shift_age = 'weekly', shift_size = 1048576, subsecond_precision = nil) ⇒ Object

Creates a new EasyLogger instance with the given arguments, and uses it for all subsequent EasyLog logging. The arguments are the same as used in EasyLogger creation.



168
169
170
171
172
# File 'lib/eymiha/util/easylog.rb', line 168

def start_logging(logdev, shift_age = 'weekly', shift_size = 1048576,
                  subsecond_precision = nil)
  EasyLogger.finish_logging
  EasyLogger.new(logdev, shift_age, shift_size, subsecond_precision)
end