Skip to content

Commit

Permalink
fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbradford committed Mar 26, 2024
1 parent dd6d216 commit 365c1de
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
11 changes: 8 additions & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# Logging configuration
require "dev_logger"
config.log_formatter = DevLogger.new("soils-ag-wx")
config.log_level = :debug

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# In the development environment your application's code is reloaded any time
# it changes. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
Expand Down Expand Up @@ -38,10 +46,7 @@

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false

# Letter opener config
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.default_url_options = {host: "localhost:3000"}

Expand Down
7 changes: 1 addition & 6 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,13 @@
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true

# Log to STDOUT by default
config.logger = ActiveSupport::Logger.new($stdout)
.tap { |logger| logger.formatter = ::Logger::Formatter.new }
.then { |logger| ActiveSupport::TaggedLogging.new(logger) }

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

# Info include generic and useful information about system operation, but avoids logging too much
# information to avoid inadvertent exposure of personally identifiable information (PII). If you
# want to log everything, set the level to "debug".
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")
config.log_level = :info

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
Expand Down
5 changes: 3 additions & 2 deletions config/environments/staging.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Just use the production settings
# Load production defaults
require File.expand_path("../production.rb", __FILE__)

# Overwrite production defaults
# Staging settings
Rails.application.configure do
config.action_mailer.default_url_options = {
host: ENV["AG_WEATHER_HOST"] || "dev.agweather.cals.wisc.edu",
protocol: "https"
}
config.log_level = :debug
end
41 changes: 41 additions & 0 deletions lib/dev_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# custom color-coded logging
class DevLogger < Logger::Formatter
PAL = {
black: "\u001b[30m",
red: "\u001b[31m",
green: "\u001b[32m",
yellow: "\u001b[33m",
blue: "\u001b[34m",
magenta: "\u001b[35m",
cyan: "\u001b[36m",
white: "\u001b[37m",
reset: "\u001b[0m"
}

SEV = {
debug: "DBG",
info: "NFO",
warn: "WRN",
error: "ERR",
fatal: "FTL",
unknown: "UNK"
}

CLR = {
debug: PAL[:green],
info: PAL[:white],
warn: PAL[:yellow],
error: PAL[:red],
fatal: PAL[:red]
}

def initialize(app_name = "dev")
@app = app_name
end

def call(severity, time, progname, msg)
sev = severity.to_sym.downcase
msg = msg&.truncate(500, omission: "...")
"#{CLR[sev]}[#{@app} #{SEV[sev]}] #{msg}#{PAL[:reset]}\n"
end
end

0 comments on commit 365c1de

Please sign in to comment.