Class: Klogger::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/klogger/logger.rb

Constant Summary collapse

LEVELS =
[:debug, :info, :warn, :error, :fatal].freeze
FORMATTERS =
{
  json: Formatters::JSON,
  simple: Formatters::Simple,
  go: Formatters::Go
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, destination: $stdout, formatter: :json, highlight: false, extra: {}) ⇒ Logger

Returns a new instance of Logger.



22
23
24
25
26
27
28
29
# File 'lib/klogger/logger.rb', line 22

def initialize(name, destination: $stdout, formatter: :json, highlight: false, extra: {})
  @name = name
  @extra = extra
  @destinations = []

  super(destination)
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
end

Instance Attribute Details

#destinationsObject (readonly)

Returns the value of attribute destinations.



12
13
14
# File 'lib/klogger/logger.rb', line 12

def destinations
  @destinations
end

#extraObject (readonly)

Returns the value of attribute extra.



13
14
15
# File 'lib/klogger/logger.rb', line 13

def extra
  @extra
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/klogger/logger.rb', line 11

def name
  @name
end

Instance Method Details

#add_destination(destination) ⇒ Object



69
70
71
# File 'lib/klogger/logger.rb', line 69

def add_destination(destination)
  @destinations << destination
end

#exception(exception, message = nil, **additional) ⇒ Object



31
32
33
34
35
36
# File 'lib/klogger/logger.rb', line 31

def exception(exception, message = nil, **additional)
  error({ message: message,
          exception: exception.class.name,
          exception_message: exception.message,
          backtrace: exception.backtrace[0, 4].join("\n") }.merge(additional))
end

#group(**additional) ⇒ Object



44
45
46
47
48
49
# File 'lib/klogger/logger.rb', line 44

def group(**additional)
  groups << additional
  yield
ensure
  groups.pop
end

#remove_destination(destination) ⇒ Object



73
74
75
# File 'lib/klogger/logger.rb', line 73

def remove_destination(destination)
  @destinations.delete(destination)
end

#silence!Object



51
52
53
54
55
56
# File 'lib/klogger/logger.rb', line 51

def silence!
  @silence = true
  yield if block_given?
ensure
  unsilence! if block_given?
end

#silenced?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/klogger/logger.rb', line 65

def silenced?
  @silence == true
end

#unsilence!Object



58
59
60
61
62
63
# File 'lib/klogger/logger.rb', line 58

def unsilence!
  @silence = false
  yield if block_given?
ensure
  silence! if block_given?
end