Created
February 19, 2023 02:40
-
-
Save packrat386/114ee63d93ef351f1171ac0871268fb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require_relative './kv_logger.rb' | |
logger = KVLogger.new(STDOUT) | |
logger.info "sup" | |
logger = logger.with_tags(tag: 'info') | |
logger.warn "with tags!" | |
logger.untagged.warn "without!" | |
logger.info "it's fine\nwith newline" | |
class ComplexThing | |
attr_accessor :foo, :bar | |
def initialize(foo, bar) | |
self.foo = foo | |
self.bar = bar | |
end | |
def to_json(*) | |
{ | |
foo: self.foo, | |
bar: self.bar, | |
}.to_json | |
end | |
end | |
logger = logger.with_tags(complex: ComplexThing.new("baz", "qux")) | |
logger.info "works with random objects!" | |
logger = logger.with_tags(random_garbage: binding) | |
logger.info "won't barf!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
require 'json' | |
class KVLogger < Logger | |
class JSONFormatter | |
attr_accessor :tags | |
def initialize(tags = {}) | |
self.tags = tags | |
end | |
def call(severity, time, progname, message) | |
JSON.generate( | |
{ | |
severity: severity, | |
time: time, | |
progname: progname, | |
message: message, | |
tags: tags | |
} | |
) + "\n" | |
end | |
end | |
def initialize(*args, **kwargs) | |
super | |
self.formatter = JSONFormatter.new | |
end | |
def with_tags(tags) | |
new_logger = self.clone | |
new_logger.formatter = JSONFormatter.new(self.formatter.tags.merge(tags)) | |
new_logger | |
end | |
def untagged | |
new_logger = self.clone | |
new_logger.formatter = JSONFormatter.new | |
new_logger | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[fg386-server] kv_logger > ./example.rb | |
{"severity":"INFO","time":"2023-02-18 20:40:07 -0600","progname":null,"message":"sup","tags":{}} | |
{"severity":"WARN","time":"2023-02-18 20:40:07 -0600","progname":null,"message":"with tags!","tags":{"tag":"info"}} | |
{"severity":"WARN","time":"2023-02-18 20:40:07 -0600","progname":null,"message":"without!","tags":{}} | |
{"severity":"INFO","time":"2023-02-18 20:40:07 -0600","progname":null,"message":"it's fine\nwith newline","tags":{"tag":"info"}} | |
{"severity":"INFO","time":"2023-02-18 20:40:07 -0600","progname":null,"message":"works with random objects!","tags":{"tag":"info","complex":{"foo":"baz","bar":"qux"}}} | |
{"severity":"INFO","time":"2023-02-18 20:40:07 -0600","progname":null,"message":"won't barf!","tags":{"tag":"info","complex":{"foo":"baz","bar":"qux"},"random_garbage":"#<Binding:0x000055955a2b2768>"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment