log4jruby 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,87 @@
1
+ = Log4jruby
2
+
3
+ * https://github.com/lenny/log4jruby
4
+
5
+ == Description:
6
+
7
+ Log4jruby is a thin wrapper around the {Log4j Logger}[http://logging.apache.org/log4j/1.2/apidocs/index.html]. It is geared more toward those who are using JRuby to integrate with and build on top of Java code that uses Log4j. The <tt>Log4jruby::Logger</tt> provides an interface much like the standard ruby {Logger}[http://ruby-doc.org/core/classes/Logger.html]. Logging is configured via traditional Log4j methods.
8
+
9
+ * Filename, line number, and method name are available(if tracing is on) to your appender layout via {MDC}[http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html].
10
+ * Exceptions can be logged directly and are output with backtraces. Java Exceptions(i.e. NativeExceptions)
11
+ are logged with full java backtrace(including nested exceptions).
12
+ * Logging config for your ruby code can be added to your existing configuration. Ruby logger names are mapped to dot separated names prefixed with <tt>.jruby</tt>
13
+
14
+ log4j.appender.Ruby=org.apache.log4j.ConsoleAppender
15
+ log4j.appender.Ruby.layout=org.apache.log4j.PatternLayout
16
+ log4j.appender.Ruby.layout.ConversionPattern=%5p %.50X{fileName} %X{methodName}:%X{lineNumber} - %m%n
17
+
18
+ log4j.logger.jruby=info,Ruby
19
+ log4j.logger.jruby.MyClass=debug
20
+
21
+ === Examples
22
+
23
+ def foo
24
+ logger.debug("hello from foo")
25
+ begin
26
+ bar
27
+ rescue => e
28
+ logger.error(e)
29
+ end
30
+ end
31
+
32
+ DEBUG jruby.MyClass foo:15 - hello from foo
33
+ DEBUG jruby.MyClass bar:24 - hello from bar
34
+ DEBUG jruby.MyClass baz:29 - hello from baz
35
+ ERROR jruby.MyClass foo:19 - error from baz
36
+ examples/simple.rb:30:in `baz'
37
+ examples/simple.rb:25:in `bar'
38
+ examples/simple.rb:17:in `foo'
39
+ examples/simple.rb:35
40
+
41
+
42
+ Mix It In
43
+
44
+ class MyClass
45
+ include Log4jruby::LoggerForClass
46
+
47
+ class << self
48
+ def my_class_method
49
+ logger.info("hello from class method")
50
+ end
51
+ end
52
+
53
+ def my_method
54
+ logger.info("hello from instance method")
55
+ end
56
+ end
57
+
58
+ INFO examples/logger_per_class.rb my_class_method:11 - hello from class method
59
+ INFO examples/logger_per_class.rb my_method:16 - hello from instance method
60
+
61
+ See more in {log4jruby/examples}[http://github.com/lenny/log4jruby/tree/master/examples/]. They should be runnable from the source.
62
+
63
+ == Installation
64
+ > gem install log4jruby
65
+
66
+ or
67
+
68
+ > git clone git://github.com/lenny/log4jruby.git
69
+
70
+ == Usage
71
+
72
+ The primary use case(i.e. mine) for this library is that you are already up and running with Log4j and now you want to use it for your Ruby code too. There is not much help here for configuration. In our environment, we deploy Rails apps that call and extend Java code into Tomcat as WARs and use a log4j.properties to configure. For the most part, all there is to using log4jruby is making sure the log4j jar is <tt>required</tt> and that Log4j is at least minimally configured(e.g. log4j.properties in the CLASSPATH). The examples should give you the idea.
73
+
74
+ === Rails
75
+
76
+ First try to add the following to one of your config/environment* files.
77
+
78
+ require 'log4jruby'
79
+ config.logger = Log4jruby::Logger.get('MyApp')
80
+
81
+ Depending on your runtime environment and how it differs between development and production that may not work for you
82
+ due to CLASSPATH issues. Alternatively you may add the following to one of your config/initializers/* files.
83
+
84
+ require 'log4jruby/rails'
85
+ Log4jruby::Rails.configure do |c|
86
+ c.logger_name = 'MyApp'
87
+ end
data/lib/log4jruby.rb ADDED
@@ -0,0 +1 @@
1
+ require 'log4jruby/logger'
@@ -0,0 +1,40 @@
1
+ module Log4jruby
2
+ class Log4jArgs
3
+ class << self
4
+ def convert(object = nil, exception = nil)
5
+ msg = ''
6
+
7
+ if exception
8
+ msg << object.to_s
9
+ else
10
+ exception = block_given? ? yield : object
11
+ end
12
+
13
+ if exception.is_a?(NativeException)
14
+ append_ruby_error(msg, exception)
15
+ msg << "\nNativeException:"
16
+ exception = exception.cause
17
+ elsif exception.is_a?(::Exception)
18
+ append_ruby_error(msg, exception)
19
+ exception = nil
20
+ elsif !exception.is_a?(java.lang.Throwable)
21
+ msg << exception.to_s
22
+ exception = nil
23
+ end
24
+
25
+ [msg, exception]
26
+ end
27
+
28
+ private
29
+
30
+ def append_ruby_error(msg, error)
31
+ append(msg, "#{error}\n " + error.backtrace.join("\n "))
32
+ end
33
+
34
+ def append(msg, s)
35
+ msg << "\n#{s}"
36
+ msg.lstrip!
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,202 @@
1
+ require 'log4jruby/log4j_args'
2
+
3
+ module Log4jruby
4
+
5
+ # Author:: Lenny Marks
6
+ #
7
+ # Wrapper around org.apache.log4j.Logger with interface similar to standard ruby Logger.
8
+ #
9
+ # * Ruby and Java exceptions are logged with backtraces.
10
+ # * fileName, lineNumber, methodName available to appender layouts via MDC variables(e.g. %X{lineNumber})
11
+ class Logger
12
+ BLANK_CALLER = ['', '', ''] #:nodoc:
13
+
14
+ # turn tracing on to make fileName, lineNumber, and methodName available to
15
+ # appender layout through MDC(ie. %X{fileName} %X{lineNumber} %X{methodName})
16
+ attr_accessor :tracing
17
+
18
+ class << self
19
+ def logger_mapping
20
+ @logger_mapping ||= {}
21
+ end
22
+
23
+ # get Logger for name
24
+ def[](name)
25
+ name = name.nil? ? 'jruby' : "jruby.#{name.gsub('::', '.')}"
26
+
27
+ log4j = Java::org.apache.log4j.Logger.getLogger(name)
28
+ log4jruby = logger_mapping[log4j]
29
+
30
+ unless log4jruby
31
+ log4jruby = new(log4j)
32
+ end
33
+
34
+ log4jruby
35
+ end
36
+
37
+ # same as [] but acceptions attributes
38
+ def get(name, values = {})
39
+ logger = self[name]
40
+ logger.attributes = values
41
+ logger
42
+ end
43
+
44
+ # Return root Logger(i.e. jruby)
45
+ def root
46
+ log4j = Java::org.apache.log4j.Logger.getLogger('jruby')
47
+
48
+ log4jruby = logger_mapping[log4j]
49
+ unless log4jruby
50
+ log4jruby = new(log4j)
51
+ end
52
+ log4jruby
53
+ end
54
+ end
55
+
56
+ def attributes=(values)
57
+ if values
58
+ values.each_pair do |k, v|
59
+ setter = "#{k}="
60
+ send(setter, v) if respond_to?(setter)
61
+ end
62
+ end
63
+ end
64
+
65
+ # Shortcut for setting log levels. (:debug, :info, :warn, :error)
66
+ def level=(level)
67
+ @logger.level = case level
68
+ when :debug
69
+ then Java::org.apache.log4j.Level::DEBUG
70
+ when :info
71
+ then Java::org.apache.log4j.Level::INFO
72
+ when :warn
73
+ then Java::org.apache.log4j.Level::WARN
74
+ when :error
75
+ then Java::org.apache.log4j.Level::ERROR
76
+ else
77
+ raise NotImplementedError
78
+ end
79
+ end
80
+
81
+ def level
82
+ @logger.level
83
+ end
84
+
85
+ def flush
86
+ #rails compatability
87
+ end
88
+
89
+ def debug(object = nil, &block)
90
+ if debug?
91
+ send_to_log4j(:debug, object, nil, &block)
92
+ end
93
+ end
94
+
95
+ def info(object = nil, &block)
96
+ if info?
97
+ send_to_log4j(:info, object, nil, &block)
98
+ end
99
+ end
100
+
101
+ def warn(object = nil, &block)
102
+ if warn?
103
+ send_to_log4j(:warn, object, nil, &block)
104
+ end
105
+ end
106
+
107
+ def error(object = nil, &block)
108
+ send_to_log4j(:error, object, nil, &block)
109
+ end
110
+
111
+ def log_error(msg, error)
112
+ send_to_log4j(:error, msg, error)
113
+ end
114
+
115
+ def fatal(object = nil, &block)
116
+ send_to_log4j(:fatal, object, nil, &block)
117
+ end
118
+
119
+ def log_fatal(msg, error)
120
+ send_to_log4j(:fatal, msg, error)
121
+ end
122
+
123
+ # return org.apache.log4j.Logger instance backing this Logger
124
+ def log4j_logger
125
+ @logger
126
+ end
127
+
128
+ def debug?
129
+ @logger.isEnabledFor(Java::org.apache.log4j.Priority::DEBUG)
130
+ end
131
+
132
+ def info?
133
+ @logger.isEnabledFor(Java::org.apache.log4j.Priority::INFO)
134
+ end
135
+
136
+ def warn?
137
+ @logger.isEnabledFor(Java::org.apache.log4j.Priority::WARN)
138
+ end
139
+
140
+ def tracing?
141
+ if tracing.nil?
142
+ if parent == Logger.root
143
+ Logger.root.tracing == true
144
+ else
145
+ parent.tracing?
146
+ end
147
+ else
148
+ tracing == true
149
+ end
150
+ end
151
+
152
+ def parent
153
+ logger_mapping[log4j_logger.parent] || Logger.root
154
+ end
155
+
156
+ private
157
+
158
+ def logger_mapping
159
+ Logger.logger_mapping
160
+ end
161
+
162
+ def initialize(logger, values = {}) # :nodoc:
163
+ @logger = logger
164
+
165
+ Logger.logger_mapping[@logger] = self
166
+
167
+ self.attributes = values
168
+ end
169
+
170
+ def with_context # :nodoc:
171
+ file_line_method = tracing? ? parse_caller(caller(3).first) : BLANK_CALLER
172
+
173
+ mdc.put("fileName", file_line_method[0])
174
+ mdc.put("lineNumber", file_line_method[1])
175
+ mdc.put("methodName", file_line_method[2].to_s)
176
+
177
+ begin
178
+ yield
179
+ ensure
180
+ mdc.remove("fileName")
181
+ mdc.remove("lineNumber")
182
+ mdc.remove("methodName")
183
+ end
184
+ end
185
+
186
+ def send_to_log4j(level, object, error, &block)
187
+ msg, throwable = Log4jArgs.convert(object, error, &block)
188
+ with_context do
189
+ @logger.send(level, msg, throwable)
190
+ end
191
+ end
192
+
193
+ def parse_caller(at) # :nodoc:
194
+ at.match(/^(.+?):(\d+)(?::in `(.*)')?/).captures
195
+ end
196
+
197
+ def mdc
198
+ Java::org.apache.log4j.MDC
199
+ end
200
+
201
+ end
202
+ end
@@ -0,0 +1,26 @@
1
+ require 'log4jruby'
2
+
3
+ module Log4jruby
4
+
5
+ # Gives class its own Logger and makes it available via class and instance method.
6
+ module LoggerForClass
7
+
8
+ def self.included(klass)
9
+ def klass.logger
10
+ @logger ||= Logger.get(name)
11
+ end
12
+
13
+ def klass.logger=(logger)
14
+ @logger = logger
15
+ end
16
+
17
+ def klass.set_logger(name, options = {})
18
+ @logger = Logger.get(name, options)
19
+ end
20
+ end
21
+
22
+ def logger
23
+ self.class.logger
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,62 @@
1
+ require 'log4jruby'
2
+
3
+ module Log4jruby
4
+
5
+ # Configure Rails logging from a config/initializers file
6
+ #
7
+ # Setting up log4j using config.logger from within the Rails initialization
8
+ # process may not be possible if the CLASSPATH has not yet been setup.
9
+ # This class can be used to configure the logging from within a config/initializers/
10
+ # file.
11
+ #
12
+ # ex.
13
+ # require 'log4jruby/rails'
14
+ # Log4jruby::Rails.configure do |c|
15
+ # c.logger_name = 'MyApp'
16
+ # end
17
+ #
18
+ # @attr [String] logger_name Default is 'Rails'
19
+ # @attr [String] tracing Defaults to false in 'production' or true otherwise
20
+ #
21
+ #
22
+ class Rails
23
+ attr_accessor :logger_name, :tracing
24
+
25
+ def initialize #:nodoc:
26
+ @logger_name = 'Rails'
27
+ @tracing = (::Rails.env != 'production')
28
+
29
+ yield(self)
30
+ end
31
+
32
+ class << self
33
+
34
+ # Sets rails Base loggers(e.g. ActionController::Base, ActiveRecord::Base, etc)
35
+ #
36
+ # @yield [config] Block to customize configuration
37
+ # @yeildparam [Rails] config
38
+ def configure(&block)
39
+ config = new(&block)
40
+
41
+ Logger.root.tracing = config.tracing
42
+
43
+ logger = Log4jruby::Logger.get(config.logger_name)
44
+
45
+ set_logger('ActionController', logger)
46
+ set_logger('ActiveRecord', logger)
47
+ set_logger('ActiveResource', logger)
48
+ set_logger('ActionMailer', logger)
49
+ end
50
+
51
+ private
52
+
53
+ def set_logger(framework, logger)
54
+ begin
55
+ Kernel.const_get(framework).const_get('Base').logger = logger
56
+ rescue
57
+ logger.info "Skipping logger setup for #{framework}"
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log4jruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
+ platform: ruby
11
+ authors:
12
+ - Lenny Marks
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-02 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 1
31
+ version: 1.3.1
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: " Log4jruby is a thin wrapper around the Log4j Logger. It is geared more toward \n those who are using JRuby to integrate with and build on top of Java code that \n uses Log4j. The Log4jruby::Logger provides an interface much like the standard \n ruby Logger. Your ruby loggers are given a .jruby prefix and can be configured \n along with the loggers for your Java code.\n"
35
+ email:
36
+ - lenny@aps.org
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - lib/log4jruby.rb
45
+ - lib/log4jruby/logger.rb
46
+ - lib/log4jruby/logger_for_class.rb
47
+ - lib/log4jruby/log4j_args.rb
48
+ - lib/log4jruby/rails.rb
49
+ - README.rdoc
50
+ - History.txt
51
+ has_rdoc: true
52
+ homepage: https://github.com/lenny/log4jruby
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --main
58
+ - README.rdoc
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.6
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Ruby Logger using Log4j, geared toward those who use JRuby to write Ruby code using/extending Java code. Ruby and Java are configured together using traditional Log4j methods.
82
+ test_files: []
83
+