diff --git a/Dockerfile b/Dockerfile index 76b87ca6..6186d852 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ @@ -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 diff --git a/Gemfile b/Gemfile index 96b12e6b..11b97600 100644 --- a/Gemfile +++ b/Gemfile @@ -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 @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index aede9b85..40179b05 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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 @@ -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 @@ -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! @@ -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) diff --git a/config/environments/development.rb b/config/environments/development.rb index 115fd8f7..591d8397 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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 diff --git a/config/environments/production.rb b/config/environments/production.rb index 2bab3d8d..6f7eca6d 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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? @@ -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 diff --git a/config/initializers/fixnum_patch.rb b/config/initializers/fixnum_patch.rb new file mode 100644 index 00000000..c61ffb26 --- /dev/null +++ b/config/initializers/fixnum_patch.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +Fixnum = Integer unless defined?(Integer) diff --git a/config/initializers/logging.rb b/config/initializers/logging.rb new file mode 100644 index 00000000..9c8783f5 --- /dev/null +++ b/config/initializers/logging.rb @@ -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 diff --git a/lib/custom_logger.rb b/lib/custom_logger.rb new file mode 100644 index 00000000..e069ca18 --- /dev/null +++ b/lib/custom_logger.rb @@ -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