Class: RootLogger::ClassLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/unicorn-cuba-base/root_logger.rb

Constant Summary collapse

@@levels =
[:debug, :info, :warn, :error, :fatal, :unknown]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_logger, class_obj) ⇒ ClassLogger

Returns a new instance of ClassLogger.



8
9
10
11
# File 'lib/unicorn-cuba-base/root_logger.rb', line 8

def initialize(root_logger, class_obj)
  @root_logger = root_logger
  @class_name = class_obj.name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/unicorn-cuba-base/root_logger.rb', line 17

def method_missing(name, *args, &block)
  if @@levels.include? name
    root_logger = @root_logger.with_meta('className' => @class_name)

    if block
      root_logger.send(name, &block)
    else
      message = args.first

      if args.last.is_a? Exception
        error = args.last
        root_logger = root_logger.with_meta('exceptionClass' => error.class.name)
        message = "#{message}: #{error.class.name}: #{error.message}\n#{error.backtrace.join("\n")}"
      end

      # log with class name
      root_logger.send(name, message.chomp, &block)
    end
  else
    # forward to root logger
    @root_logger.send(name, *args, &block)
  end
end

Instance Attribute Details

#root_loggerObject (readonly)

Returns the value of attribute root_logger.



41
42
43
# File 'lib/unicorn-cuba-base/root_logger.rb', line 41

def root_logger
  @root_logger
end

Instance Method Details

#inspectObject



43
44
45
# File 'lib/unicorn-cuba-base/root_logger.rb', line 43

def inspect
  "#<ClassLogger[#{@progname}] #{"0x%X" % object_id} root_logger=#{@root_logger.inspect}>"
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/unicorn-cuba-base/root_logger.rb', line 13

def respond_to?(method)
  super or @root_logger.respond_to? method
end