Class: Vx::Lib::Logger::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/vx/lib/logger/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, params = {}) ⇒ Instance

Returns a new instance of Instance.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vx/lib/logger/instance.rb', line 11

def initialize(io, params = {})
  @logstash = params.delete(:logstash)
  @params   = params

  @progname = @params.delete(:progname)

  @progname ||= begin
    pname = $PROGRAM_NAME
    pname && File.basename(pname)
  end

  @formatter = ->(_,_,_,m) { m }

  @logger           = ::Logger.new(io, 7, 50_000_000)
  @logger.formatter = @formatter
  @logger.progname  = @progname

  @logstash_logger  = ::Logger.new(logstash)
  @logstash_logger.formatter = @formatter
  @logstash_logger.progname  = @progname
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/vx/lib/logger/instance.rb', line 9

def logger
  @logger
end

#logstashObject (readonly)

Returns the value of attribute logstash.



9
10
11
# File 'lib/vx/lib/logger/instance.rb', line 9

def logstash
  @logstash
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/vx/lib/logger/instance.rb', line 9

def params
  @params
end

Instance Method Details

#handle(message, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vx/lib/logger/instance.rb', line 56

def handle(message, options = {})
  be = Time.now.to_f
  re = nil
  ex = nil

  begin
    re = yield options
  rescue Exception => e
    ex = e
  end

  en = Time.now.to_f
  options.merge!(duration: (en - be))

  if ex
    error message, options.merge!(exception: ex) if ex
    raise ex
  else
    info message, options
  end

  re
end

#levelObject



43
44
45
# File 'lib/vx/lib/logger/instance.rb', line 43

def level
  @logger.level
end

#level=(new_val) ⇒ Object



47
48
49
50
# File 'lib/vx/lib/logger/instance.rb', line 47

def level=(new_val)
  @logger.level = new_val
  @logstash_logger.level = new_val
end

#progname=(new_val) ⇒ Object



52
53
54
# File 'lib/vx/lib/logger/instance.rb', line 52

def progname=(new_val)
  @progname = new_val
end