jlogger 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/CHANGES +15 -0
- data/LICENSE +20 -0
- data/README.md +66 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/jlogger.rb +253 -0
- data/spec/jlogger_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +122 -0
data/.document
ADDED
data/.gitignore
ADDED
data/CHANGES
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
0.0.4
|
2
|
+
Switch to underlying jlogger, not slf logger, to allow parameterized logging
|
3
|
+
|
4
|
+
0.0.3
|
5
|
+
Allow JLogger::Simple to act as a rootlogger (e.g., JLogger::Simple.info "hello",
|
6
|
+
JLogger::Simple.startConsole, etc. will all work.)
|
7
|
+
|
8
|
+
Provide a class-level 'log' method (e.g., MyClass.log.info 'hello') for use inside
|
9
|
+
class methods. (def self.hello; log 'hello'; end)
|
10
|
+
|
11
|
+
0.0.2
|
12
|
+
First gem
|
13
|
+
|
14
|
+
0.0.1
|
15
|
+
First release
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 BillDueber
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# jlogger
|
2
|
+
|
3
|
+
A simple wrapper around rjack-logback to make your logging easy.
|
4
|
+
|
5
|
+
|
6
|
+
I wanted a way to just include a module and automatically get logging that integrated with the Java logging I already had. rjack-logback provided all the tools for me to make one.
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
|
10
|
+
require 'jlogger'
|
11
|
+
|
12
|
+
class MyClass
|
13
|
+
include JLogger::Simple
|
14
|
+
def hello
|
15
|
+
log.info "Calling hello" # 'log' method thanks to JLogger::Simple
|
16
|
+
puts 'Hello'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
rootlog = JLogger::RootLogger.new
|
22
|
+
|
23
|
+
# Start some log output. By default, NO logging is done!
|
24
|
+
|
25
|
+
rootlog.startConsole # starts logging on STDERR
|
26
|
+
rootlog.startFile 'mylog.log' # starts logging to the file
|
27
|
+
|
28
|
+
|
29
|
+
c = MyClass.new
|
30
|
+
|
31
|
+
rootlog.info 'Hello' # logs to both STDERR and the file mylog.log
|
32
|
+
c.hello # ditto
|
33
|
+
|
34
|
+
rootlog.loglevel = :warn # use :debug, :info, :warn, :error, or :off
|
35
|
+
|
36
|
+
rootlog.info 'hello' # doesn't log to anywhere
|
37
|
+
c.hello # ditto
|
38
|
+
|
39
|
+
MyClass.loglevel = :info # set it just for this class and its subclasses
|
40
|
+
|
41
|
+
rootlog.info 'hello' # doesn't log to anywhere
|
42
|
+
c.hello # logs again
|
43
|
+
|
44
|
+
rootlog.stopFile('mylog.log') # stop logging to the file
|
45
|
+
rootlog.stopConsole # stop logging to STDERR
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
## Bugs
|
51
|
+
|
52
|
+
I can't seem to write a good wrapper to allow the use of File handles (converted to Outputstreams); that's something I would really like.
|
53
|
+
|
54
|
+
## Note on Patches/Pull Requests
|
55
|
+
|
56
|
+
* Fork the project.
|
57
|
+
* Make your feature addition or bug fix.
|
58
|
+
* Add tests for it. This is important so I don't break it in a
|
59
|
+
future version unintentionally.
|
60
|
+
* Commit, do not mess with rakefile, version, or history.
|
61
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
62
|
+
* Send me a pull request. Bonus points for topic branches.
|
63
|
+
|
64
|
+
== Copyright
|
65
|
+
|
66
|
+
Copyright (c) 2010 BillDueber. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "jlogger"
|
8
|
+
gem.summary = %Q{Simple wrapper around rjack-logback}
|
9
|
+
gem.description = %Q{Include in your modules to get a defaul 'log' object; then use it to log stuff}
|
10
|
+
gem.email = "bill@dueber.com"
|
11
|
+
gem.homepage = "http://github.com/billdueber/jlogger"
|
12
|
+
gem.authors = ["BillDueber"]
|
13
|
+
|
14
|
+
gem.add_dependency "rjack-logback"
|
15
|
+
|
16
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
17
|
+
gem.add_development_dependency "yard", ">= 0"
|
18
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'spec/rake/spectask'
|
26
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
32
|
+
spec.libs << 'lib' << 'spec'
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :spec => :check_dependencies
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
begin
|
42
|
+
require 'yard'
|
43
|
+
YARD::Rake::YardocTask.new
|
44
|
+
rescue LoadError
|
45
|
+
task :yardoc do
|
46
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
47
|
+
end
|
48
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.4
|
data/lib/jlogger.rb
ADDED
@@ -0,0 +1,253 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rjack-logback'
|
3
|
+
|
4
|
+
|
5
|
+
# Redefine how to get logger names
|
6
|
+
module RJack
|
7
|
+
module SLF4J
|
8
|
+
def self.to_log_name classobject
|
9
|
+
classline = []
|
10
|
+
parent_is_a_logback = false
|
11
|
+
loggingname = 'none'
|
12
|
+
ancestors = []
|
13
|
+
classobject.ancestors.each do |c|
|
14
|
+
c = c.to_s
|
15
|
+
if c == "JLogger::Simple"
|
16
|
+
break
|
17
|
+
end
|
18
|
+
if c == "Object"
|
19
|
+
break;
|
20
|
+
end
|
21
|
+
classline.unshift c.split('::')
|
22
|
+
end
|
23
|
+
loggingname = classline.flatten.join('.')
|
24
|
+
return loggingname
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Allow access to teh underlying java logger
|
30
|
+
module RJack
|
31
|
+
module Logback
|
32
|
+
class Logger
|
33
|
+
attr_reader :jlogger
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
# Create an outputstream logger
|
40
|
+
# Whoops. Nothing happens, and I don't know why. Don't worry about it for now.
|
41
|
+
|
42
|
+
# module RJack
|
43
|
+
# module Logback
|
44
|
+
# class StreamAppender < Java::ch.qos.logback.core.OutputStreamAppender
|
45
|
+
# include AppenderUtil
|
46
|
+
#
|
47
|
+
# # Sets defaults, yields self to block, and calls self.start
|
48
|
+
# def initialize( ioobject, &block )
|
49
|
+
# super()
|
50
|
+
# set_defaults
|
51
|
+
# self.setOutputStream(ioobject.to_outputstream)
|
52
|
+
# self.encoding = "UTF-8"
|
53
|
+
# finish( &block )
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
|
60
|
+
|
61
|
+
module JLogger
|
62
|
+
module Simple
|
63
|
+
include RJack::Logback
|
64
|
+
|
65
|
+
Level = Java::ch.qos.logback.classic.Level
|
66
|
+
LEVELS = {
|
67
|
+
# :off => RJack::Logback::OFF,
|
68
|
+
:off => Level::OFF,
|
69
|
+
:trace => TRACE,
|
70
|
+
:debug => DEBUG,
|
71
|
+
:info => INFO,
|
72
|
+
:warn => WARN,
|
73
|
+
:error => ERROR,
|
74
|
+
}
|
75
|
+
|
76
|
+
def self.method_missing(meth, *args)
|
77
|
+
@root.send(meth, *args)
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
# On inclusion, we set up a logger for this class
|
83
|
+
|
84
|
+
def self.included klass
|
85
|
+
@root ||= JLogger::RootLogger.new
|
86
|
+
|
87
|
+
class << klass
|
88
|
+
attr_accessor :_slflogger, :_logobject
|
89
|
+
|
90
|
+
def createlogger
|
91
|
+
RJack::Logback.root.jlogger.detachAppender('console')
|
92
|
+
@_slflogger = RJack::SLF4J[self]
|
93
|
+
@_logobject = RJack::Logback[@_slflogger.name]
|
94
|
+
@_slflogger = @_logobject.jlogger
|
95
|
+
end
|
96
|
+
|
97
|
+
def log
|
98
|
+
createlogger unless @_slflogger
|
99
|
+
return @_slflogger
|
100
|
+
end
|
101
|
+
|
102
|
+
def loglevel= level
|
103
|
+
self.createlogger unless @_slflogger
|
104
|
+
|
105
|
+
level = level.to_s.downcase.to_sym
|
106
|
+
unless LEVELS.has_key? level
|
107
|
+
raise ArgumentError, "'#{level}' is an invalid loglevel"
|
108
|
+
end
|
109
|
+
|
110
|
+
@_logobject.level = LEVELS[level]
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
# The log object. First thing we do is try to
|
118
|
+
# get a logger from the class; if we can't, go
|
119
|
+
# ahead and build one
|
120
|
+
|
121
|
+
def log
|
122
|
+
unless self.class._slflogger
|
123
|
+
self.class.createlogger
|
124
|
+
end
|
125
|
+
return self.class._slflogger
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
# The root logger
|
136
|
+
|
137
|
+
module JLogger
|
138
|
+
class RootLogger
|
139
|
+
include JLogger::Simple
|
140
|
+
PATTERN = RJack::Logback::PatternLayout.new("%-5level\t%date{HH:mm:ss}\t%5r\t%-30logger\t%msg\n")
|
141
|
+
|
142
|
+
def initialize
|
143
|
+
self.class._logobject = RJack::Logback.root
|
144
|
+
self.class._slflogger = self.class._logobject.jlogger
|
145
|
+
RJack::Logback.root.jlogger.detachAppender('console')
|
146
|
+
end
|
147
|
+
|
148
|
+
# The logging methods
|
149
|
+
|
150
|
+
def debug *args
|
151
|
+
log.debug *args
|
152
|
+
end
|
153
|
+
|
154
|
+
def info *args
|
155
|
+
log.info *args
|
156
|
+
end
|
157
|
+
|
158
|
+
def warn *args
|
159
|
+
log.warn *args
|
160
|
+
end
|
161
|
+
|
162
|
+
def error *args
|
163
|
+
log.error *args
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
def debug?
|
168
|
+
return log.debug_enabled?
|
169
|
+
end
|
170
|
+
|
171
|
+
def info?
|
172
|
+
return log.info_enabled?
|
173
|
+
end
|
174
|
+
|
175
|
+
def warn?
|
176
|
+
return log.warn_enabled?
|
177
|
+
end
|
178
|
+
|
179
|
+
def error?
|
180
|
+
return log.error_enabled?
|
181
|
+
end
|
182
|
+
|
183
|
+
# Set the log level
|
184
|
+
# Why do I need to redefine this here? self.class.loglevel = level fails with
|
185
|
+
# method not found.s
|
186
|
+
|
187
|
+
def loglevel= level
|
188
|
+
self.class.createlogger unless self.class._logobject
|
189
|
+
level = level.to_s.downcase.to_sym
|
190
|
+
unless JLogger::Simple::LEVELS.has_key? level
|
191
|
+
raise ArgumentError, "'#{level}' is an invalid loglevel"
|
192
|
+
end
|
193
|
+
|
194
|
+
self.class._logobject.level = JLogger::Simple::LEVELS[level]
|
195
|
+
end
|
196
|
+
|
197
|
+
def startConsole pattern = nil
|
198
|
+
return if RJack::Logback.root.jlogger.getAppender('jlconsole')
|
199
|
+
jlconsole = RJack::Logback::ConsoleAppender.new do |a|
|
200
|
+
a.target = "System.err"
|
201
|
+
a.name = 'jlconsole'
|
202
|
+
a.layout = pattern || PATTERN
|
203
|
+
end
|
204
|
+
RJack::Logback.root.add_appender( jlconsole )
|
205
|
+
end
|
206
|
+
|
207
|
+
def stopConsole
|
208
|
+
RJack::Logback.root.jlogger.detachAppender('jlconsole')
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
def startFile file, pattern = nil
|
214
|
+
|
215
|
+
# Get the ID and class to use
|
216
|
+
|
217
|
+
id = nil
|
218
|
+
mod = nil
|
219
|
+
if file.is_a? String
|
220
|
+
id = file
|
221
|
+
mod = RJack::Logback::FileAppender
|
222
|
+
# elsif file.respond_to? :to_outputstream
|
223
|
+
# id = 'objid' + file.object_id.to_s
|
224
|
+
# mod = RJack::Logback::StreamAppender
|
225
|
+
else
|
226
|
+
raise ArgumentError, "#{file} is not a valid target for file logging. Must be a filename"
|
227
|
+
end
|
228
|
+
|
229
|
+
return if RJack::Logback.root.jlogger.getAppender(id)
|
230
|
+
|
231
|
+
app = mod.new(file) do |a|
|
232
|
+
a.name = id
|
233
|
+
a.layout = pattern || PATTERN
|
234
|
+
end
|
235
|
+
RJack::Logback.root.add_appender( app )
|
236
|
+
end
|
237
|
+
|
238
|
+
def stopFile file
|
239
|
+
if file.is_a? String
|
240
|
+
id = file
|
241
|
+
# elsif file.respond_to? :to_outputstream
|
242
|
+
# id = 'objid' + file.object_id.to_s
|
243
|
+
else
|
244
|
+
raise ArgumentError, "#{file} is not a valid target for file logging"
|
245
|
+
end
|
246
|
+
RJack::Logback.root.jlogger.detachAppender(id)
|
247
|
+
end
|
248
|
+
|
249
|
+
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jlogger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- BillDueber
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-15 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rjack-logback
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 13
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 2
|
47
|
+
- 9
|
48
|
+
version: 1.2.9
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: yard
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description: Include in your modules to get a defaul 'log' object; then use it to log stuff
|
66
|
+
email: bill@dueber.com
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files:
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
files:
|
75
|
+
- .document
|
76
|
+
- .gitignore
|
77
|
+
- CHANGES
|
78
|
+
- LICENSE
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- VERSION
|
82
|
+
- lib/jlogger.rb
|
83
|
+
- spec/jlogger_spec.rb
|
84
|
+
- spec/spec.opts
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
has_rdoc: true
|
87
|
+
homepage: http://github.com/billdueber/jlogger
|
88
|
+
licenses: []
|
89
|
+
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options:
|
92
|
+
- --charset=UTF-8
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirements: []
|
114
|
+
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.3.7
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Simple wrapper around rjack-logback
|
120
|
+
test_files:
|
121
|
+
- spec/jlogger_spec.rb
|
122
|
+
- spec/spec_helper.rb
|