Module: Maestro::Logging

Defined in:
lib/maestro_common/logging/maestro_logging.rb

Constant Summary collapse

DEFAULT_EMAIL_CONFIG =
{
  :level => :error,
  :from => "[email protected]",
  :to => "[email protected]",
  :server => "localhost",
  :port => 25,
  #:domain => "localhost.localdomain"
  #:acct => "user",
  #:passwd => "password",
  #:authtype => :cram_md5,
  :subject => "[#{$0}] LOG NOTIFICATION", # $0 is the name of the program used in the cmdline
  :buffer_size => 1 # send emails immediately
}

Instance Method Summary collapse

Instance Method Details

#logObject



76
77
78
# File 'lib/maestro_common/logging/maestro_logging.rb', line 76

def log
  return @@log
end

#log_levelObject



84
85
86
# File 'lib/maestro_common/logging/maestro_logging.rb', line 84

def log_level
  @@log.level
end

#log_level=(level) ⇒ Object



80
81
82
# File 'lib/maestro_common/logging/maestro_logging.rb', line 80

def log_level=(level)
  @@log.level = level.is_a?(Symbol) ? level : level.downcase.to_sym
end

#log_pathObject



88
89
90
# File 'lib/maestro_common/logging/maestro_logging.rb', line 88

def log_path
  @@log_path
end

#setup_logging(log_file, log_pattern = "%d [Thread-%t] %-5l - %m\n", date_pattern = "%Y-%m-%d %H:%M:%S,%L") ⇒ Object



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
53
54
55
56
57
58
59
60
61
# File 'lib/maestro_common/logging/maestro_logging.rb', line 25

def setup_logging(log_file, log_pattern="%d [Thread-%t] %-5l - %m\n", date_pattern = "%Y-%m-%d %H:%M:%S,%L")
  @@log_path = File.dirname(log_file)
  FileUtils.mkdir_p @@log_path
  puts "Logging to #{File.expand_path(log_file)}"

  # define a rolling file log
  rolling_file = ::Logging.appenders.rolling_file(
    log_file,
    :age    => 'daily',
    :layout => ::Logging::Layouts::Pattern.new(:date_pattern => date_pattern, :pattern => log_pattern))


  # Setup a module-wide logger
  @@log = ::Logging::Logger.new(log_file)
  @@log.additive = false
  @@log.add_appenders(rolling_file)

  # logging of the 'logging' framework itself. Useful for debugging 'log by email' issues
  ::Logging::Logger[::Logging].level=:info
  ::Logging::Logger[::Logging].add_appenders(rolling_file)

  # unless in production or test
  unless (ENV['MAESTRO_ENV'] == 'production' || ENV['MAESTRO_ENV'] == 'test')

    # show "debug" or higher messages on STDOUT using the Basic layout
    stdout = ::Logging.appenders.stdout(
      :level => :debug,
      :layout => ::Logging::Layouts::Pattern.new(:date_pattern => date_pattern, :pattern => log_pattern))

    @@log.add_appenders(stdout)
    ::Logging::Logger[::Logging].add_appenders(stdout)

    # For some reason not having this line causes "warning: syswrite for buffered IO" messages
    # in the Logging gem.
    puts "Logging configured"
  end
end

#setup_logging_email(config) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/maestro_common/logging/maestro_logging.rb', line 63

def setup_logging_email(config)
  # send high priority logs via e-mail
  # options from logging https://github.com/TwP/logging/blob/master/lib/logging/appenders/email.rb
  # and net/smtp http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/smtp/rdoc/Net/SMTP.html#method-i-start

  # remove nils in config passed, change keys to be symbols instead of strings
  myconfig = config.delete_if { |k, v| v.nil? }.inject({}) { |h, (k, v)| h[k.to_sym] = v; h }
  # merge defaults with config values
  email_config = DEFAULT_EMAIL_CONFIG.merge(myconfig)
  email = ::Logging.appenders.email('email', email_config)
  @@log.add_appenders(email)
end