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
43
# 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 }
  @block_destinations = Concurrent::ThreadLocalVar.new { [] }
  @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



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

def add_destination(destination)
  @destinations << destination
end

#add_group(**tags) ⇒ Object



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

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

#create_tagged_logger(**tags) ⇒ Object



109
110
111
# File 'lib/klogger/logger.rb', line 109

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

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



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

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



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

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

#pop_groupObject



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

def pop_group
  @group_set.pop
end

#remove_destination(destination) ⇒ Object



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

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

#silence!Object



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

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

#silenced?Boolean

Returns:

  • (Boolean)


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

def silenced?
  @silenced.value == true
end

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



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

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

#unsilence!Object



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

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

#with_destination(destination) ⇒ Object



102
103
104
105
106
107
# File 'lib/klogger/logger.rb', line 102

def with_destination(destination)
  @block_destinations.value << destination
  yield
ensure
  @block_destinations.value.delete(destination)
end