Skip to content

Commit

Permalink
Add SemanticLogger appender, increase doc cov, and check doc cov
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherRegularDude committed Sep 30, 2021
1 parent a3cb022 commit 3e9746a
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
run: bundle exec ci-helper RunSpecs
- name: Audit
run: bundle exec ci-helper BundlerAudit
- name: Documentation coverage
run: bundle exec rake doc:coverage
- name: Coveralls
uses: coverallsapp/github-action@master
with:
Expand Down
13 changes: 7 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lamian (1.3.0)
lamian (1.4.0)
rails (>= 4.2)

GEM
Expand Down Expand Up @@ -115,15 +115,13 @@ GEM
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.1)
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.1)
minitest (5.14.4)
multipart-post (2.1.1)
nio4r (2.5.8)
nokogiri (1.12.4-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.12.4-x86_64-linux)
nokogiri (1.12.5-x86_64-linux)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.2.0)
Expand Down Expand Up @@ -213,6 +211,8 @@ GEM
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
semantic_logger (4.8.2)
concurrent-ruby (~> 1.0)
sentry-raven (3.1.2)
faraday (>= 1.0)
sentry-ruby (4.7.1)
Expand Down Expand Up @@ -265,11 +265,12 @@ DEPENDENCIES
rake
rspec
rubocop-config-umbrellio
semantic_logger
sentry-raven
sentry-ruby
simplecov
simplecov-lcov
yard

BUNDLED WITH
2.2.27
2.2.28
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "bundler/setup"
require "lamian"
require "semantic_logger"

require "pry"
Pry.start
1 change: 1 addition & 0 deletions lamian.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rubocop-config-umbrellio"
spec.add_development_dependency "semantic_logger"
spec.add_development_dependency "sentry-raven"
spec.add_development_dependency "sentry-ruby"
spec.add_development_dependency "simplecov"
Expand Down
2 changes: 2 additions & 0 deletions lib/lamian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module Lamian
autoload :RavenContextExtension, "lamian/raven_context_extension"
autoload :SidekiqRavenMiddleware, "lamian/sidekiq_raven_middleware"
autoload :SidekiqSentryMiddleware, "lamian/sidekiq_sentry_middleware"
autoload :SemanticLoggerAppender, "lamian/semantic_logger_appender"

# The key under which logs are stored in the Sentry extra data.
SENTRY_EXTRA_KEY = :lamian_log

require "lamian/engine"
Expand Down
10 changes: 10 additions & 0 deletions lib/lamian/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@ class Engine < ::Rails::Engine
# :nocov:
end

# Reassembles the callback that runs before sending the event to the Sentry:
# connects the user callback with the callback that adds lamian logs to the event.
# @private
# @return [Proc] final callback.
def rebuild_before_send
defined_callback = Sentry.configuration.before_send || build_default_lambda
lamian_callback = build_lamian_callback

proc { |*args, **kwargs| lamian_callback.call(defined_callback.call(*args, **kwargs)) }
end

# Builds a callback, which does nothing.
# @private
# @return [Proc] empty callback.
def build_default_lambda
-> (event, _hint) { event }
end

# Builds a callback that adds logs to the event.
# @private
# @return [Proc] callback.
def build_lamian_callback
lambda do |event|
event.tap do |event|
Expand Down
24 changes: 24 additions & 0 deletions lib/lamian/semantic_logger_appender.rb
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
2 changes: 1 addition & 1 deletion lib/lamian/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ module Lamian
# According to this, it is enought to specify '~> a.b'
# if private API was not used and to specify '~> a.b.c' if it was

VERSION = "1.3.0"
VERSION = "1.4.0"
end
35 changes: 35 additions & 0 deletions spec/lamian/semantic_logger_appender_spec.rb
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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "bundler/setup"
require "pry"
require "semantic_logger"

require "simplecov"
require "simplecov-lcov"
Expand Down

0 comments on commit 3e9746a

Please sign in to comment.