Skip to content

Commit

Permalink
LTI-404: add support for multi-logging (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfederico authored Aug 13, 2024
1 parent 9101a45 commit c8767a2
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RUN apk add --no-cache \
shared-mime-info \
libffi-dev

FROM base as builder
FROM base AS builder
RUN apk add --update --no-cache \
build-base \
libxml2-dev \
Expand Down Expand Up @@ -62,6 +62,12 @@ ARG PORT
ENV PORT=${PORT:-3000}
EXPOSE ${PORT}

# Create the log directory
RUN mkdir -p /usr/src/app/log

# Set the correct permissions for the log directory
RUN chmod -R 755 /usr/src/app/log

# Precompile assets
RUN SECRET_KEY_BASE=1 RAILS_ENV=${RAILS_ENV:-production} RELATIVE_URL_ROOT=${RELATIVE_URL_ROOT:-apps} bundle exec rake assets:precompile --trace

Expand Down
9 changes: 7 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ group :test do
end

group :production do
gem 'lograge', '~> 0.14.0'
gem 'remote_syslog_logger'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand All @@ -104,4 +102,11 @@ gem 'coveralls_reborn', require: false
gem 'net-smtp'
gem 'webpacker', '~> 6.0.0.rc.5'

gem 'bigdecimal'
gem 'mutex_m'
gem 'rdoc', require: false

gem 'logging', '~> 2.3'
gem 'lograge', '~> 0.14.0'
gem 'remote_syslog_logger'
gem 'syslog'
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ GEM
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
little-plugger (1.1.4)
logger (1.6.0)
logging (2.4.0)
little-plugger (~> 1.1)
multi_json (~> 1.14)
lograge (0.14.0)
actionpack (>= 4)
activesupport (>= 4)
Expand Down Expand Up @@ -187,6 +191,7 @@ GEM
multi_json (1.15.0)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
mutex_m (0.2.0)
net-http (0.4.1)
uri
net-imap (0.4.14)
Expand Down Expand Up @@ -384,6 +389,7 @@ GEM
stringio (3.1.1)
strscan (3.1.0)
sync (0.5.0)
syslog (0.1.0)
syslog_protocol (0.9.2)
term-ansicolor (1.10.2)
mize
Expand Down Expand Up @@ -442,6 +448,7 @@ DEPENDENCIES
action-cable-testing
activerecord-session_store (>= 2.1.0)
bigbluebutton-api-ruby (~> 1.9.1)
bigdecimal
byebug
capybara (>= 3.40.0)
coveralls_reborn
Expand All @@ -452,8 +459,10 @@ DEPENDENCIES
jbuilder (~> 2.11, >= 2.11.5)
json
listen (>= 3.0.5, < 3.2)
logging (~> 2.3)
lograge (~> 0.14.0)
minitest
mutex_m
net-smtp
omniauth (>= 2.1.2)
omniauth-bbbltibroker!
Expand All @@ -478,6 +487,7 @@ DEPENDENCIES
selenium-webdriver (>= 4.23.0)
spring
spring-watcher-listen (~> 2.0.0)
syslog
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
Expand Down
10 changes: 10 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@
config.hosts = nil

config.web_console.whiny_requests = false

# Disable output buffering when STDOUT isn't a tty (e.g. Docker images, systemd services)
$stdout.sync = true

# Set the logging level for the environment
config.log_level = :debug

# Use the custom logger with Rails
require_relative '../../lib/custom_logger'
config.logger = ActiveSupport::TaggedLogging.new(CustomLogger.new('custom_logger'))
end
14 changes: 4 additions & 10 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@

# Disable output buffering when STDOUT isn't a tty (e.g. Docker images, systemd services)
$stdout.sync = true
logger = ActiveSupport::Logger.new($stdout)

if ENV['RAILS_LOG_REMOTE_NAME'] && ENV['RAILS_LOG_REMOTE_PORT']
require 'remote_syslog_logger'
logger_program = ENV['RAILS_LOG_REMOTE_TAG'] || "bbb-lti-broker-#{ENV['RAILS_ENV']}"
logger = RemoteSyslogLogger.new(ENV['RAILS_LOG_REMOTE_NAME'],
ENV['RAILS_LOG_REMOTE_PORT'], program: logger_program)
end
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)

# configure redis for ActionCable
config.cache_store = if ENV['REDIS_URL'].present?
Expand All @@ -136,4 +126,8 @@

config.lograge.enabled = true
config.lograge.ignore_actions = ['HealthCheckController#show']

# Use the custom logger with Rails
require_relative '../../lib/custom_logger'
config.logger = ActiveSupport::TaggedLogging.new(CustomLogger.new('custom_logger'))
end
3 changes: 3 additions & 0 deletions config/initializers/fixnum_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

Fixnum = Integer unless defined?(Integer)
29 changes: 29 additions & 0 deletions config/initializers/logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'syslog/logger'
require_relative '../../lib/custom_logger'

# Initialize stdout logger
stdout_appender = Logging.appenders.stdout(
layout: Logging.layouts.pattern(
pattern: '[%d] %-5l %c: %m\n',
date_pattern: '%Y-%m-%d %H:%M:%S'
)
)

Logging.logger.root.appenders = stdout_appender

# Initialize remote logger (E.g for Papertrail)
if ENV['RAILS_LOG_REMOTE_NAME'] && ENV['RAILS_LOG_REMOTE_PORT']
require 'remote_syslog_logger'
remote_logger = RemoteSyslogLogger.new(
ENV['RAILS_LOG_REMOTE_NAME'],
ENV['RAILS_LOG_REMOTE_PORT'].to_i,
program: ENV['RAILS_LOG_REMOTE_TAG'] || "bbb-lti-broker-#{ENV['RAILS_ENV']}"
)
Rails.logger.extend(ActiveSupport::Logger.broadcast(remote_logger))
end

# Set the log level from the environment variable or default to debug
log_level = ENV['LOG_LEVEL']&.downcase || 'debug'
Logging.logger.root.level = log_level
18 changes: 18 additions & 0 deletions lib/custom_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'logging'
require 'active_support/tagged_logging'

class CustomLogger < ActiveSupport::Logger
def initialize(_logger_name)
# Define the log file path based on the Rails environment
log_file_path = Rails.root.join('log', "#{Rails.env}.log")

# Initialize the logger to write to the environment-specific log file
super(log_file_path)

self.formatter = proc do |severity, datetime, progname, msg|
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}] #{severity} #{progname}: #{msg}\n"
end
end
end

0 comments on commit c8767a2

Please sign in to comment.