Class: BatchKit::Database::JavaUtilLogHandler

Inherits:
Java::JavaUtilLogging::Handler
  • Object
show all
Defined in:
lib/batch-kit/database/java_util_log_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(job_run, opts = {}) ⇒ JavaUtilLogHandler

Create a new java.util.logging handler for recording log records to the database.

Parameters:

  • job_run (JobRun)

    A JobRun object representing the job run that is to be logged.

  • opts (Hash) (defaults to: {})

    An options hash.

Options Hash (opts):

  • :max_lines (Fixnum)

    The maximium number of lines to log to the database. Default is 10,000.

  • :max_errors (Fixnum)

    The maximum number of errors to ignore before disabling further attempts to store log messages.



18
19
20
21
22
23
24
25
# File 'lib/batch-kit/database/java_util_log_handler.rb', line 18

def initialize(job_run, opts = {})
    super()
    @job_run_id = job_run.job_run_id
    @log_line = 0
    @errors = 0
    @max_lines = opts.fetch(:max_lines, 10_000)
    @max_errors = opts.fetch(:max_errors, 3)
end

Instance Method Details

#closeObject



28
29
30
# File 'lib/batch-kit/database/java_util_log_handler.rb', line 28

def close
    @job_run_id = nil
end

#flushObject



33
34
# File 'lib/batch-kit/database/java_util_log_handler.rb', line 33

def flush
end

#publish(event) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/batch-kit/database/java_util_log_handler.rb', line 37

def publish(event)
    if @job_run_id && @errors < @max_errors &&
        event.level.intValue >= Java::JavaUtilLogging::Level::FINE.intValue
        if @log_line < @max_lines || event.level >= Java::JavaUtilLogging::Level::WARNING
            msg = event.getMessage[0...1000].strip
            return unless msg.length > 0
            @log_line += 1
            log_name = (event.getLoggerName[-40..-1] || event.getLoggerName)
            level = event.level
            begin
                JobRunLog.new(job_run: @job_run_id, log_line: @log_line,
                              thread_id: event.getThreadID,
                              log_time: Time.at(event.getMillis / 1000.0), log_name: log_name,
                              log_level: level, log_message: msg).save
            rescue
                # Disable logging if an exception occurs
                @errors += 1
                raise
            end
        end
    end
end