Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable production-like logs if env variable is set #302

Merged
merged 3 commits into from
Jul 5, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* BREAKING: JSON logs are no longer configured automatically for production Rails apps and are turned on with the GOVUK_RAILS_JSON_LOGGING environment variable ([#302](https://github.com/alphagov/govuk_app_config/pull/302))

# 8.1.1

* Fix prometheus_exporter to method patching compatible with open telemetry.
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ check docs](docs/healthchecks.md) for more information on how to use it.
In Rails applications, the application will be configured to send JSON-formatted
logs to `STDOUT` and unstructured logs to `STDERR`.
yuetylauiris marked this conversation as resolved.
Show resolved Hide resolved

To enable production-like logging, an env variable `GOVUK_RAILS_JSON_LOGGING`
is set in the `govuk-helm-charts` and then checked in `railtie.rb`. This will
allow JSON format logs and `Govuk-Request-Id` to be visible.

For development logs, in order to see the production style logs, developers should
set `GOVUK_RAILS_JSON_LOGGING`in `govuk-docker` -> `docker-compose` files.


## Content Security Policy generation

Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_app_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require "govuk_app_config/govuk_prometheus_exporter"

if defined?(Rails)
require "govuk_app_config/govuk_logging"
require "govuk_app_config/govuk_json_logging"
require "govuk_app_config/govuk_content_security_policy"
require "govuk_app_config/railtie"
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "action_controller"
require_relative "rails_ext/action_dispatch/debug_exceptions"

module GovukLogging
module GovukJsonLogging
def self.configure
# GOV.UK Rails applications are expected to output JSON to stdout which is
# then indexed in a Kibana instance. These log outputs are created by the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "action_dispatch/middleware/debug_exceptions"

module GovukLogging
module GovukJsonLogging
module RailsExt
module ActionDispatch
def self.should_monkey_patch_log_error?(clazz = ::ActionDispatch::DebugExceptions)
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_app_config/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Railtie < Rails::Railtie
end

config.before_initialize do
GovukLogging.configure if Rails.env.production?
GovukJsonLogging.configure if ENV["GOVUK_RAILS_JSON_LOGGING"]
end

config.after_initialize do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "spec_helper"
require "rails"
require "govuk_app_config/govuk_logging"
require "govuk_app_config/govuk_json_logging"
require "rack/test"

RSpec.describe GovukLogging do
RSpec.describe GovukJsonLogging do
before do
stub_const("DummyLoggingRailsApp", Class.new(Rails::Application) do
config.hosts.clear
Expand Down Expand Up @@ -35,19 +35,19 @@
describe ".configure" do
it "enables logstasher" do
Rails.application.config.logstasher.enabled = false
expect { GovukLogging.configure }
expect { GovukJsonLogging.configure }
.to change { Rails.application.config.logstasher.enabled }
.to(true)
end

it "initialises a logstasher logger using the rails logger level" do
GovukLogging.configure
GovukJsonLogging.configure
expect(Rails.application.config.logstasher.logger.level)
.to eq(info_log_level)
end

it "can write to logstasher log" do
GovukLogging.configure
GovukJsonLogging.configure
logger = Rails.application.config.logstasher.logger
logger.info("test log entry")
fake_stdout.rewind
Expand All @@ -56,7 +56,7 @@
end

it "can write to default rails logger" do
GovukLogging.configure
GovukJsonLogging.configure
logger = Rails.logger
logger.info("test default log entry")
$stderr.rewind
Expand All @@ -72,7 +72,7 @@ def app
end

it "logs errors thrown by the application" do
GovukLogging.configure
GovukJsonLogging.configure
get "/error"
$stderr.rewind
lines = $stderr.read.split("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "rails"
require "govuk_app_config/rails_ext/action_dispatch/debug_exceptions"

RSpec.describe ::GovukLogging::RailsExt::ActionDispatch do
RSpec.describe ::GovukJsonLogging::RailsExt::ActionDispatch do
describe "#should_monkey_patch_log_error?" do
before do
Rails.logger = double(:rails_logger)
Expand Down