Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change log formatting when logging to journald
- Loading branch information
Showing
12 changed files
with
85 additions
and
23 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class RMT::Logger < ActiveSupport::Logger | ||
def initialize(dest) | ||
super(dest) | ||
self.formatter = if ENV['LOG_TO_JOURNALD'].present? | ||
proc do |severity, _time, _progname, msg| | ||
"#{severity}: #{msg}\n" | ||
end | ||
else | ||
::Logger::Formatter.new | ||
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
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
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 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe RMT::Logger do | ||
describe 'logging format' do | ||
subject(:logger) { described_class.new(log) } | ||
|
||
let(:log) { StringIO.new } | ||
|
||
context 'with env variable' do | ||
before do | ||
ENV['LOG_TO_JOURNALD'] = '1' | ||
end | ||
|
||
it do | ||
logger.info('42') | ||
log.rewind | ||
expect(log.read).to eq("INFO: 42\n") | ||
end | ||
end | ||
|
||
context 'without env variable' do | ||
before do | ||
ENV['LOG_TO_JOURNALD'] = nil | ||
end | ||
|
||
it do | ||
Timecop.freeze(Time.local(2018, 4, 4, 11, 11, 0)) do # rubocop:disable Rails/TimeZone | ||
logger.info('42') | ||
log.rewind | ||
expect(log.read).to eq("I, [2018-04-04T11:11:00.000000 ##{Process.pid}] INFO -- : 42\n") | ||
end | ||
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