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 = nil, destination: $stdout, formatter: :go, highlight: false, include_group_ids: false, tags: {}) ⇒ Logger

Returns a new instance of Logger.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/klogger/logger.rb', line 25

def initialize(name = nil, destination: $stdout, formatter: :go, highlight: false, include_group_ids: false,
               tags: {})
  @name = name
  @tags = tags
  @destinations = []
  @group_set = GroupSet.new
  @silenced = Concurrent::ThreadLocalVar.new { false }
  @include_group_ids = include_group_ids
  super(destination)
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
end

Instance Attribute Details

#destinationsObject (readonly)

Returns the value of attribute destinations.



15
16
17
# File 'lib/klogger/logger.rb', line 15

def destinations
  @destinations
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



16
17
18
# File 'lib/klogger/logger.rb', line 16

def tags
  @tags
end

Instance Method Details

#add_destination(destination) ⇒ Object



84
85
86
# File 'lib/klogger/logger.rb', line 84

def add_destination(destination)
  @destinations << destination
end

#add_group(**tags) ⇒ Object



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

def add_group(**tags)
  @group_set.add(**tags)
end

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



37
38
39
40
41
42
# File 'lib/klogger/logger.rb', line 37

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

#group(**tags, &block) ⇒ Object



50
51
52
# File 'lib/klogger/logger.rb', line 50

def group(**tags, &block)
  @group_set.call(**tags, &block)
end

#pop_groupObject



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

def pop_group
  @group_set.pop
end

#remove_destination(destination) ⇒ Object



88
89
90
# File 'lib/klogger/logger.rb', line 88

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

#silence!Object



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

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

#silenced?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/klogger/logger.rb', line 80

def silenced?
  @silenced.value == true
end

#tagged(**tags, &block) ⇒ Object



62
63
64
# File 'lib/klogger/logger.rb', line 62

def tagged(**tags, &block)
  @group_set.call_without_id(**tags, &block)
end

#unsilence!Object



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

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