Class: Mole::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mole/logger.rb

Defined Under Namespace

Classes: ConfigurationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Logger

create a new logger



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mole/logger.rb', line 52

def initialize( opts = {} )
  @options = ::Mole::Logger.default_options.merge(opts)
  @log     = ::Logging::Logger[@options[:logger_name]]
  @layout  = ::Logging::Layouts::Pattern.new( { :pattern => @options[:layout_pattern] } )
 
  # add appenders explicitly, since this logger may already be defined and
  # already have loggers
  @appenders = []
  @appenders << log_file_appender if @options[:log_file]
  @appenders << email_appender if @options[:email_alerts_to]

  @log.appenders = @appenders
  @log.level = @options[:log_level]
  @log.additive = @options[:additive]
end

Instance Attribute Details

#logObject (readonly)

here for testing, don’t really use it.



8
9
10
# File 'lib/mole/logger.rb', line 8

def log
  @log
end

Class Method Details

.default_optionsObject

there are more options here than are typically utilized for the logger, they are made available if someone wants to utilize them directly.



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
46
47
48
# File 'lib/mole/logger.rb', line 20

def self.default_options #:nodoc:
  @default_options ||= {

    # log event layout pattern
    #    YYYY-MM-DDTHH:MM:SS 12345 LEVEL LoggerName : The Log message
    #
    :layout_pattern       => '%d %5p %5l %c : %m\n'.freeze  ,
    :logger_name          => Mole                           ,
    :additive             => true                           ,

    # log file configuration options
    #   age        -> how often to rotate the logs if a file is used
    #   keep_count -> how many of the log files to keep
    :log_level            => :info          ,
    :log_file             => $stdout        ,
    :log_file_age         => 'daily'.freeze ,
    :log_file_keep_count  => 7              ,

    # email logging options
    #   buffsize -> number of log events used as a threshold to send an
    #               email.  If that is not reached before the program
    #               exists then the at_exit handler for logging will flush
    #               the log to smtp.
    :email_alerts_to      => nil    ,
    :email_alert_level    => :error ,
    :email_alert_server   => nil    ,
    :email_alert_buffsize => 200    ,
  }
end

Instance Method Details

#email_appenderObject

an email appender that uses :email_alerts_to option to send emails to. :email_alerts_to can either be a singe email address as a string or an Array of email addresses. Any other option for :email_alerts_to is invalid and raises an error.

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mole/logger.rb', line 94

def email_appender #:nodoc:
  email_alerts_to = [ @options[:email_alerts_to] ].flatten.reject { |x| x == nil }
  raise ConfigurationError, "Invalid :email_alerts_to option [#{@options[:email_alerts_to].inspect}]" unless email_alerts_to.size > 0
  ::Logging::Appenders::Email.new("#{@log.name}-email_appender",
                                  {
                                    :level    => @options[:email_alert_level],
                                    :layout   => @layout,
                                    :from     => "#{@log.name}",
                                    :to       => "#{email_alerts_to.join(', ')}",
                                    :subject  => "Logging Alert from #{@log.name} on #{ENV['HOSTNAME']}",
                                    :server   => @options[:email_alert_server],
                                    :buffsize => @options[:email_alert_buffsize], # lines 
  })
end

#for(arg) ⇒ Object

create a new logger thi logger has no options when it is created although more can be added with the logger instance that is returned. The appenders of the current instance of Logger will be set on the new logger and the options of the current logger will be applied



113
114
115
116
117
118
119
# File 'lib/mole/logger.rb', line 113

def for( arg ) #:nodoc:
  new_logger           = ::Logging::Logger[arg]
  new_logger.level     = @options[:level]
  new_logger.additive  = @options[:additive]
  new_logger.appenders = @appenders
  return new_logger
end

#log_file_appenderObject

File appender, this is either an IO appender or a RollingFile appender depending on if an IO object or a String is passed in.

a configuration error is raised in any other circumstance



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mole/logger.rb', line 72

def log_file_appender  #:nodoc:
  appender_name = "#{@log.name}-log_file_appender"
  if String === @options[:log_file] then 
    ::Logging::Appenders::RollingFile.new( appender_name, 
                                         { :layout    => @layout,
                                           :filename  => @options[:log_file],
                                           :age       => @options[:log_file_age],
                                           :keep      => @options[:log_file_keep_count],
                                           :safe      => true  # make sure log files are rolled using lockfile
    })
  elsif @options[:log_file].respond_to?(:print) then
    ::Logging::Appenders::IO.new( appender_name, @options[:log_file],  :layout => @layout  )
  else
    raise ConfigurationError, "Invalid :log_file option [#{@options[:log_file].inspect}]"
  end
end

#log_it(context, feature, user_id, args) ⇒ Object

mole the bastard - create an entry into MOle log file



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mole/logger.rb', line 122

def log_it( context, feature, user_id, args )
  args    ||= "no args"                           
  user_id ||= "N/A"             
  ip_addr, browser_type = MoleLog.log_details( context )
  info = []
  args.keys.sort { |a,b| a.to_s <=> b.to_s }.each { |k| info << "#{k}=>#{args[k]}" }
  buff = ""
  buff << "[#{ip_addr}/#{browser_type}]--" if ip_addr and browser_type
  buff << "#{context.class.name}(#{feature}) --- #{user_id} -> #{info.join( ', ' ) }"
  self.info(  buff )
end