-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SemanticLogger appender, increase doc cov, and check doc cov
- Loading branch information
1 parent
a3cb022
commit 3e9746a
Showing
10 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
require "bundler/setup" | ||
require "lamian" | ||
require "semantic_logger" | ||
|
||
require "pry" | ||
Pry.start |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Lamian | ||
# Custom appender for the `semantic_logger` library. | ||
# This appender adds all logs to the current Lamian logger. | ||
# Since Lamian stores logs in a thread variable, | ||
# it is necessary that this appender writes logs synchronously: | ||
# just call `SematicLogger.sync!` somewhere in the initialization process. | ||
# @see https://logger.rocketjob.io Semantic Logger documentation. | ||
class SemanticLoggerAppender < SemanticLogger::Subscriber | ||
# Mapping between standard Logger severity and SemanticLogger log levels. | ||
LOGGER_LEVELS_MAPPING = { trace: 0, debug: 0, info: 1, warn: 2, error: 3, fatal: 4 }.freeze | ||
|
||
# The method to be implemented when creating a custom appender. | ||
# @see https://logger.rocketjob.io/custom_appenders.html Documentation about custom appenders. | ||
# @returns [Boolean] | ||
def log(log) | ||
mapped_level = LOGGER_LEVELS_MAPPING[log.level] || ::Logger::UNKNOWN | ||
Lamian::Logger.current.add(mapped_level, log.message) | ||
|
||
true | ||
end | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Lamian::SemanticLoggerAppender, :cool_loggers do | ||
subject(:appender) { described_class.new } | ||
|
||
let(:cool_formatter) do | ||
-> (severity, _date, _progname, message) { "[#{severity}] #{message}\n" } | ||
end | ||
|
||
let(:log) do | ||
instance_double(SemanticLogger::Log).tap do |instance| | ||
allow(instance).to receive(:message).and_return("some message") | ||
allow(instance).to receive(:level).and_return(log_level) | ||
end | ||
end | ||
let(:log_level) { :debug } | ||
|
||
it "properly adds logs to Lamian" do | ||
Lamian.run do | ||
expect(appender.log(log)).to eq(true) | ||
expect(Lamian.dump).to eq("[DEBUG] some message\n") | ||
end | ||
end | ||
|
||
context "with unknown log level" do | ||
let(:log_level) { :custom_level } | ||
|
||
it "uses UNKNOWN severity" do | ||
Lamian.run do | ||
expect(appender.log(log)).to eq(true) | ||
expect(Lamian.dump).to eq("[ANY] some message\n") | ||
end | ||
end | ||
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