Module: Log

Defined in:
lib/log.rb,
lib/log/version.rb

Overview

Module: Log. Abstruct: The Log is used to log infowarningerrordebug messages

This module is actually a wrapper to log4r gem and serves
as a central code to use the log utility.

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.debug1(msg) ⇒ Object

Log debug level 1 massages



144
145
146
147
148
149
150
# File 'lib/log.rb', line 144

def Log.debug1(msg)
  if Params['log_debug_level'] >= 1
    Log.init if @log4r.nil?
    @log4r.debug(msg_with_caller(msg))
    Log.flush if Params['log_flush_each_message']
  end
end

.debug2(msg) ⇒ Object

Log debug level 2 massages



153
154
155
156
157
158
159
# File 'lib/log.rb', line 153

def Log.debug2(msg)
  if Params['log_debug_level'] >= 2
    Log.init if @log4r.nil?
    @log4r.debug(msg_with_caller(msg))
    Log.flush if Params['log_flush_each_message']
  end
end

.debug3(msg) ⇒ Object

Log debug level 3 massages



162
163
164
165
166
167
168
# File 'lib/log.rb', line 162

def Log.debug3(msg)
  if Params['log_debug_level'] >= 3
    Log.init if @log4r.nil?
    @log4r.debug(msg_with_caller(msg))
    Log.flush if Params['log_flush_each_message']
  end
end

.error(msg) ⇒ Object

Log error massages



130
131
132
133
134
# File 'lib/log.rb', line 130

def Log.error(msg)
  Log.init if @log4r.nil?
  @log4r.error(msg_with_caller(msg))
  Log.flush if Params['log_flush_each_message']
end

.executable_nameObject

Auxiliary method to retrieve the executable name



18
19
20
21
# File 'lib/log.rb', line 18

def Log.executable_name
  /([a-zA-Z0-9\-_\.]+):\d+/ =~ caller[caller.size-1]
  return $1
end

.flushObject

Flush email log



171
172
173
174
175
176
177
178
# File 'lib/log.rb', line 171

def Log.flush()
  return if @log4r.nil?
  @log4r.outputters.each { |o|
    # Not flushing to email since this will cause empty emails to be sent
    # Email is already configured to immediately send mail on ERROR|FATAL messages.
    o.flush unless o.is_a?(Log4r::EmailOutputter)
  }
end

.info(msg) ⇒ Object

Log info massages



137
138
139
140
141
# File 'lib/log.rb', line 137

def Log.info(msg)
  Log.init if @log4r.nil?
  @log4r.info(msg_with_caller(msg))
  Log.flush if Params['log_flush_each_message']
end

.initObject

Should be called from executable right after params handling. Init Log level and set output to file,stdout and email according to configuration params.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/log.rb', line 44

def Log.init
  @log4r = Log4r::Logger.new 'BBFS log'
  @log4r.trace = true

  #levels setup
  log4r_level = Log4r::DEBUG
  log4r_level = Log4r::INFO if 0 == Params['log_debug_level']

  #formatters
  formatter = Log4r::PatternFormatter.new(:pattern => "[%l] [%d] [%m]")

  #stdout setup
  if Params['log_write_to_console']
    stdout_outputter = Log4r::Outputter.stdout
    stdout_outputter.formatter = formatter
    stdout_outputter.level = log4r_level
    @log4r.outputters << stdout_outputter
  end

  #file setup
  if Params['log_write_to_file']
    if File.exist?(Params['log_file_name'])
      File.delete(Params['log_file_name'])
    else
      dir_name = File.dirname(Params['log_file_name'])
      FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)
    end
    file_config = {
        "filename" => Params['log_file_name'],
        "maxsize" => Params['log_rotation_size'],
        "trunc" => true
    }
    file_outputter = Log4r::RollingFileOutputter.new("file_log", file_config)
    file_outputter.level = log4r_level
    file_outputter.formatter = formatter
    @log4r.outputters << file_outputter
  end

  #email setup
  if Params['log_write_to_email']
    email_outputter = Log4r::EmailOutputter.new('email_log',
                                                :server => 'smtp.gmail.com',
                                                :port => 587,
                                                :subject => "Error happened at server:'#{Params['local_server_name']}' run by #{ENV['USER']}. Service_name is #{Params['service_name']}",
                                                :acct => Params['from_email'],
                                                :from => Params['from_email'],
                                                :passwd => Params['from_email_password'],
                                                :to => Params['to_email'],
                                                :immediate_at => 'FATAL,ERROR',
                                                :authtype => :plain,
                                                :tls => true,
                                                :formatfirst => true,
                                                :buffsize => 9999,
    )
    email_outputter.level = Log4r::ERROR
    email_outputter.formatter = formatter
    @log4r.outputters << email_outputter
  end

  # Write init message and user parameters
  @log4r.info('BBFS Log initialized.') # log first data

  # print params
  if Params['print_params_to_stdout']
    Params.get_init_messages().each { |msg|
      Log.info(msg)
    }
  else
    Log.info("Not printing executable parameters since param:'print_params_to_stdout' is false")
  end
end

.warning(msg) ⇒ Object

Log warning massages



123
124
125
126
127
# File 'lib/log.rb', line 123

def Log.warning(msg)
  Log.init if @log4r.nil?
  @log4r.warn(msg_with_caller(msg))
  Log.flush if Params['log_flush_each_message']
end