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.2.0"

Class Method Summary collapse

Class Method Details

.debug1(msg, *args) ⇒ Object

Log debug level 1 massages params:

msg, *args - Works to construct a string using: msg % args
Examples:
 "hello"  # no args provided
 "Time is %s. have a good day %s", Time.now, "Sir"
Note: Use ONLY %s in msg. Since d will crush for nil objects, while s will print empty string.
      Implicit to_s is used to convert arg to %s.
      If no to_s exists then ruby uses inspection.


168
169
170
171
172
173
174
175
# File 'lib/log.rb', line 168

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

.debug2(msg, *args) ⇒ Object

Log debug level 2 massages params:

msg, *args - Works to construct a string using: msg % args
Examples:
 "hello"  # no args provided
 "Time is %s. have a good day %s", Time.now, "Sir"
Note: Use ONLY %s in msg. Since d will crush for nil objects, while s will print empty string.
      Implicit to_s is used to convert arg to %s.
      If no to_s exists then ruby uses inspection.


186
187
188
189
190
191
192
193
# File 'lib/log.rb', line 186

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

.debug3(msg, *args) ⇒ Object

Log debug level 3 massages params:

msg, *args - Works to construct a string using: msg % args
Examples:
 "hello"  # no args provided
 "Time is %s. have a good day %s", Time.now, "Sir"
Note: Use ONLY %s in msg. Since d will crush for nil objects, while s will print empty string.
      Implicit to_s is used to convert arg to %s.
      If no to_s exists then ruby uses inspection.


204
205
206
207
208
209
210
211
# File 'lib/log.rb', line 204

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

.error(msg) ⇒ Object

Log error massages



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

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



214
215
216
217
218
219
220
221
# File 'lib/log.rb', line 214

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, *args) ⇒ Object

Log info massages params:

msg, *args - Works to construct a string using: msg % args
Examples:
 "hello"  # no args provided
 "Time is %s. have a good day %s", Time.now, "Sir"
Note: Use ONLY %s in msg. Since d will crush for nil objects, while s will print empty string.
      Implicit to_s is used to convert arg to %s.
      If no to_s exists then ruby uses inspection.


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

def Log.info(msg, *args)
  Log.init if @log4r.nil?
  msg = msg % args
  @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
115
116
117
118
119
120
121
# 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 to console

  if Params['print_params_to_stdout']
    Params.get_init_info_messages().each { |msg|
      puts(msg)
    }
  else
    Log.info("Not printing executable parameters to console since param:'print_params_to_stdout' is false")
  end
  #print params to log info and warning

  Params.get_init_info_messages().each { |msg|
    Log.info(msg)
  }
  Params.get_init_warning_messages().each { |msg|
    Log.warning(msg)
  }
end

.warning(msg) ⇒ Object

Log warning massages



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

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