Class: Ziya::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ziya/utils/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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ziya/utils/logger.rb', line 56

def initialize( opts = {} )
  @options = ::Ziya::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.



12
13
14
# File 'lib/ziya/utils/logger.rb', line 12

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.



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
49
50
51
52
# File 'lib/ziya/utils/logger.rb', line 24

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          => 'ZiYa'                         ,
    :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:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ziya/utils/logger.rb', line 98

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



117
118
119
120
121
122
123
# File 'lib/ziya/utils/logger.rb', line 117

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ziya/utils/logger.rb', line 76

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