Class: RBatch::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/rbatch/log.rb

Constant Summary

@@FORMATTER =
proc do |severity, datetime, progname, msg|
  head = "[#{datetime}] [" + sprintf("%-5s",severity) +"]"
  if msg.is_a? Exception
    "#{head} #{msg}\n" + msg.backtrace.map{|s| "    [backtrace] #{s}"}.join("\n") + "\n"
  else
    "#{head} #{msg}\n"
  end
end
@@STDOUT_FORMATTER =
proc do |severity, datetime, progname, msg|
  head = "[" + sprintf("%-5s",severity) +"]"
  if msg.is_a? Exception
    "#{head} #{msg}\n" + msg.backtrace.map{|s| "    [backtrace] #{s}"}.join("\n") + "\n"
  else
    "#{head} #{msg}\n"
  end
end
@@LOG_LEVEL_MAP =
{
  "debug" => Logger::DEBUG,
  "info"  => Logger::INFO,
  "warn"  => Logger::WARN,
  "error" => Logger::ERROR,
  "fatal" => Logger::FATAL
}

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RBatch::Log) initialize(opt = nil) {|log| ... }

External command wrapper

Examples:

require 'rbatch'
RBatch::Log.new{ |log|
  log.info "info string"
  log.error "error string"
  raise "exception" # => rescued in this block
}

use option

require 'rbatch'
RBatch::Log.new({:name => "hoge.log"}){ |log|
  log.info "info string"
}

Parameters:

  • opt (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (opt):

  • :dir (String)

    Output directory

  • :name (String)

    Log file name. Default is “_

  • :append (Boolean)
  • :level (String)

    Effective values are “debug”,“info”,“wran”,“error”,and “fatal”.

  • :stdout (Boolean)

    Print log string both log file and STDOUT

  • :delete_old_log (Boolean)

    If this is true, delete old log files when this is called. If log filename does not include “”, do nothing.

  • :delete_old_log_date (Integer)
  • :output_exit_status (Boolean)

    When you use the “exist” method in a log block, output exit status into the log file.

  • :send_mail (Boolean)

    When log.error(str) is called, log.fatal(str) is called , or rescue an Exception, send e-mail.

  • :bufferd (Boolean)

    If true, log output is bufferd. Default is false.

  • :mail_to (String)
  • :mail_from (String)
  • :mail_server_host (String)
  • :mail_server_port (Integer)

Yields:

  • (log)

    RBatch::Log instance

Raises:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbatch/log.rb', line 118

def initialize(opt=nil)
  @opt = opt
  @vars = @@def_vars.clone
  if ! opt.nil?
    # change opt key from "hoge" to "log_hoge"
    tmp = {}
    opt.each_key do |key|
      tmp[("log_" + key.to_s).to_sym] = opt[key]
    end
    @vars.merge!(tmp)
  end
  @path = File.join(@vars[:log_dir],@vars[:log_name])
  unless Dir.exist? @vars[:log_dir]
    raise LogException,"Log directory \"#{@vars[:log_dir]}\" does not exist"
  end
  # create Logger instance
  begin
    if @vars[:log_append] && File.exist?(@path)
      _io = open(@path,"a")
    else
      _io = open(@path,"w")
    end
    _io.sync = ! @vars[:log_bufferd]
    @log = Logger.new(_io)
  rescue Errno::ENOENT => e
    raise LogException,"Can not open log file  - #{@path}"
  end
  # set logger option
  @log.level = @@LOG_LEVEL_MAP[@vars[:log_level]]
  @log.formatter = @@FORMATTER
  if @vars[:log_stdout]
    # ccreate Logger instance for STDOUT
    @stdout_log = Logger.new(STDOUT)
    @stdout_log.level = @@LOG_LEVEL_MAP[@vars[:log_level]]
    @stdout_log.formatter = @@STDOUT_FORMATTER
  end
  # delete old log
  delete_old_log(@vars[:log_delete_old_log_date]) if @vars[:log_delete_old_log]
  # Start logging
  @@journal.put 1,"Logging Start: \"#{@path}\""
  @@journal.add_log(self)
  if block_given?
    begin
      yield self
    rescue SystemExit => e
      if @vars[:log_output_exit_status]
        if e.status == 0
          info("RBatch catch SystemExit. Exit with status " + e.status.to_s)
        else
          fatal(e)
          fatal("RBatch catch SystemExit. Exit with status " + e.status.to_s)
        end
      end
      exit e.status
    rescue Exception => e
      fatal(e)
      fatal("RBatch catch an exception. Exit with status 1")
      exit 1
    ensure
      close
    end
  end
end

Class Method Details

+ (RBatch::Variables) def_vars

Returns:



50
# File 'lib/rbatch/log.rb', line 50

def Log.def_vars    ; @@def_vars ; end

+ (Object) def_vars=(v)

Parameters:

Raises:

  • (ArgumentError)


43
44
45
46
# File 'lib/rbatch/log.rb', line 43

def Log.def_vars=(v)
  raise ArgumentError, "type mismatch: #{v} for #RBatch::Variables" if ! v.kind_of?(RBatch::Variables)
  @@def_vars=v
end

+ (Object) journal=(j)

Parameters:



57
# File 'lib/rbatch/log.rb', line 57

def Log.journal=(j) ; @@journal=j ; end

Instance Method Details

- (Object) debug(str)

Out put log with DEBUG level

Parameters:

  • str (String)

    log string



214
215
216
217
# File 'lib/rbatch/log.rb', line 214

def debug(str)
  @stdout_log.debug(str) if @vars[:log_stdout]
  @log.debug(str)
end

- (Object) error(str)

Out put log with ERROR level

Parameters:

  • str (String)

    log string



192
193
194
195
196
# File 'lib/rbatch/log.rb', line 192

def error(str)
  @stdout_log.error(str) if @vars[:log_stdout]
  @log.error(str)
  send_mail(str) if @vars[:log_send_mail]
end

- (Object) fatal(str)

Out put log with ERROR level

Parameters:

  • str (String)

    log string



184
185
186
187
188
# File 'lib/rbatch/log.rb', line 184

def fatal(str)
  @stdout_log.fatal(str) if @vars[:log_stdout]
  @log.fatal(str)
  send_mail(str) if @vars[:log_send_mail]
end

- (Object) info(str)

Out put log with INFO level

Parameters:

  • str (String)

    log string



207
208
209
210
# File 'lib/rbatch/log.rb', line 207

def info(str)
  @stdout_log.info(str) if @vars[:log_stdout]
  @log.info(str)
end

- (Object) journal(str)



220
221
222
# File 'lib/rbatch/log.rb', line 220

def journal(str)
  @log.info("[RBatch] " + str)
end

- (Object) warn(str)

Out put log with WARN level

Parameters:

  • str (String)

    log string



200
201
202
203
# File 'lib/rbatch/log.rb', line 200

def warn(str)
  @stdout_log.warn(str) if @vars[:log_stdout]
  @log.warn(str)
end