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.



10
11
12
13
14
# File 'lib/vx/lib/logger/instance.rb', line 10

def initialize(io, params = {})
  @params           = params
  @logger           = ::Logger.new(io, 7, 50_000_000)
  @logger.formatter = get_formatter(params[:format])
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#get_formatter(name) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/vx/lib/logger/instance.rb', line 34

def get_formatter(name)
  case name
  when :json
    JsonFormatter.new
  else
    JsonFormatter.new
  end
end

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vx/lib/logger/instance.rb', line 43

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



22
23
24
# File 'lib/vx/lib/logger/instance.rb', line 22

def level
  @logger.level
end

#level=(new_val) ⇒ Object



26
27
28
# File 'lib/vx/lib/logger/instance.rb', line 26

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

#progname=(new_val) ⇒ Object



30
31
32
# File 'lib/vx/lib/logger/instance.rb', line 30

def progname=(new_val)
  params[:progname] = new_val
end