Class: Klogger::Logger
- Inherits:
-
Logger
- Object
- Logger
- Klogger::Logger
- 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
-
#destinations ⇒ Object
readonly
Returns the value of attribute destinations.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #add_destination(destination) ⇒ Object
- #add_group(**tags) ⇒ Object
- #create_tagged_logger(**tags) ⇒ Object
- #exception(exception, message = nil, **tags) ⇒ Object
- #group(**tags, &block) ⇒ Object
-
#initialize(name = nil, destination: $stdout, formatter: :go, highlight: false, include_group_ids: false, tags: {}) ⇒ Logger
constructor
A new instance of Logger.
- #pop_group ⇒ Object
- #remove_destination(destination) ⇒ Object
- #silence! ⇒ Object
- #silenced? ⇒ Boolean
- #tagged(**tags, &block) ⇒ Object
- #unsilence! ⇒ Object
- #with_destination(destination) ⇒ Object
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 = @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
#destinations ⇒ Object (readonly)
Returns the value of attribute destinations.
18 19 20 |
# File 'lib/klogger/logger.rb', line 18 def destinations @destinations end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/klogger/logger.rb', line 17 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
19 20 21 |
# File 'lib/klogger/logger.rb', line 19 def @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(**) @group_set.add(**) end |
#create_tagged_logger(**tags) ⇒ Object
109 110 111 |
# File 'lib/klogger/logger.rb', line 109 def create_tagged_logger(**) TaggedLogger.new(self, **) 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, = nil, **) error( message: , exception: exception.class.name, exception_message: exception., backtrace: exception.backtrace[0, 4].join("\n"), ** ) end |
#group(**tags, &block) ⇒ Object
60 61 62 |
# File 'lib/klogger/logger.rb', line 60 def group(**, &block) @group_set.call(**, &block) end |
#pop_group ⇒ Object
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
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(**, &block) @group_set.call_without_id(**, &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 |