slf4r 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.4.0 / 2010-12-11
2
+
3
+ * added rails3 support: railtie + generators
4
+
5
+ * revised readme
6
+
1
7
  === 0.3.3 / 2010-07-29
2
8
 
3
9
  * reworked Rakefile, pom.xml
data/README.txt CHANGED
@@ -1,48 +1,39 @@
1
1
  = SLF4R
2
2
 
3
- 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.
3
+ 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.
4
4
 
5
- one idea is to have a logger per class or object (see also http://slf4j.org/). in ruby you would have something like
5
+ the idea is to have a logger per class or object (see also http://slf4j.org/). in ruby you would have something like
6
6
 
7
7
  @logger = Slf4r::LoggerFacade.new(self.class)
8
8
 
9
- or the convinience module
9
+ or the convinience module inside any class which needs logging
10
10
 
11
11
  include Slf4r::Logger
12
12
 
13
- 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.
13
+ 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.
14
14
 
15
15
  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.
16
16
 
17
17
  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.
18
18
 
19
- == FEATURES:
20
-
21
- * can replace other logging frameworks via adapters
22
-
23
- * for the actual logging it depends on a third party logging framework and its configuration
24
-
25
19
  == SYNOPSIS:
26
20
 
27
21
  === using with logging gem
28
22
 
29
23
  require 'slf4r/logging_logger'
30
24
 
31
- Logging.init :debug, :info, :warn, :error
32
-
33
- appender = Logging::Appender.stdout
34
- appender.layout = Logging::Layouts::Pattern.new(:pattern => "%d [%-l] (%c) %m\n")
35
- logger = Logging::Logger.new(:root)
36
- logger.add_appenders(appender)
37
- logger.level = :debug
25
+ Logging.logger.root.appenders = Logging.appenders.file("developent.log"),
26
+ :level => :debug,
27
+ :layout => Logging.layouts.pattern(:pattern => '%d %l (%c) - %m\n')
28
+ )
38
29
 
39
- === using with ruby logger
30
+ === using with ruby logger (from MRI)
40
31
 
41
32
  require 'slf4r/ruby_logger'
42
33
 
43
- === using the log4r adapter
44
-
45
- require 'log4r_adapter'
34
+ Slf4r::LoggerFacade4RubyLogger.level = :warn # default :debug
35
+ Slf4r::LoggerFacade4RubyLogger.file = "development.log" # default STDOUT
36
+ Slf4r::LoggerFacade4RubyLogger.dateformat = "%Y%m%d %H:%M:%S - " # default "%Y-%m-%d %H:%M:%S "
46
37
 
47
38
  === using with rails/merb/datamapper logger
48
39
 
@@ -52,33 +43,98 @@ Slf4r::LoggerFacade4WrappedLogger.logger = framwork_logger
52
43
 
53
44
  === using with slf4j with jruby
54
45
 
46
+ require 'slf4r/init_slf4j'
47
+
55
48
  just get the needed jar files/ configuration files in the classpath
56
- (see http://slf4j.org/) or if you use maven then have a look and the
57
- pom.xml of that project.
49
+ (see http://slf4j.org/) or if you use maven then have a look at the
50
+ pom.xml of this very project.
58
51
 
59
- === getting an instance of a logger
52
+ this require falls back on the standard ruby logger if there is no slf4j in the classpath.
60
53
 
61
- Slf4r::LoggerFacade.new("Full::Qualified::Class::Name")
54
+ === usign a noop logger
62
55
 
63
- or
56
+ require 'slf4r/noop_logger'
64
57
 
65
- Slf4r::LoggerFacade.new(Full::Qualified::Class::Name)
58
+ === getting an instance of a logger
66
59
 
67
- == REQUIREMENTS:
60
+ * logger = Slf4r::LoggerFacade.new("Fully::Qualified::Class::Name")
61
+ or
62
+ * logger = Slf4r::LoggerFacade.new("any name you wish")
63
+ or
64
+ * logger = Slf4r::LoggerFacade.new(Fully::Qualified::Class::Name)
68
65
 
69
- * logging for slf4r/logging_logger
66
+ there are following log-levels:
67
+ * logger.debug("asd") or logger.debug { "asd" } and logger.debug?
68
+ * logger.info("asd") or logger.info { "asd" } and logger.warn?
69
+ * logger.warn("asd") or logger.warn { "asd" } and logger.info?
70
+ * logger.error("asd") or logger.error { "asd" } and logger.error?
71
+ * logger.fatal("asd") or logger.fatal { "asd" } and logger.fatal?
70
72
 
71
- * log4r for slf4r/log4r_logger
73
+ the block variant will evaluate the block only if the log level indicates logging to avoid needless string operations.
72
74
 
73
- * slf4j jars in the classpath for slf4j
75
+ * logger.name
76
+ will return the log category or name
74
77
 
75
78
  == INSTALL:
76
79
 
77
- * sudo gem install slf4r
80
+ === general
81
+
82
+ $ gem install slf4r
83
+
84
+ === Rails3
85
+
86
+ === use the Rails.logger as slf4r logger
87
+
88
+ add in your Gemfile
89
+
90
+ gem 'slf4r'
91
+
92
+ === rails generators
93
+
94
+ setup a log4j logging (fallback on rails logging without JRUBY)
95
+
96
+ $ rails3 generate slf4r:log4j
97
+
98
+ but to use this you need to add slf4j-log4j12-1.6.1.jar to you classpath or require the jar. see below how to do this with ruby-maven gem.
99
+
100
+ setup a logging from logging gem
101
+
102
+ $ rails3 generate slf4r:logging
103
+
104
+ all these setups use the same files as default rails and adjust the log level from the rails config. they establish the folowing categories:
105
+ * Rails
106
+ * <NAME>.Application # where the name is your application name
107
+ * ActionController
108
+ * ActiveRecord
109
+ * ActionView
110
+ * ActionMailer
111
+ * ActiveSupport::Cache::Store
112
+
113
+ == use ruby-maven to add jar files to your rails3 application (experimental)
114
+
115
+ install it with
116
+ $ jruby -S gem install ruby-maven --pre
117
+
118
+ then you can run the rails3 with log4j wioth
119
+ $ rmvn rails server
120
+ or the generator
121
+ $ rmv rails generate . .
122
+ or the rake
123
+ $ rmvn rake . . .
124
+ or console/dbconsole
125
+ $ rmvn rails console
126
+ $ rmvn rails dbconsole
127
+ or run the server in jetty
128
+ $ jetty-run
129
+
130
+ *NOTE* it is a prerelease so there are bugs and limitations
78
131
 
79
- * sudo gem install logging # optional
132
+ == TODO:
80
133
 
81
- * sudo gem install log4r # optional
134
+ * 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
135
+ * follow the naming of slf4j
136
+ * for the completeness: generators for jdk14, java-commons-logging, logback, log4r
137
+ * generators for slf4j should take advantage of ruby-maven and configure the jar in Gemfile as dependency
82
138
 
83
139
  == LICENSE:
84
140
 
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate log4j Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,20 @@
1
+ class Slf4r::Log4jGenerator < Rails::Generators::Base
2
+ #source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def setup
5
+ source = File.expand_path('../../templates', __FILE__)
6
+ initializer "log4j.rb", File.read(File.join(source, "log4j.rb"))
7
+ initializer "log4j.properties", File.read(File.join(source, "log4j.properties"))
8
+
9
+ gemfile = File.read('Gemfile')
10
+ jar_line = "org.slf4j.slf4j-log4j12"
11
+ unless gemfile =~ /#{jar_line}/
12
+ File.open('Gemfile', 'a') do |f|
13
+ f.puts
14
+ f.puts "if defined? MAVEN"
15
+ f.puts " jar '#{jar_line}', '1.6.1'"
16
+ f.puts "end"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate logging Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,9 @@
1
+ class Slf4r::LoggingGenerator < Rails::Generators::Base
2
+ #source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def setup
5
+ source = File.expand_path('../../templates', __FILE__)
6
+ initializer "logging.rb", File.read(File.join(source, "logging.rb"))
7
+ gem "logging"
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ log4j.rootCategory=debug, stdout, logfile
2
+
3
+ log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4
+ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5
+ log4j.appender.stdout.layout.ConversionPattern=(%c) - %m%n
6
+
7
+ log4j.appender.logfile=org.apache.log4j.RollingFileAppender
8
+
9
+ log4j.appender.logfile.MaxFileSize=100KB
10
+ # Keep one backup file
11
+ log4j.appender.logfile.MaxBackupIndex=1
12
+
13
+ log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
14
+ log4j.appender.logfile.layout.ConversionPattern=%d %p (%c) - %m%n
@@ -0,0 +1,19 @@
1
+ if defined? JRUBY_VERSION
2
+ require 'slf4r/java_logger'
3
+
4
+ # load log4j configuration
5
+ props = java.util.Properties.new
6
+ props.load(java.io.FileInputStream.new(File.join(File.dirname(__FILE__), 'log4j.properties')))
7
+
8
+ # use rails default log file location
9
+ props.setProperty("log4j.appender.logfile.File", File.join(Rails.root, "log", "#{Rails.env}.log"))
10
+
11
+ # adjust the log level
12
+ root = props.getProperty("log4j.rootCategory")
13
+ props.setProperty("log4j.rootCategory", root.sub(/^[a-zA-Z]+/, Rails.application.config.log_level.to_s))
14
+
15
+ # configure log4j
16
+ org.apache.log4j.PropertyConfigurator.configure(props)
17
+ else
18
+ Rails.logger.debug("skip log4j config since no jruby is used")
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'slf4r/logging_logger'
2
+
3
+ Logging.logger.root.appenders = Logging.appenders.file(
4
+ File.join(Rails.root, "log", "#{Rails.env}.log"),
5
+ :layout => Logging.layouts.pattern(:pattern => '%d %l (%c) - %m\n')
6
+ )
@@ -0,0 +1,22 @@
1
+ require 'slf4r/logger'
2
+ begin
3
+ # make sure we have java
4
+ require 'java'
5
+ begin
6
+ # make sure we have SLF4J in the classloader
7
+ import 'org.slf4j.LoggerFactory'
8
+ org.slf4j.LoggerFactory.getLogger("root")
9
+
10
+ require 'slf4r/java_logger'
11
+ puts "using slf4j logger"
12
+ rescue NameError
13
+ puts "no SLF4J found in classloader - using ruby logger"
14
+ require 'slf4r/ruby_logger'
15
+ end
16
+ rescue LoadError
17
+ puts "using ruby logger"
18
+ require 'slf4r/ruby_logger'
19
+ end
20
+ if defined?(Rails)
21
+ require 'slf4r/slf4r_railtie'
22
+ end
@@ -4,7 +4,7 @@ require 'slf4r/abstract_logger_facade'
4
4
  module Slf4r
5
5
  class LoggerFacade4RubyLogger
6
6
 
7
- @@file = STDERR
7
+ @@file = STDOUT
8
8
  @@datetime_format = "%Y-%m-%d %H:%M:%S "
9
9
 
10
10
  def self.new_logger_facade(name)
@@ -0,0 +1,41 @@
1
+ class Slf4rRailtie < Rails::Railtie
2
+
3
+ # config.before_configuration do |app|
4
+ # app.config.class.class_eval do
5
+ # attr_accessor :slf4r
6
+ # end
7
+ # end
8
+
9
+ config.after_initialize do |app|
10
+ if defined?(Slf4r)
11
+ logger = (Rails.logger = setup_logger(Rails.logger, Rails))
12
+ app.config.logger = setup_logger(app.config.logger, Rails.application.class)
13
+ app.config.action_controller.logger = Slf4r::LoggerFacade.new(ActionController)
14
+ app.config.active_record.logger = Slf4r::LoggerFacade.new(ActiveRecord)
15
+ app.config.action_view.logger = Slf4r::LoggerFacade.new(ActionView)
16
+ app.config.action_mailer.logger = Slf4r::LoggerFacade.new(ActionMailer)
17
+ ActiveSupport::Cache::Store.logger = Slf4r::LoggerFacade.new(ActiveSupport::Cache::Store)
18
+ clazz = if logger.instance_variable_get(:@logger).respond_to?(:java_class)
19
+ logger.instance_variable_get(:@logger).java_class
20
+ else
21
+ logger.instance_variable_get(:@logger).class
22
+ end
23
+ logger.info("setup slf4r logger categories for #{clazz}")
24
+ else
25
+ require 'slf4r/wrapped_logger'
26
+ logger = (Slf4r::LoggerFacade4WrappedLogger.logger = Rails.logger)
27
+ logger.info("setup slf4r logger wrapper with #{logger.class}")
28
+ end
29
+ end
30
+
31
+ private
32
+ def self.setup_logger(logger, category)
33
+ l = Slf4r::LoggerFacade.new(category)
34
+ real_logger = l.instance_variable_get(:@logger)
35
+ # adjust log level it the logger implementation allows it
36
+ if logger && real_logger.respond_to?(:level)
37
+ real_logger.level = logger.level
38
+ end
39
+ l
40
+ end
41
+ end
data/lib/slf4r/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Slf4r
2
- VERSION = '0.3.3'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
data/lib/slf4r.rb CHANGED
@@ -1,18 +1,3 @@
1
- require 'slf4r/logger'
2
- begin
3
- # make sure we have java
4
- require 'java'
5
- begin
6
- # make sure we have SLF4J in the classloader
7
- import 'org.slf4j.LoggerFactory'
8
-
9
- require 'slf4r/java_logger'
10
- puts "using slf4j logger"
11
- rescue NameError
12
- puts "no SLF4J found in classloader - using ruby logger"
13
- require 'slf4r/ruby_logger'
14
- end
15
- rescue LoadError
16
- puts "using ruby logger"
17
- require 'slf4r/ruby_logger'
1
+ if defined?(Rails)
2
+ require 'slf4r/slf4r_railtie'
18
3
  end
@@ -1,7 +1,9 @@
1
1
  require 'pathname'
2
2
  require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
3
- def Slf4r.flush(klass)
4
- remove_const(klass.to_sym) if const_defined? klass.to_sym
3
+ module Slf4r
4
+ def flush(klass)
5
+ remove_const(klass.to_sym) if const_defined? klass.to_sym
6
+ end
5
7
  end
6
8
 
7
9
  def behave_as_logger(logger)
@@ -1,5 +1,6 @@
1
1
  require 'pathname'
2
2
  require 'fileutils'
3
+ require 'logger'
3
4
  require Pathname(__FILE__).dirname.expand_path + 'logger_helper'
4
5
 
5
6
  basedir = Pathname(__FILE__).dirname.parent.expand_path
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 3
9
- version: 0.3.3
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Meier
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-31 00:00:00 +05:30
17
+ date: 2010-12-11 00:00:00 +05:30
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -27,8 +27,8 @@ dependencies:
27
27
  segments:
28
28
  - 1
29
29
  - 3
30
- - 0
31
- version: 1.3.0
30
+ - 1
31
+ version: 1.3.1
32
32
  type: :development
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,34 @@ dependencies:
59
59
  version: 1.1.7
60
60
  type: :development
61
61
  version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: cucumber
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ - 9
72
+ - 4
73
+ version: 0.9.4
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rails
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 3
85
+ - 0
86
+ - 1
87
+ version: 3.0.1
88
+ type: :development
89
+ version_requirements: *id005
62
90
  description: Slf4r provides a uniform interface for instantiating und using of a logger
63
91
  email:
64
92
  - m.kristian@web.de
@@ -74,27 +102,35 @@ files:
74
102
  - History.txt
75
103
  - README.txt
76
104
  - Rakefile
77
- - lib/logging_adapter.rb
78
105
  - lib/slf4r.rb
79
- - lib/log4r_adapter.rb
106
+ - lib/logging_adapter.rb
80
107
  - lib/ruby_logger_adapter.rb
81
- - lib/slf4r/logger.rb
82
- - lib/slf4r/noop_logger.rb
108
+ - lib/log4r_adapter.rb
109
+ - lib/generators/slf4r/logging/logging_generator.rb
110
+ - lib/generators/slf4r/logging/USAGE
111
+ - lib/generators/slf4r/templates/log4j.properties
112
+ - lib/generators/slf4r/templates/log4j.rb
113
+ - lib/generators/slf4r/templates/logging.rb
114
+ - lib/generators/slf4r/log4j/log4j_generator.rb
115
+ - lib/generators/slf4r/log4j/USAGE
116
+ - lib/slf4r/version.rb
83
117
  - lib/slf4r/abstract_logger_facade.rb
84
- - lib/slf4r/logging_logger.rb
85
118
  - lib/slf4r/log4r_logger.rb
119
+ - lib/slf4r/logger.rb
120
+ - lib/slf4r/ruby_logger.rb
86
121
  - lib/slf4r/wrapped_logger.rb
87
- - lib/slf4r/version.rb.errors
122
+ - lib/slf4r/init_slf4j.rb
123
+ - lib/slf4r/noop_logger.rb
124
+ - lib/slf4r/logging_logger.rb
125
+ - lib/slf4r/slf4r_railtie.rb
88
126
  - lib/slf4r/java_logger.rb
89
- - lib/slf4r/ruby_logger.rb
90
- - lib/slf4r/version.rb
91
- - spec/logger_helper.rb
92
127
  - spec/spec_helper.rb
93
- - spec/ruby_logger_spec.rb
94
128
  - spec/wrapper_logger_spec.rb
95
- - spec/spec.opts
96
129
  - spec/logging_logger_spec.rb
130
+ - spec/spec.opts
131
+ - spec/ruby_logger_spec.rb
97
132
  - spec/log4r_logger_spec.rb
133
+ - spec/logger_helper.rb
98
134
  has_rdoc: true
99
135
  homepage: http://github.com/mkristian/slf4r
100
136
  licenses:
@@ -127,7 +163,7 @@ signing_key:
127
163
  specification_version: 3
128
164
  summary: Slf4r
129
165
  test_files:
130
- - spec/ruby_logger_spec.rb
131
166
  - spec/wrapper_logger_spec.rb
132
167
  - spec/logging_logger_spec.rb
168
+ - spec/ruby_logger_spec.rb
133
169
  - spec/log4r_logger_spec.rb
File without changes