Skip to content

Commit

Permalink
Format production log (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
relf authored Nov 28, 2024
1 parent 1ca1668 commit 0a53bb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 16 additions & 4 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,22 @@
# Skip http-to-https redirect for the default health check endpoint.
# config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }

# Log to STDOUT by default
# config.logger = ActiveSupport::Logger.new(STDOUT)
# .tap { |logger| logger.formatter = ::Logger::Formatter.new }
# .then { |logger| ActiveSupport::TaggedLogging.new(logger) }
# Customizing the log format by defining a new formatter class.
# This formatter outputs logs with a custom format including timestamp, severity,
# and the log message.
class CustomLoggerFormatter < Logger::Formatter
def call(severity, time, progname, msg)
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")
"#{formatted_time} #{severity} #{msg}\n"
end
end

# Configure log file rotation. This line sets up the logger to write to
# 'log/production.log', rotate the log file when it reaches 100 megabytes, and
# keep up to 10 old log files.
config.logger = Logger.new('log/production.log', 10, 100.megabytes)
.tap { |logger| logger.formatter = CustomLoggerFormatter.new }
.then { |logger| ActiveSupport::TaggedLogging.new(logger) }

# Prepend all log lines with the following tags.
config.log_tags = [ :remote_ip ]
Expand Down
20 changes: 16 additions & 4 deletions config/environments/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,22 @@
# Skip http-to-https redirect for the default health check endpoint.
# config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }

# Log to STDOUT by default
# config.logger = ActiveSupport::Logger.new(STDOUT)
# .tap { |logger| logger.formatter = ::Logger::Formatter.new }
# .then { |logger| ActiveSupport::TaggedLogging.new(logger) }
# Customizing the log format by defining a new formatter class.
# This formatter outputs logs with a custom format including timestamp, severity,
# and the log message.
class CustomLoggerFormatter < Logger::Formatter
def call(severity, time, progname, msg)
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")
"#{formatted_time} #{severity} #{msg}\n"
end
end

# Configure log file rotation. This line sets up the logger to write to
# 'log/staging.log', rotate the log file when it reaches 100 megabytes, and
# keep up to 10 old log files.
config.logger = Logger.new('log/staging.log', 10, 100.megabytes)
.tap { |logger| logger.formatter = CustomLoggerFormatter.new }
.then { |logger| ActiveSupport::TaggedLogging.new(logger) }

# Prepend all log lines with the following tags.
config.log_tags = [ :remote_ip ]
Expand Down

0 comments on commit 0a53bb0

Please sign in to comment.