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
- #exception(exception, message = nil, **tags) ⇒ Object
- #group(**tags) ⇒ Object
-
#initialize(name, destination: $stdout, formatter: :json, highlight: false, tags: {}) ⇒ Logger
constructor
A new instance of Logger.
- #remove_destination(destination) ⇒ Object
- #silence! ⇒ Object
- #silenced? ⇒ Boolean
- #unsilence! ⇒ Object
Constructor Details
#initialize(name, destination: $stdout, formatter: :json, highlight: false, tags: {}) ⇒ Logger
23 24 25 26 27 28 29 30 31 |
# File 'lib/klogger/logger.rb', line 23 def initialize(name, destination: $stdout, formatter: :json, highlight: false, tags: {}) @name = name = @destinations = [] @groups = Concurrent::ThreadLocalVar.new([]) super(destination) self.formatter = FORMATTERS[formatter].new(highlight: highlight) end |
Instance Attribute Details
#destinations ⇒ Object (readonly)
Returns the value of attribute destinations.
13 14 15 |
# File 'lib/klogger/logger.rb', line 13 def destinations @destinations end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/klogger/logger.rb', line 12 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
14 15 16 |
# File 'lib/klogger/logger.rb', line 14 def end |
Instance Method Details
#add_destination(destination) ⇒ Object
71 72 73 |
# File 'lib/klogger/logger.rb', line 71 def add_destination(destination) @destinations << destination end |
#exception(exception, message = nil, **tags) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/klogger/logger.rb', line 33 def exception(exception, = nil, **) error({ message: , exception: exception.class.name, exception_message: exception., backtrace: exception.backtrace[0, 4].join("\n") }.merge()) end |
#group(**tags) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/klogger/logger.rb', line 46 def group(**) @groups.value += [] yield ensure @groups.value.pop end |
#remove_destination(destination) ⇒ Object
75 76 77 |
# File 'lib/klogger/logger.rb', line 75 def remove_destination(destination) @destinations.delete(destination) end |
#silence! ⇒ Object
53 54 55 56 57 58 |
# File 'lib/klogger/logger.rb', line 53 def silence! @silence = true yield if block_given? ensure unsilence! if block_given? end |
#silenced? ⇒ Boolean
67 68 69 |
# File 'lib/klogger/logger.rb', line 67 def silenced? @silence == true end |
#unsilence! ⇒ Object
60 61 62 63 64 65 |
# File 'lib/klogger/logger.rb', line 60 def unsilence! @silence = false yield if block_given? ensure silence! if block_given? end |