Skip to content

Commit

Permalink
feat: tagged loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Mar 1, 2024
1 parent 20c14d5 commit eb16561
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Metrics/MethodLength:
Max: 14

Metrics/ClassLength:
Max: 120
Max: 140

Style/SymbolArray:
EnforcedStyle: brackets
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Klogger.group(ip: '1.2.3.4') do
end

# If you can't use a block you can manually open and close a group but you'll need to be sure to close it
#when you're finished.
# when you're finished.
group_id = Klogger.global_groups.add(ip: '1.2.3.4')
# ... do anything that you want - everything will be tagged as appropriate
Klogger.global_groups.pop
Expand All @@ -149,6 +149,16 @@ logger.tagged(name: 'steve') do
end
```

## Tagged Loggers

If you wish to apply tags to a series of log entries but you don't wish to use blocks, you can create a "sub" logger which will always include those tags for all messages sent to it.

```ruby
logger = Klogger.new(:logger)
tagged_logger = logger.create_tagged_logger(tag: 'my-tag')
tagged_logger.info "Hello world!" # => will be tagged with tag=my-tag
```

### Silencing

Sometimes you don't want to log for a little while. You can use the `silence` method to temporarily disable logging.
Expand Down
1 change: 1 addition & 0 deletions lib/klogger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'klogger/logger'
require 'klogger/tagged_logger'

module Klogger

Expand Down
20 changes: 13 additions & 7 deletions lib/klogger/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class Logger < ::Logger
go: Formatters::Go
}.freeze

def initialize(name = nil, destination: $stdout, formatter: :go, highlight: false, include_group_ids: false,
def initialize(name = nil,
destination: $stdout,
formatter: :go,
highlight: false,
include_group_ids: false,
tags: {})
@name = name
@tags = tags
Expand All @@ -39,12 +43,10 @@ def initialize(name = nil, destination: $stdout, formatter: :go, highlight: fals

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)
message: message,
exception: exception.class.name,
exception_message: exception.message,
backtrace: exception.backtrace[0, 4].join("\n"), **tags
)
end

Expand Down Expand Up @@ -96,6 +98,10 @@ def remove_destination(destination)
@destinations.delete(destination)
end

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

private

def add(severity, message = nil, progname = nil, **tags, &block)
Expand Down
56 changes: 56 additions & 0 deletions lib/klogger/tagged_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require 'klogger/logger'

module Klogger
class TaggedLogger

def initialize(parent, **tags)
@parent = parent
@tags = tags
end

Klogger::Logger::LEVELS.each do |level|
define_method(level) do |message = nil, progname = nil, **tags, &block|
@parent.public_send(level, message, progname, **@tags.merge(tags), &block)
end
end

def exception(exception, message = nil, **tags)
@parent.exception(exception, message, **@tags.merge(tags))
end

def group(**tags, &block)
@parent.group(**@tags.merge(tags), &block)
end

def add_group(**tags)
@parent.add_group(**@tags.merge(tags))
end

def pop_group
@parent.pop_group
end

def tagged(**tags, &block)
@parent.tagged(**@tags.merge(tags), &block)
end

def silence!(&block)
@parent.silence!(&block)
end

def unsilence!(&block)
@parent.unsilence!(&block)
end

def silenced?
@parent.silenced?
end

def create_tagged_logger(**tags)
@parent.create_tagged_logger(**@tags.merge(tags))
end

end
end
8 changes: 8 additions & 0 deletions spec/specs/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ module Klogger
end
end

describe '#create_tagged_logger' do
subject(:logger) { described_class.new('example', destination: output, formatter: :json) }

it 'returns a new tagged logger' do
expect(logger.create_tagged_logger(tag: 'tag1')).to be_a Klogger::TaggedLogger
end
end

describe '#add_destination' do
subject(:logger) { described_class.new('example', destination: output) }

Expand Down
52 changes: 52 additions & 0 deletions spec/specs/tagged_logger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require 'spec_helper'
require 'klogger'
require 'klogger/tagged_logger'

module Klogger

RSpec.describe TaggedLogger do
before { Timecop.freeze }
after { Timecop.return }

let!(:output) { StringIO.new }
let(:logger) { Logger.new('example', destination: output, formatter: :json) }

subject(:tagged_logger) { described_class.new(logger, tag1: 'test') }

Logger::LEVELS.each do |level|
describe "##{level}" do
it 'logs with the parent classes' do
tagged_logger.public_send(level, 'Hello', tag2: 'test')
expect(output.string).to eq({ time: Time.now.to_s, severity: level,
logger: 'example', message: 'Hello',
tag1: 'test', tag2: 'test' }.to_json + "\n")
end
end
end

describe '#group' do
it 'logs appropriately' do
tagged_logger.group(grouptag: 'gt1') do
tagged_logger.info 'Hello', tag2: 'test'
end
expect(output.string).to eq({ time: Time.now.to_s, severity: 'info',
logger: 'example', message: 'Hello',
tag1: 'test', tag2: 'test', grouptag: 'gt1' }.to_json + "\n")
end
end

describe '#tagged' do
it 'logs appropriately' do
tagged_logger.tagged(taggedtag: 'gt1') do
tagged_logger.info 'Hello', tag2: 'test'
end
expect(output.string).to eq({ time: Time.now.to_s, severity: 'info',
logger: 'example', message: 'Hello',
tag1: 'test', tag2: 'test', taggedtag: 'gt1' }.to_json + "\n")
end
end
end

end

0 comments on commit eb16561

Please sign in to comment.