Class: DefaultLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/pipeline_toolkit/default_logger.rb

Overview

DefaultLogger will log to stderr and a log file in the execution directory by default, as specified in this yml. You can specify your own file to use by calling init_logger with option = someconfig.yml

Constant Summary collapse

@@logger =
nil
@@options =
nil

Class Method Summary collapse

Class Method Details

.default_yamlObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pipeline_toolkit/default_logger.rb', line 18

def self.default_yaml    
"log4r_config:

# define all loggers ...
loggers:
  - name      : log
    trace     : true
    outputters:
      - stderr
      - logfile

# define all outputters (incl. formatters)      
outputters:
  - type     : StderrOutputter
    name     : stderr
    formatter:
      date_pattern: '%Y-%m-%d %H:%M:%S'
      pattern     : '%d %l: [#\{NAME\}] %m '
      type        : PatternFormatter
      
  - type     : FileOutputter
    name     : logfile
    filename : '#\{NAME\}.log'
    formatter:
      date_pattern: '%Y-%m-%d %H:%M:%S'
      pattern     : '%d %l: [#\{NAME\}] %m '
      type        : PatternFormatter"
end

.init_logger(options = {}) ⇒ Object

Initialize a new logger. Optional, as it will be called with default values when log methods are used. Options fields are as follows:

options ||= “pipeline” Logger name. Used to build the file name. Made available in config YML as #NAME

options ||= “DEBUG” Log level. Made available in config YML as #LEVEL

options ||= “log” Name of logger to use. If you specify multiple loggers in your YML, you can select which one to use here.

options ||= “log_config.yml” File to read configuration from.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pipeline_toolkit/default_logger.rb', line 63

def self.init_logger(options = {})
  options[:log_name] ||= "pipeline"
  options[:log_level] ||= 'DEBUG'
  options[:logger_name] ||= "log"
  options[:log_conf] ||= "log_config.yml"
  
  
  @@options = options
  
  ycfg = YamlConfigurator
  ycfg['LEVEL'] = options[:log_level]
  ycfg['NAME'] = options[:log_name]
  
  if File.exists?(options[:log_conf])
    #puts "using log_config.yml"
    h = File.open(options[:log_conf]) { |yf| YAML::load( yf ) }
  else
    #puts "using default yml"
    h = YAML.load default_yaml
  end
  #y h
  ycfg.decode_yaml h['log4r_config']
  
  @@logger = Logger[options[:logger_name]]
end

.loggerObject

Getter to return the logger



115
116
117
# File 'lib/pipeline_toolkit/default_logger.rb', line 115

def self.logger
  @@logger
end

.logger=(log) ⇒ Object

Setter for setting the logger to use

Parameters:

  • log (Logger)

    The new logger to use



108
109
110
# File 'lib/pipeline_toolkit/default_logger.rb', line 108

def self.logger=(log)
  @@logger = log
end

.method_missing(name, *args) ⇒ Object

Send log calls to the Logger, initializing it first if necessary.



92
93
94
95
96
97
98
99
100
101
# File 'lib/pipeline_toolkit/default_logger.rb', line 92

def self.method_missing(name, *args)
  @@logger || init_logger()

  # add trace message if the logger is tracing. we can't use the default trace (%t) because that'll just show this 
  # method_missing as the caller. Usefulness: rather low.
  
  args[0] += " (#{caller[0]})" if @@logger.trace && args[0].class == String

  @@logger.send(name, *args)
end