Permalink
Browse files

some renaming to follow slf4j closer

  • Loading branch information...
1 parent 42fc4c0 commit 3dd35a047cdf7beef012c6f9e771d29efb472e16 @mkristian committed Sep 27, 2011
Showing with 158 additions and 126 deletions.
  1. +0 −22 lib/slf4r/init_slf4j.rb
  2. +0 −35 lib/slf4r/slf4r_railtie.rb
  3. +0 −3 lib/slf4r/version.rb
  4. 0 { → slf4r-gem}/History.txt
  5. 0 { → slf4r-gem}/MIT-LICENSE
  6. 0 { → slf4r-gem}/Mavenfile
  7. +17 −22 { → slf4r-gem}/README.rdoc
  8. 0 { → slf4r-gem}/Rakefile
  9. 0 { → slf4r-gem}/features/generators.feature
  10. 0 { → slf4r-gem}/features/step_definitions/simple_steps.rb
  11. 0 { → slf4r-gem}/lib/generators/slf4r/log4j/USAGE
  12. +2 −3 { → slf4r-gem}/lib/generators/slf4r/log4j/log4j_generator.rb
  13. 0 { → slf4r-gem}/lib/generators/slf4r/logging/USAGE
  14. +2 −3 { → slf4r-gem}/lib/generators/slf4r/logging/logging_generator.rb
  15. 0 { → slf4r-gem}/lib/generators/slf4r/templates/log4j.properties
  16. +0 −2 { → slf4r-gem}/lib/generators/slf4r/templates/log4j.rb
  17. +0 −2 { → slf4r-gem}/lib/generators/slf4r/templates/logging.rb
  18. +1 −1 { → slf4r-gem}/lib/log4r_adapter.rb
  19. +1 −1 { → slf4r-gem}/lib/logging_adapter.rb
  20. +1 −1 { → slf4r-gem}/lib/ruby_logger_adapter.rb
  21. +1 −1 { → slf4r-gem}/lib/slf4r.rb
  22. +1 −2 lib/slf4r/abstract_logger_facade.rb → slf4r-gem/lib/slf4r/abstract_logger.rb
  23. +53 −0 slf4r-gem/lib/slf4r/init.rb
  24. +2 −0 slf4r-gem/lib/slf4r/java.rb
  25. +1 −1 { → slf4r-gem}/lib/slf4r/java_logger.rb
  26. +2 −0 slf4r-gem/lib/slf4r/log4r.rb
  27. +2 −2 { → slf4r-gem}/lib/slf4r/log4r_logger.rb
  28. +1 −1 lib/slf4r/logger.rb → slf4r-gem/lib/slf4r/logger_module.rb
  29. +2 −0 slf4r-gem/lib/slf4r/logging.rb
  30. +2 −2 { → slf4r-gem}/lib/slf4r/logging_logger.rb
  31. +2 −0 slf4r-gem/lib/slf4r/noop.rb
  32. +1 −1 { → slf4r-gem}/lib/slf4r/noop_logger.rb
  33. +37 −0 slf4r-gem/lib/slf4r/railtie.rb
  34. +2 −0 slf4r-gem/lib/slf4r/ruby.rb
  35. +5 −5 { → slf4r-gem}/lib/slf4r/ruby_logger.rb
  36. +3 −0 slf4r-gem/lib/slf4r/version.rb
  37. +2 −0 slf4r-gem/lib/slf4r/wrapped.rb
  38. +5 −5 { → slf4r-gem}/lib/slf4r/wrapped_logger.rb
  39. +2 −1 { → slf4r-gem}/slf4r.gemspec
  40. +2 −2 { → slf4r-gem}/spec/log4r_logger_spec.rb
  41. 0 { → slf4r-gem}/spec/logger_helper.rb
  42. +1 −1 { → slf4r-gem}/spec/logging_logger_spec.rb
  43. +3 −3 { → slf4r-gem}/spec/ruby_logger_spec.rb
  44. 0 { → slf4r-gem}/spec/spec.opts
  45. 0 { → slf4r-gem}/spec/spec_helper.rb
  46. +2 −2 { → slf4r-gem}/spec/wrapper_logger_spec.rb
  47. 0 { → slf4r-gem}/templates/helper.rb
  48. 0 { → slf4r-gem}/templates/jruby.template
  49. 0 { → slf4r-gem}/templates/log4j.template
  50. 0 { → slf4r-gem}/templates/logging.template
  51. 0 { → slf4r-gem}/templates/simple.template
  52. 0 { → slf4r-gem}/templates/slf4r.template
  53. +0 −2 slf4r.gemspec.files
View
@@ -1,22 +0,0 @@
-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
@@ -1,35 +0,0 @@
-class Slf4rRailtie < Rails::Railtie
-
- initializer "set_servlet_logger", :after => :initialize_logger do |app|
- if defined?(Slf4r::LoggerFacade)
- @logger = Slf4r::LoggerFacade.new("Rails::Setup")
- @logger.info {
- l = @logger.instance_variable_get('@logger'.to_sym)
- logger_name = l.respond_to?(:java_class) ? l.java_class : l.class
- "setup slf4r logger #{logger_name}"
- }
- app.config.logger = Rails.logger = setup_logger(Rails)
- app.config.action_controller.logger = setup_logger(ActionController)
- app.config.active_record.logger = setup_logger(ActiveRecord)
- app.config.action_view.logger = setup_logger(ActionView) if app.config.action_view.respond_to? :logger=
- app.config.action_mailer.logger = setup_logger(ActionMailer)
- ActiveSupport::Cache::Store.logger = setup_logger(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
- else
- require 'slf4r/wrapped_logger'
- l = Slf4r::LoggerFacade4WrappedLogger.logger = Rails.logger
- l.info "setup slf4r logger wrapper"
- end
- end
-
- private
- def self.setup_logger(category)
- l = Slf4r::LoggerFacade.new((category == Rails ? "" : "Rails::") + category.to_s)
- @logger.info("setup logger for '#{l.name}'") if @logger
- l
- end
-end
View
@@ -1,3 +0,0 @@
-module Slf4r
- VERSION = '0.4.2'.freeze
-end
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -4,11 +4,11 @@ the main idea is from www.slf4j.org which is to provide an uniform interface for
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)
+ @logger = Slf4r::Logger.new(self.class)
or the convinience module inside any class which needs logging
- include Slf4r::Logger
+ include Slf4r::LoggerModule
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.
@@ -20,7 +20,7 @@ in case you have a framework B which uses log4r internally you can use the 'log4
=== using with logging gem
-require 'slf4r/logging_logger'
+require 'slf4r/logging'
Logging.logger.root.appenders = Logging.appenders.file("developent.log"),
:level => :debug,
@@ -29,39 +29,39 @@ Logging.logger.root.appenders = Logging.appenders.file("developent.log"),
=== using with ruby logger (from MRI)
-require 'slf4r/ruby_logger'
+require 'slf4r/ruby'
-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 "
+Slf4r::RubyLogger.level = :warn # default :debug
+Slf4r::RubyLogger.file = "development.log" # default STDOUT
+Slf4r::RubyLogger.dateformat = "%Y%m%d %H:%M:%S - " # default "%Y-%m-%d %H:%M:%S "
=== using with rails/merb/datamapper logger
-require 'slf4r/wrapped_logger'
+require 'slf4r/wrapped'
-Slf4r::LoggerFacade4WrappedLogger.logger = framwork_logger
+Slf4r::WrappedLogger.logger = framwork_logger
=== using with slf4j with jruby
-require 'slf4r/init_slf4j'
+require 'slf4r/java'
just get the needed jar files/ configuration files in the classpath
(see http://slf4j.org/) or if you use maven then have a look at the
pom.xml of this very project.
this require falls back on the standard ruby logger if there is no slf4j in the classpath.
-=== usign a noop logger
+=== using a noop logger
-require 'slf4r/noop_logger'
+require 'slf4r/noop'
=== getting an instance of a logger
-* logger = Slf4r::LoggerFacade.new("Fully::Qualified::Class::Name")
+* logger = Slf4r::Logger.new("Fully::Qualified::Class::Name")
or
-* logger = Slf4r::LoggerFacade.new("any name you wish")
+* logger = Slf4r::Logger.new("any name you wish")
or
-* logger = Slf4r::LoggerFacade.new(Fully::Qualified::Class::Name)
+* logger = Slf4r::Logger.new(Fully::Qualified::Class::Name)
there are following log-levels:
* logger.debug("asd") or logger.debug { "asd" } and logger.debug?
@@ -113,9 +113,9 @@ all these setups use the same files as default rails and adjust the log level fr
== use ruby-maven to add jar files to your rails3 application (experimental)
install it with
-$ jruby -S gem install ruby-maven --pre
+$ jruby -S gem install ruby-maven
-then you can run the rails3 with log4j wioth
+then you can run the rails3 with log4j with
$ rmvn rails server
or the generator
$ rmv rails generate . .
@@ -127,14 +127,9 @@ $ rmvn rails dbconsole
or run the server in jetty
$ jetty-run
-*NOTE* it is a prerelease so there are bugs and limitations
-
== TODO:
-* 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:
File renamed without changes.
File renamed without changes.
@@ -1,18 +1,17 @@
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"))
- gem 'slf4r', :require => 'slf4r/java_logger'
+ gem 'slf4r', :require => 'slf4r/java'
file_name = 'Mavenfile'
jar_line = "org.slf4j.slf4j-log4j12"
mavenfile = File.exists?(file_name) ? File.read(file_name) : ''
unless mavenfile =~ /#{jar_line}/
File.open(file_name, 'a') do |f|
- f.puts "jar '#{jar_line}', '1.6.1'"
+ f.puts "jar '#{jar_line}', '1.6.2'"
end
end
end
@@ -1,10 +1,9 @@
-class Slf4r::LoggingGenerator < Rails::Generators::Base
- #source_root File.expand_path('../templates', __FILE__)
+class Slf4r::LoggingGenerator < Rails::Generators::Based
def setup
source = File.expand_path('../../templates', __FILE__)
initializer "logging.rb", File.read(File.join(source, "logging.rb"))
- gem 'slf4r', :require => 'slf4r/logging_logger'
+ gem 'slf4r', :require => 'slf4r/logging'
gem "logging"
end
end
@@ -1,6 +1,4 @@
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')))
@@ -1,5 +1,3 @@
-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')
@@ -2,7 +2,7 @@ module Log4r
class Logger
def initialize(_fullname, _level=nil, _additive=true, _trace=false)
- @logger = ::Slf4r::LoggerFacade.new(_fullname)
+ @logger = ::Slf4r::Logger.new(_fullname)
end
def method_missing(method, *args, &block)
@@ -2,7 +2,7 @@ module Logging
class Logger
def initialize(name)
- @logger = ::Slf4r::LoggerFacade.new(name)
+ @logger = ::Slf4r::Logger.new(name)
end
class << self
@@ -1,7 +1,7 @@
class Logger
def initialize(*args)
- @logger = ::Slf4r::LoggerFacade.new(:root)
+ @logger = ::Slf4r::Logger.new(:root)
end
def debug(msg, &block)
@@ -1,4 +1,4 @@
if defined?(Rails::Railtie)
require 'slf4r/slf4r_railtie'
end
-require 'slf4r/logger'
+require 'slf4r/init'
@@ -1,6 +1,5 @@
-require 'slf4r'
module Slf4r
- class AbstractLoggerFacade
+ class AbstractLogger
protected
@@ -0,0 +1,53 @@
+require 'slf4r/logger_module'
+
+if defined?(Rails)
+ require 'slf4r/railtie'
+end
+
+# first import bridge from slf4j to slf4r when it is in classloader
+is_slf4j_to_slf4r = false
+if defined? JRUBY_VERSION
+ require 'java'
+ begin
+ import 'de.mkristian.slf4j.impl.Slf4rLoggerFactory'
+ is_slf4j_to_slf4r = true
+ rescue NameError
+ # ignore
+ end
+end
+
+#second make sure there is a Slf4r::Logger
+unless defined? Slf4r::Logger
+ if is_slf4j_to_slf4r
+ # we have slf4j-slf4r bridge but no ruby logging => use default
+ puts "using ruby logger"
+ require 'slf4r/ruby_logger'
+ else
+ # no slf4j-slf4r bridge in place
+ if defined? JRUBY_VERSION
+ require 'java'
+ begin
+ # make sure we have SLF4J in the classloader
+ import 'org.slf4j.LoggerFactory'
+ org.slf4j.LoggerFactory.getLogger("root")
+
+ # now we can use java logging for the slf4r
+ require 'slf4r/java_logger'
+ puts "using slf4j logger"
+ rescue NameError
+ puts "no SLF4J found in classloader - using ruby logger"
+ require 'slf4r/ruby_logger'
+ end
+ else
+ # no java, no slf4j => default ruby logging
+ puts "using ruby logger"
+ require 'slf4r/ruby_logger'
+ end
+ end
+end
+
+# third setup the slf4j-slf4r bridge if used
+if is_slf4j_to_slf4r
+ f = de.mkristian.slf4j.impl.Slf4rLoggerFactory.new
+ f.setSlf4rLoggerClass Slf4r::Logger
+end
@@ -0,0 +1,2 @@
+require 'slf4r/java_logger'
+require 'init'
@@ -1,6 +1,6 @@
require 'java'
module Slf4r
- class LoggerFacade
+ class Logger
protected
@@ -0,0 +1,2 @@
+require 'slf4r/log4r_logger'
+require 'init'
@@ -1,8 +1,8 @@
require 'log4r'
-require 'slf4r/abstract_logger_facade'
+require 'slf4r/abstract_logger'
module Slf4r
- class LoggerFacade < AbstractLoggerFacade
+ class Logger < AbstractLogger
attr_reader :logger
@@ -1,5 +1,5 @@
module Slf4r
- module Logger
+ module LoggerModule
def logger
@logger ||= ::Slf4r::LoggerFacade.new(self.class == Class ? name : self.class)
@@ -0,0 +1,2 @@
+require 'slf4r/logging_logger'
+require 'init'
@@ -1,8 +1,8 @@
require 'logging'
-require 'slf4r/abstract_logger_facade'
+require 'slf4r/abstract_logger'
module Slf4r
- class LoggerFacade < AbstractLoggerFacade
+ class Logger < AbstractLogger
attr_reader :logger
@@ -0,0 +1,2 @@
+require 'slf4r/noop_logger'
+require 'init'
@@ -1,5 +1,5 @@
module Slf4r
- class LoggerFacade
+ class Logger
def initialize(name)
@name = name
Oops, something went wrong.

0 comments on commit 3dd35a0

Please sign in to comment.