log4jruby 0.5.0-java

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,91 @@
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].
8
+ It is geared more toward those who are using JRuby to integrate with and build on top of Java code that uses Log4j.
9
+ The <tt>Log4jruby::Logger</tt> provides an interface much like the standard ruby {Logger}[http://ruby-doc.org/core/classes/Logger.html].
10
+ Logging is configured via traditional Log4j methods.
11
+
12
+ The primary use case(i.e. mine) for this library is that you are already up and running with Log4j and now you want
13
+ to use it for your Ruby code too. There is not much help here for configuration. In our environment, we deploy Rails
14
+ apps that call and extend Java code into Tomcat as WARs and use a log4j.properties to configure. For the most part,
15
+ all there is to using log4jruby is making sure the log4j jar is <tt>required</tt> and that Log4j is at least minimally
16
+ configured(e.g. log4j.properties in the CLASSPATH). The examples should give you the idea.
17
+
18
+ === Features
19
+
20
+ * 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].
21
+ * Exceptions can be logged directly and are output with backtraces. Java Exceptions(i.e. NativeExceptions)
22
+ are logged with full java backtrace(including nested exceptions).
23
+ * 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>
24
+
25
+ log4j.appender.Ruby=org.apache.log4j.ConsoleAppender
26
+ log4j.appender.Ruby.layout=org.apache.log4j.PatternLayout
27
+ log4j.appender.Ruby.layout.ConversionPattern=%5p %.50X{fileName} %X{methodName}:%X{lineNumber} - %m%n
28
+
29
+ log4j.logger.jruby=info,Ruby
30
+ log4j.logger.jruby.MyClass=debug
31
+
32
+ === Examples
33
+
34
+ def foo
35
+ logger.debug("hello from foo")
36
+ bar
37
+ rescue => e
38
+ logger.error(e)
39
+ end
40
+
41
+ DEBUG jruby.MyClass foo:17 - hello from foo
42
+ DEBUG jruby.MyClass bar:24 - hello from bar
43
+ DEBUG jruby.MyClass baz:29 - hello from baz
44
+ ERROR jruby.MyClass foo:20 - error from baz
45
+ examples/simple.rb:30:in `baz'
46
+ examples/simple.rb:25:in `bar'
47
+ examples/simple.rb:18:in `foo'
48
+ examples/simple.rb:35:in `(root)'
49
+
50
+ See more in {log4jruby/examples}[http://github.com/lenny/log4jruby/tree/master/examples/].
51
+
52
+ === Installation
53
+
54
+ gem install log4jruby
55
+
56
+ === Usage
57
+
58
+ class MyClass
59
+ enable_logger
60
+
61
+ class << self
62
+ def my_class_method
63
+ logger.info("hello from class method")
64
+ end
65
+ end
66
+
67
+ def my_method
68
+ logger.info("hello from instance method")
69
+ end
70
+ end
71
+
72
+ INFO jruby.MyModule.A my_class_method:14 - hello from class method
73
+ INFO jruby.MyModule.A my_method:19 - hello from instance method
74
+
75
+ See more in {log4jruby/examples}[http://github.com/lenny/log4jruby/tree/master/examples/].
76
+ They should be runnable from the source.
77
+
78
+ === Rails
79
+
80
+ First try to add the following to one of your config/environment* files.
81
+
82
+ require 'log4jruby'
83
+ config.logger = Log4jruby::Logger.get('MyApp')
84
+
85
+ Depending on your runtime environment and how it differs between development and production that may not work for you
86
+ due to CLASSPATH issues. Alternatively you may add the following to one of your config/initializers/* files.
87
+
88
+ require 'log4jruby/rails'
89
+ Log4jruby::Rails.configure do |c|
90
+ c.logger_name = 'MyApp'
91
+ end
@@ -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 accepts 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
@@ -0,0 +1,3 @@
1
+ module Log4jruby
2
+ VERSION = '0.5.0'
3
+ end
data/lib/log4jruby.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'log4jruby/logger'
2
+ require 'log4jruby/logger_for_class'
3
+
4
+ module Log4jruby
5
+
6
+ end
7
+
8
+ Object.class_eval do
9
+ class << self
10
+ def enable_logger
11
+ send(:include, Log4jruby::LoggerForClass)
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log4jruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.5.0
6
+ platform: java
7
+ authors:
8
+ - Lenny Marks
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-11-25 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: 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.
17
+ email:
18
+ - lenny@aps.org
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/log4jruby.rb
27
+ - lib/log4jruby/log4j_args.rb
28
+ - lib/log4jruby/logger.rb
29
+ - lib/log4jruby/logger_for_class.rb
30
+ - lib/log4jruby/rails.rb
31
+ - lib/log4jruby/version.rb
32
+ - README.rdoc
33
+ - History.txt
34
+ homepage: https://github.com/lenny/log4jruby
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.24
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Log4jruby is a thin wrapper around the Log4j Logger. It is geared more toward those who are using JRuby to integrate with and build on top of Java code that uses Log4j. The Log4jruby::Logger provides an interface much like the standard ruby Logger. Your ruby loggers are given a .jruby prefix and can be configured along with the loggers for your Java code.
61
+ test_files: []
62
+