diff --git a/Gemfile b/Gemfile index 3db57832b..28d7532de 100644 --- a/Gemfile +++ b/Gemfile @@ -169,6 +169,10 @@ gem "mobile-fu", "1.3.1" gem "will_paginate", "3.0.7" gem "rails-timeago", "2.11.0" +# Logging + +gem "logging-rails", "0.5.0", require: "logging/rails" + # Workarounds # https://github.com/rubyzip/rubyzip#important-note gem "zip-zip" diff --git a/Gemfile.lock b/Gemfile.lock index 7a91520df..4d039a756 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -390,6 +390,12 @@ GEM celluloid (~> 0.16.0) rb-fsevent (>= 0.9.3) rb-inotify (>= 0.9) + little-plugger (1.1.3) + logging (2.0.0) + little-plugger (~> 1.1) + multi_json (~> 1.10) + logging-rails (0.5.0) + logging (>= 1.8) loofah (2.0.2) nokogiri (>= 1.5.9) lumberjack (1.0.9) @@ -767,6 +773,7 @@ DEPENDENCIES js_image_paths (= 0.0.2) jshintrb (= 0.3.0) json (= 1.8.2) + logging-rails (= 0.5.0) markerb (= 1.0.2) messagebus_ruby_api (= 1.0.3) mini_magick (= 4.2.3) diff --git a/config/environments/development.rb b/config/environments/development.rb index 138c8ccd5..4ddeb92ca 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -31,4 +31,10 @@ Diaspora::Application.configure do # Expands the lines which load the assets config.assets.debug = true + + # Set the logging destination(s) + config.log_to = %w[stdout file] + + # Show the logging configuration on STDOUT + config.show_log_configuration = true end diff --git a/config/environments/production.rb b/config/environments/production.rb index 3adcc72d5..a624236ab 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -37,6 +37,12 @@ Diaspora::Application.configure do # Log level config.log_level = :info + # Set the logging destination(s) + config.log_to = %w[file] + + # Show the logging configuration on STDOUT + config.show_log_configuration = false + # Prepend all log lines with the following tags # config.log_tags = [ :subdomain, :uuid ] @@ -66,7 +72,7 @@ Diaspora::Application.configure do config.active_support.deprecation = :notify # For nginx: - config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' + config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" if AppConfig.environment.assets.host.present? config.action_controller.asset_host = AppConfig.environment.assets.host.get diff --git a/config/logging.rb b/config/logging.rb new file mode 100644 index 000000000..b58db314e --- /dev/null +++ b/config/logging.rb @@ -0,0 +1,85 @@ +Logging::Rails.configure do |config| + # Configure the Logging framework with the default log levels + Logging.init %w(debug info warn error fatal) + + # Objects will be converted to strings using the :inspect method. + Logging.format_as :inspect + + # The default layout used by the appenders. + layout = Logging.layouts.pattern(pattern: "[%d] %-5l %c : %m\n") + + # Setup a color scheme called 'bright' than can be used to add color codes + # to the pattern layout. Color schemes should only be used with appenders + # that write to STDOUT or STDERR; inserting terminal color codes into a file + # is generally considered bad form. + Logging.color_scheme("bright", + levels: { + info: :green, + warn: :yellow, + error: :red, + fatal: %i(white on_red) + }, + date: :blue, + logger: :cyan, + message: :magenta + ) + + # Configure an appender that will write log events to STDOUT. A colorized + # pattern layout is used to format the log events into strings before + # writing. + Logging.appenders.stdout("stdout", + auto_flushing: true, + layout: Logging.layouts.pattern( + pattern: "[%d] %-5l %c : %m\n", + color_scheme: "bright" + ) + ) if config.log_to.include? "stdout" + + # Configure an appender that will write log events to a file. The file will + # be rolled on a daily basis, and the past 7 rolled files will be kept. + # Older files will be deleted. The default pattern layout is used when + # formatting log events into strings. + Logging.appenders.rolling_file("file", + filename: config.paths["log"].first, + keep: 7, + age: "daily", + truncate: false, + auto_flushing: true, + layout: layout + ) if config.log_to.include? "file" + + # Setup the root logger with the Rails log level and the desired set of + # appenders. The list of appenders to use should be set in the environment + # specific configuration file. + # + # For example, in a production application you would not want to log to + # STDOUT, but you would want to send an email for "error" and "fatal" + # messages: + # + # => config/environments/production.rb + # + # config.log_to = %w[file email] + # + # In development you would want to log to STDOUT and possibly to a file: + # + # => config/environments/development.rb + # + # config.log_to = %w[stdout file] + # + Logging.logger.root.level = config.log_level + Logging.logger.root.appenders = config.log_to unless config.log_to.empty? + + # Under Phusion Passenger smart spawning, we need to reopen all IO streams + # after workers have forked. + # + # The rolling file appender uses shared file locks to ensure that only one + # process will roll the log file. Each process writing to the file must have + # its own open file descriptor for `flock` to function properly. Reopening + # the file descriptors after forking ensures that each worker has a unique + # file descriptor. + if defined? PhusionPassenger + PhusionPassenger.on_event(:starting_worker_process) do |forked| + Logging.reopen if forked + end + end +end diff --git a/config/unicorn.rb b/config/unicorn.rb index 0dfabe003..f70f54fa8 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -26,6 +26,8 @@ before_fork do |_server, _worker| end after_fork do |_server, _worker| + Logging.reopen # reopen logfiles to obtain a new file descriptor + ActiveRecord::Base.establish_connection # preloading app in master, so reconnect to DB # We don't generate uuids in the frontend, but let's be on the safe side