Class: StructuredLogger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logger::Severity
Defined in:
lib/structured_logger.rb,
lib/structured_logger/version.rb

Defined Under Namespace

Classes: ArgumentFormatter

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ StructuredLogger

Returns a new instance of StructuredLogger.



12
13
14
15
16
17
18
# File 'lib/structured_logger.rb', line 12

def initialize(io)
  @logger = Logger.new(io)
  @progname = nil
  @formatter = nil
  @default_formatter = Logger::Formatter.new
  @argument_formatter = ArgumentFormatter.new
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



10
11
12
# File 'lib/structured_logger.rb', line 10

def formatter
  @formatter
end

#prognameObject

Returns the value of attribute progname.



9
10
11
# File 'lib/structured_logger.rb', line 9

def progname
  @progname
end

Instance Method Details

#add(severity, *args, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/structured_logger.rb', line 47

def add(severity, *args, &block)
  if level > severity
    return
  end
  block_result = block_given? ? yield : nil
  s_severity = format_severity(severity)
  time = Time.now
  message = @argument_formatter.call(severity: s_severity,
                                     time: time,
                                     progname: @progname,
                                     args: args,
                                     block_result: block_result)
  s = (@formatter || @default_formatter).call(s_severity, time, @progname,
                                              message)
  @logger << s
end

#debug(*args, &block) ⇒ Object



27
28
29
# File 'lib/structured_logger.rb', line 27

def debug(*args, &block)
  add(DEBUG, *args, &block)
end

#error(*args, &block) ⇒ Object



39
40
41
# File 'lib/structured_logger.rb', line 39

def error(*args, &block)
  add(ERROR, *args, &block)
end

#fatal(*args, &block) ⇒ Object



43
44
45
# File 'lib/structured_logger.rb', line 43

def fatal(*args, &block)
  add(FATAL, *args, &block)
end

#info(*args, &block) ⇒ Object



31
32
33
# File 'lib/structured_logger.rb', line 31

def info(*args, &block)
  add(INFO, *args, &block)
end

#warn(*args, &block) ⇒ Object



35
36
37
# File 'lib/structured_logger.rb', line 35

def warn(*args, &block)
  add(WARN, *args, &block)
end