diff --git a/doc/config/environment-variables.md b/doc/config/environment-variables.md index 940424e04..b41644174 100644 --- a/doc/config/environment-variables.md +++ b/doc/config/environment-variables.md @@ -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 | diff --git a/doc/config/yaml.yml b/doc/config/yaml.yml index f3a735a9f..2fa02a958 100644 --- a/doc/config/yaml.yml +++ b/doc/config/yaml.yml @@ -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 diff --git a/lib/postal/config.rb b/lib/postal/config.rb index a93cdac9e..c683b2d94 100644 --- a/lib/postal/config.rb +++ b/lib/postal/config.rb @@ -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 diff --git a/lib/postal/config_schema.rb b/lib/postal/config_schema.rb index e3c6415d5..275cb88cd 100644 --- a/lib/postal/config_schema.rb +++ b/lib/postal/config_schema.rb @@ -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 diff --git a/spec/lib/postal_spec.rb b/spec/lib/postal_spec.rb index f416ad497..2d9e61842 100644 --- a/spec/lib/postal_spec.rb +++ b/spec/lib/postal_spec.rb @@ -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