Permalink
Browse files

added rails3 support: railtie + generators

  • Loading branch information...
1 parent 67a4381 commit 082e3cd812c29cd9eb3f23566021799350dfd7a8 @mkristian committed Dec 11, 2010
View
@@ -3,3 +3,4 @@ tmp
target
*~
lib/slf4r/version.rb.errors
+*pom
View
@@ -1,3 +1,9 @@
+=== 0.4.0 / 2010-12-11
+
+* added rails3 support: railtie + generators
+
+* revised readme
+
=== 0.3.3 / 2010-07-29
* reworked Rakefile, pom.xml
View
@@ -1,48 +1,39 @@
= SLF4R
-the main idea is from www.slf4j.org which is to provide a uniform interface for instantiating und using of a logger. but the actual logging is done by some third party logging framework.
+the main idea is from www.slf4j.org which is to provide an uniform interface for instantiating und using of a logger. but the actual logging is done by some third party logging framework.
-one idea is to have a logger per class or object (see also http://slf4j.org/). in ruby you would have something like
+the idea is to have a logger per class or object (see also http://slf4j.org/). in ruby you would have something like
@logger = Slf4r::LoggerFacade.new(self.class)
-or the convinience module
+or the convinience module inside any class which needs logging
include Slf4r::Logger
-if the underlying logging framework allows it (like logging or log4r) then you get a logger for each namespace of your class and create a hierachy of loggers. with this you can control the log level for each logger and/or namespace separately.
+if the underlying logging framework allows it (like logging or log4r) then you get a logger for each namespace of your class and create a hierachy of loggers. with this you can control the log level for each logger and/or namespace separately or any branch of the hierachy tree.
for example you have a framework A with namespace 'A' then you can set the log level for the logger with name 'A' to debug and get all the debug from the framework, etc.
in case you have a framework B which uses log4r internally you can use the 'log4r_adapter' to delegate the logger creation from log4r to slf4r. in this way you have only one place where logging gets configured and controlled.
-== FEATURES:
-
-* can replace other logging frameworks via adapters
-
-* for the actual logging it depends on a third party logging framework and its configuration
-
== SYNOPSIS:
=== using with logging gem
require 'slf4r/logging_logger'
-Logging.init :debug, :info, :warn, :error
+Logging.logger.root.appenders = Logging.appenders.file("developent.log"),
+ :level => :debug,
+ :layout => Logging.layouts.pattern(:pattern => '%d %l (%c) - %m\n')
+)
-appender = Logging::Appender.stdout
-appender.layout = Logging::Layouts::Pattern.new(:pattern => "%d [%-l] (%c) %m\n")
-logger = Logging::Logger.new(:root)
-logger.add_appenders(appender)
-logger.level = :debug
-
-=== using with ruby logger
+=== using with ruby logger (from MRI)
require 'slf4r/ruby_logger'
-=== using the log4r adapter
-
-require 'log4r_adapter'
+Slf4r::LoggerFacade4RubyLogger.level = :warn # default :debug
+Slf4r::LoggerFacade4RubyLogger.file = "development.log" # default STDOUT
+Slf4r::LoggerFacade4RubyLogger.dateformat = "%Y%m%d %H:%M:%S - " # default "%Y-%m-%d %H:%M:%S "
=== using with rails/merb/datamapper logger
@@ -52,33 +43,79 @@ Slf4r::LoggerFacade4WrappedLogger.logger = framwork_logger
=== using with slf4j with jruby
+require 'slf4r/init_slf4j'
+
just get the needed jar files/ configuration files in the classpath
-(see http://slf4j.org/) or if you use maven then have a look and the
-pom.xml of that project.
+(see http://slf4j.org/) or if you use maven then have a look at the
+pom.xml of this very project.
-=== getting an instance of a logger
+this require falls back on the standard ruby logger if there is no slf4j in the classpath.
-Slf4r::LoggerFacade.new("Full::Qualified::Class::Name")
+=== usign a noop logger
-or
+require 'slf4r/noop_logger'
-Slf4r::LoggerFacade.new(Full::Qualified::Class::Name)
+=== getting an instance of a logger
-== REQUIREMENTS:
+* logger = Slf4r::LoggerFacade.new("Fully::Qualified::Class::Name")
+or
+* logger = Slf4r::LoggerFacade.new("any name you wish")
+or
+* logger = Slf4r::LoggerFacade.new(Fully::Qualified::Class::Name)
-* logging for slf4r/logging_logger
+there are following log-levels:
+* logger.debug("asd") or logger.debug { "asd" } and logger.debug?
+* logger.info("asd") or logger.info { "asd" } and logger.warn?
+* logger.warn("asd") or logger.warn { "asd" } and logger.info?
+* logger.error("asd") or logger.error { "asd" } and logger.error?
+* logger.fatal("asd") or logger.fatal { "asd" } and logger.fatal?
-* log4r for slf4r/log4r_logger
+the block variant will evaluate the block only if the log level indicates logging to avoid needless string operations.
-* slf4j jars in the classpath for slf4j
+* logger.name
+will return the log category or name
== INSTALL:
-* sudo gem install slf4r
+=== general
+
+$ gem install slf4r
+
+=== Rails3
+
+=== use the Rails.logger as slf4r logger
+
+add in your Gemfile
+
+ gem 'slf4r'
+
+=== rails generators
+
+setup a log4j logging (fallback on rails logging without JRUBY)
+
+$ rails3 generate slf4r:log4j
+
+but to use this you need to add slf4j-log4j12-1.6.1.jar to you classpath or require the jar
+
+setup a logging from logging gem
+
+$ rails3 generate slf4r:logging
+
+all these setups use the same files as default rails and adjust the log level from the rails config. they establish the folowing categories:
+* Rails
+* <NAME>.Application # where the name is your application name
+* ActionController
+* ActiveRecord
+* ActionView
+* ActionMailer
+* ActiveSupport::Cache::Store
-* sudo gem install logging # optional
+== TODO:
-* sudo gem install log4r # optional
+* the bridge from ruby to java, i.e. using rails logging for java libraries using slf4j, i.e. the java classes will log into the rails logging framework
+* follow the naming of slf4j
+* for the completeness: generators for jdk14, java-commons-logging, logback, log4r
+* generators for slf4j should take advantage of ruby-maven and configure the jar in Gemfile as dependency
== LICENSE:
@@ -0,0 +1,8 @@
+Description:
+ Explain the generator
+
+Example:
+ rails generate log4j Thing
+
+ This will create:
+ what/will/it/create
@@ -0,0 +1,9 @@
+class Slf4r::Log4jGenerator < Rails::Generators::Base
+ #source_root File.expand_path('../templates', __FILE__)
+
+ def setup
+ source = File.expand_path('../../templates', __FILE__)
+ initializer "log4j.rb", File.read(File.join(source, "log4j.rb"))
+ initializer "log4j.properties", File.read(File.join(source, "log4j.properties"))
+ end
+end
@@ -0,0 +1,8 @@
+Description:
+ Explain the generator
+
+Example:
+ rails generate logging Thing
+
+ This will create:
+ what/will/it/create
@@ -0,0 +1,9 @@
+class Slf4r::LoggingGenerator < Rails::Generators::Base
+ #source_root File.expand_path('../templates', __FILE__)
+
+ def setup
+ source = File.expand_path('../../templates', __FILE__)
+ initializer "logging.rb", File.read(File.join(source, "logging.rb"))
+ gem "logging"
+ end
+end
@@ -0,0 +1,14 @@
+log4j.rootCategory=debug, stdout, logfile
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=(%c) - %m%n
+
+log4j.appender.logfile=org.apache.log4j.RollingFileAppender
+
+log4j.appender.logfile.MaxFileSize=100KB
+# Keep one backup file
+log4j.appender.logfile.MaxBackupIndex=1
+
+log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
+log4j.appender.logfile.layout.ConversionPattern=%d %p (%c) - %m%n
@@ -0,0 +1,19 @@
+if defined? JRUBY_VERSION
+ require 'slf4r/java_logger'
+
+ # load log4j configuration
+ props = java.util.Properties.new
+ props.load(java.io.FileInputStream.new(File.join(File.dirname(__FILE__), 'log4j.properties')))
+
+ # use rails default log file location
+ props.setProperty("log4j.appender.logfile.File", File.join(Rails.root, "log", "#{Rails.env}.log"))
+
+ # adjust the log level
+ root = props.getProperty("log4j.rootCategory")
+ props.setProperty("log4j.rootCategory", root.sub(/^[a-zA-Z]+/, Rails.application.config.log_level.to_s))
+
+ # configure log4j
+ org.apache.log4j.PropertyConfigurator.configure(props)
+else
+ Rails.logger.debug("skip log4j config since no jruby is used")
+end
@@ -0,0 +1,6 @@
+require 'slf4r/logging_logger'
+
+Logging.logger.root.appenders = Logging.appenders.file(
+ File.join(Rails.root, "log", "#{Rails.env}.log"),
+ :layout => Logging.layouts.pattern(:pattern => '%d %l (%c) - %m\n')
+)
View
@@ -1,18 +1,3 @@
-require 'slf4r/logger'
-begin
- # make sure we have java
- require 'java'
- begin
- # make sure we have SLF4J in the classloader
- import 'org.slf4j.LoggerFactory'
-
- require 'slf4r/java_logger'
- puts "using slf4j logger"
- rescue NameError
- puts "no SLF4J found in classloader - using ruby logger"
- require 'slf4r/ruby_logger'
- end
-rescue LoadError
- puts "using ruby logger"
- require 'slf4r/ruby_logger'
+if defined?(Rails)
+ require 'slf4r/slf4r_railtie'
end
View
@@ -0,0 +1,22 @@
+require 'slf4r/logger'
+begin
+ # make sure we have java
+ require 'java'
+ begin
+ # make sure we have SLF4J in the classloader
+ import 'org.slf4j.LoggerFactory'
+ org.slf4j.LoggerFactory.getLogger("root")
+
+ require 'slf4r/java_logger'
+ puts "using slf4j logger"
+ rescue NameError
+ puts "no SLF4J found in classloader - using ruby logger"
+ require 'slf4r/ruby_logger'
+ end
+rescue LoadError
+ puts "using ruby logger"
+ require 'slf4r/ruby_logger'
+end
+if defined?(Rails)
+ require 'slf4r/slf4r_railtie'
+end
View
@@ -4,7 +4,7 @@
module Slf4r
class LoggerFacade4RubyLogger
- @@file = STDERR
+ @@file = STDOUT
@@datetime_format = "%Y-%m-%d %H:%M:%S "
def self.new_logger_facade(name)
View
@@ -0,0 +1,41 @@
+class Slf4rRailtie < Rails::Railtie
+
+ # config.before_configuration do |app|
+ # app.config.class.class_eval do
+ # attr_accessor :slf4r
+ # end
+ # end
+
+ config.after_initialize do |app|
+ if defined?(Slf4r)
+ logger = (Rails.logger = setup_logger(Rails.logger, Rails))
+ app.config.logger = setup_logger(app.config.logger, Rails.application.class)
+ app.config.action_controller.logger = Slf4r::LoggerFacade.new(ActionController)
+ app.config.active_record.logger = Slf4r::LoggerFacade.new(ActiveRecord)
+ app.config.action_view.logger = Slf4r::LoggerFacade.new(ActionView)
+ app.config.action_mailer.logger = Slf4r::LoggerFacade.new(ActionMailer)
+ ActiveSupport::Cache::Store.logger = Slf4r::LoggerFacade.new(ActiveSupport::Cache::Store)
+ clazz = if logger.instance_variable_get(:@logger).respond_to?(:java_class)
+ logger.instance_variable_get(:@logger).java_class
+ else
+ logger.instance_variable_get(:@logger).class
+ end
+ logger.info("setup slf4r logger categories for #{clazz}")
+ else
+ require 'slf4r/wrapped_logger'
+ logger = (Slf4r::LoggerFacade4WrappedLogger.logger = Rails.logger)
+ logger.info("setup slf4r logger wrapper with #{logger.inspect}")
+ end
+ end
+
+ private
+ def self.setup_logger(logger, category)
+ l = Slf4r::LoggerFacade.new(category)
+ real_logger = l.instance_variable_get(:@logger)
+ # adjust log level it the logger implementation allows it
+ if logger && real_logger.respond_to?(:level)
+ real_logger.level = logger.level
+ end
+ l
+ end
+end
View
@@ -1,3 +1,3 @@
module Slf4r
- VERSION = '0.3.3'.freeze
+ VERSION = '0.4.0'.freeze
end
View
@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>rubygems</groupId>
<artifactId>slf4r</artifactId>
- <version>0.3.4-SNAPSHOT</version>
+ <version>0.4.0-SNAPSHOT</version>
<packaging>gem</packaging>
<name>Slf4r</name>
<description>Slf4r provides a uniform interface for instantiating und using of a logger</description>
@@ -125,6 +125,6 @@
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <jruby.plugins.version>0.20.0</jruby.plugins.version>
+ <jruby.plugins.version>0.23.0-SNAPSHOT</jruby.plugins.version>
</properties>
</project>
View
@@ -1,7 +1,7 @@
# create by maven - leave it as is
Gem::Specification.new do |s|
s.name = 'slf4r'
- s.version = '0.3.3'
+ s.version = '0.4.0'
s.summary = 'Slf4r'
s.description = 'Slf4r provides a uniform interface for instantiating und using of a logger'
View
@@ -1,7 +1,9 @@
require 'pathname'
require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
-def Slf4r.flush(klass)
- remove_const(klass.to_sym) if const_defined? klass.to_sym
+module Slf4r
+ def flush(klass)
+ remove_const(klass.to_sym) if const_defined? klass.to_sym
+ end
end
def behave_as_logger(logger)
@@ -1,5 +1,6 @@
require 'pathname'
require 'fileutils'
+require 'logger'
require Pathname(__FILE__).dirname.expand_path + 'logger_helper'
basedir = Pathname(__FILE__).dirname.parent.expand_path

0 comments on commit 082e3cd

Please sign in to comment.