Class: BatchKit::LogManager

Inherits:
Object
  • Object
show all
Defined in:
lib/batch-kit/logging.rb

Overview

Used for setting the log framework to use, and retrieving a logger from the current framework.

Class Method Summary collapse

Class Method Details

.configure(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/batch-kit/logging.rb', line 44

def configure(options = {})
    self.log_framework = options[:log_framework] if options[:log_framework]
    if options.fetch(:log_color, true)
        case self.log_framework
        when :log4r
            require 'color_console/log4r_logger'
            Console.replace_console_logger(logger: 'batch')
        when :java_util_logging
            require 'color_console/java_util_logger'
            Console.replace_console_logger(
                level: Java::JavaUtilLogging::Level::FINE,
                level_labels: {
                    Java::JavaUtilLogging::Level::FINE => 'DETAIL',
                    Java::JavaUtilLogging::Level::FINER => 'TRACE'
                })
        else
            require 'color_console'
        end
    end
end

.levelObject

Returns the current root log level



102
103
104
# File 'lib/batch-kit/logging.rb', line 102

def level
    logger.level
end

.level=(level) ⇒ Object

Sets the log level



108
109
110
111
112
113
114
115
116
# File 'lib/batch-kit/logging.rb', line 108

def level=(level)
    case log_framework
    when :log4r
        lvl = Log4r::LNAMES.index(level.to_s.upcase)
        Log4r::Logger.each_logger{ |l| l.level = lvl }
    else
        logger.level = level
    end
end

.log_frameworkObject

Returns a symbol identifying which logging framework is being used.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/batch-kit/logging.rb', line 67

def log_framework
    unless @log_framework
        if RUBY_PLATFORM == 'java'
            LogManager.log_framework = :java_util_logging
        else
            begin
                require 'log4r'
                LogManager.log_framework = :log4r
            rescue LoadError
                LogManager.log_framework = :stdout
            end
        end
    end
    @log_framework
end

.log_framework=(framework) ⇒ Object

Sets the logging framework



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/batch-kit/logging.rb', line 85

def log_framework=(framework)
    unless Logging::FRAMEWORKS.include?(framework)
        raise ArgumentError, "Unknown logging framework #{framework.inspect}"
    end
    if @log_framework
        lvl = self.level
    end
    @log_framework = framework
    if init_proc = Logging::FRAMEWORK_INIT[@log_framework]
        init_proc.call
    end
    self.level = lvl if lvl
    logger.trace "Log framework is #{@log_framework}"
end

.logger(name = nil) ⇒ Logger

Returns a logger with a given name, which must be under the ‘batch’ namespace. If name is omitted, the logger is named ‘batch’. If a name is specified that is not under ‘batch’, then it is prepended with ‘batch’.

Returns:

  • (Logger)

    a logger object that can be used for generating log messages. The type of logger returned will depend on the log framework being used, but the logger is guaranteed to implement the following log methods:

    • error

    • warning

    • info

    • config

    • detail

    • trace

    • debug



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/batch-kit/logging.rb', line 135

def logger(name = nil)
    case name
    when NilClass, ''
        name = 'batch'
    when /^batch/
    when /\./
    when String
        name = "batch.#{name}"
    end
    case log_framework
    when :stdout
        BatchKit::Logging::StdOutLogger.logger(name)
    when :java_util_logging
        BatchKit::Logging::JavaLogFacade.new(Java::JavaUtilLogging::Logger.getLogger(name))
    when :log4r
        log4r_name = name.gsub('.', '::')
        BatchKit::Logging::Log4rFacade.new(Log4r::Logger[log4r_name] ||
                                        Log4r::Logger.new(log4r_name))
    else BatchKit::Logging::NullLogger.instance
    end
end