From 5bd13563c5cb61fadbd63b2659fa5b1949ae999c Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Tue, 16 Jun 2015 01:14:44 +0200 Subject: [PATCH] add logging module to engine and add logging-gem to dummy-app --- .gitignore | 1 + Gemfile | 5 ++ Gemfile.lock | 8 ++ lib/diaspora_federation.rb | 4 + lib/diaspora_federation/logging.rb | 25 ++++++ test/dummy/config/environments/development.rb | 6 ++ test/dummy/config/environments/production.rb | 6 ++ test/dummy/config/logging.rb | 78 +++++++++++++++++++ 8 files changed, 133 insertions(+) create mode 100644 lib/diaspora_federation/logging.rb create mode 100644 test/dummy/config/logging.rb diff --git a/.gitignore b/.gitignore index 6e7d083..844902a 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ rdoc test/dummy/db/*.sqlite3 test/dummy/db/*.sqlite3-journal test/dummy/log/*.log +test/dummy/log/*.log* test/dummy/tmp/ test/dummy/.sass-cache test/dummy/test/fixtures/*.y*ml diff --git a/Gemfile b/Gemfile index 2fbae3d..eef57cc 100644 --- a/Gemfile +++ b/Gemfile @@ -46,3 +46,8 @@ group :development, :test do # test database gem "sqlite3" end + +group :development, :production do + # Logging (only for dummy-app, not for the gem) + gem "logging-rails", "0.5.0", require: "logging/rails" +end diff --git a/Gemfile.lock b/Gemfile.lock index 8e056a3..9f6c3ee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,6 +71,12 @@ GEM activesupport (>= 4.1.0) i18n (0.7.0) json (1.8.3) + 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) macaddr (1.7.1) @@ -81,6 +87,7 @@ GEM mime-types (2.6.1) mini_portile (0.6.2) minitest (5.7.0) + multi_json (1.11.1) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) nyan-cat-formatter (0.11) @@ -188,6 +195,7 @@ DEPENDENCIES factory_girl_rails (= 4.5.0) fixture_builder (= 0.4.1) fuubar (= 2.0.0) + logging-rails (= 0.5.0) nyan-cat-formatter pry pry-byebug diff --git a/lib/diaspora_federation.rb b/lib/diaspora_federation.rb index 0bb8953..04934e1 100644 --- a/lib/diaspora_federation.rb +++ b/lib/diaspora_federation.rb @@ -1,8 +1,11 @@ require "diaspora_federation/engine" +require "diaspora_federation/logging" ## # diaspora* federation rails engine module DiasporaFederation + extend Logging + class << self ## # the pod url @@ -31,6 +34,7 @@ module DiasporaFederation # end def configure yield self + logger.info "successfully configured the federation engine" end end end diff --git a/lib/diaspora_federation/logging.rb b/lib/diaspora_federation/logging.rb new file mode 100644 index 0000000..2ee6e1e --- /dev/null +++ b/lib/diaspora_federation/logging.rb @@ -0,0 +1,25 @@ +module DiasporaFederation + ## + # logging module for the diaspora federation engine + # + # it uses the logging-gem if available + module Logging + private + + ## + # get the logger for this class + # + # use the logging-gem if available, else use a default logger + def logger + @logger ||= begin + # use logging-gem if available + return ::Logging::Logger[self] if Object.const_defined?("::Logging::Logger") + + # fallback logger + @logger = Logger.new(STDOUT) + @logger.level = Logger.const_get(Rails.configuration.log_level.to_s.upcase) + @logger + end + end + end +end diff --git a/test/dummy/config/environments/development.rb b/test/dummy/config/environments/development.rb index b55e214..b885227 100644 --- a/test/dummy/config/environments/development.rb +++ b/test/dummy/config/environments/development.rb @@ -38,4 +38,10 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = 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/test/dummy/config/environments/production.rb b/test/dummy/config/environments/production.rb index 74acd2c..577a255 100644 --- a/test/dummy/config/environments/production.rb +++ b/test/dummy/config/environments/production.rb @@ -48,6 +48,12 @@ Rails.application.configure do # when problems arise. config.log_level = :debug + # 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 ] diff --git a/test/dummy/config/logging.rb b/test/dummy/config/logging.rb new file mode 100644 index 0000000..c4b65e9 --- /dev/null +++ b/test/dummy/config/logging.rb @@ -0,0 +1,78 @@ +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. + pattern = "[%d] %-5l PID-%p TID-%t %c: %m\n" + layout = Logging.layouts.pattern(pattern: pattern) + + # 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: pattern, + 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.appenders = config.log_to unless config.log_to.empty? + + # Default log-level (development=debug, production=info) + Logging.logger.root.level = config.log_level + + # log-levels for SQL and federation debug-logging + Logging.logger[ActiveRecord::Base].level = :debug + Logging.logger["XMLLogger"].level = :debug +end