Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/config/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ This document contains all the environment variables which are available for thi
| `LOGGING_SENTRY_DSN` | String | A DSN which should be used to report exceptions to Sentry | |
| `LOGGING_ENABLED` | Boolean | Enable the Postal logger to log to STDOUT | true |
| `LOGGING_HIGHLIGHTING_ENABLED` | Boolean | Enable highlighting of log lines | false |
| `LOGGING_JSON` | Boolean | Enable JSON formatting for log output | false |
| `GELF_HOST` | String | GELF-capable host to send logs to | |
| `GELF_PORT` | Integer | GELF port to send logs to | 12201 |
| `GELF_FACILITY` | String | The facility name to add to all log entries sent to GELF | postal |
Expand Down
2 changes: 2 additions & 0 deletions doc/config/yaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ logging:
enabled: true
# Enable highlighting of log lines
highlighting_enabled: false
# Enable JSON formatting for log output
json: false

gelf:
# GELF-capable host to send logs to
Expand Down
3 changes: 2 additions & 1 deletion lib/postal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def host_with_protocol

def logger
@logger ||= begin
k = Klogger.new(nil, destination: Config.logging.enabled? ? $stdout : "/dev/null", highlight: Config.logging.highlighting_enabled?)
formatter = Config.logging.json? ? :json : :go
k = Klogger.new(nil, destination: Config.logging.enabled? ? $stdout : "/dev/null", formatter: formatter, highlight: Config.logging.highlighting_enabled?)
k.add_destination(graylog_logging_destination) if Config.gelf.host.present?
k
end
Expand Down
5 changes: 5 additions & 0 deletions lib/postal/config_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ module Postal
description "Enable highlighting of log lines"
default false
end

boolean :json do
description "Enable JSON formatting for log output"
default false
end
end

group :gelf do
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/postal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,26 @@
expect { Postal.change_database_connection_pool_size(8) }.to change { ActiveRecord::Base.connection_pool.size }.from(5).to(8)
end
end

describe "#logger" do
before do
Postal.instance_variable_set(:@logger, nil)
end

after do
Postal.instance_variable_set(:@logger, nil)
end

it "uses the go formatter by default" do
allow(Postal::Config.logging).to receive(:json?).and_return(false)
logger = Postal.logger
expect(logger.formatter).to be_a(Klogger::Formatters::Go)
end

it "uses the json formatter when json logging is enabled" do
allow(Postal::Config.logging).to receive(:json?).and_return(true)
logger = Postal.logger
expect(logger.formatter).to be_a(Klogger::Formatters::JSON)
end
end
end