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.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/klogger/logger.rb', line 28

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.



18
19
20
# File 'lib/klogger/logger.rb', line 18

def destinations
  @destinations
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



19
20
21
# File 'lib/klogger/logger.rb', line 19

def tags
  @tags
end

Instance Method Details

#add_destination(destination) ⇒ Object



93
94
95
# File 'lib/klogger/logger.rb', line 93

def add_destination(destination)
  @destinations << destination
end

#add_group(**tags) ⇒ Object



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

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

#create_tagged_logger(**tags) ⇒ Object



101
102
103
# File 'lib/klogger/logger.rb', line 101

def create_tagged_logger(**tags)
  TaggedLogger.new(self, **tags)
end

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



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

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

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



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

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

#pop_groupObject



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

def pop_group
  @group_set.pop
end

#remove_destination(destination) ⇒ Object



97
98
99
# File 'lib/klogger/logger.rb', line 97

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

#silence!Object



75
76
77
78
79
80
# File 'lib/klogger/logger.rb', line 75

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

#silenced?Boolean

Returns:

  • (Boolean)


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

def silenced?
  @silenced.value == true
end

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



71
72
73
# File 'lib/klogger/logger.rb', line 71

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

#unsilence!Object



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

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