lager 0.2.0.3 → 0.2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +36 -8
- data/VERSION +1 -1
- data/lib/lager.rb +12 -4
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Lager
|
2
2
|
=====
|
3
|
-
Lager is a logging mixin. It is designed to add class methods for logging, via extend
|
3
|
+
Lager is a logging mixin. It is designed to add class methods for logging, via `extend Lager`. It provides a unified logging instance that you can use in both class and instance methods. It is implemented with the familiar [Logger class](http://ruby-doc.org/stdlib-2.0/libdoc/logger/rdoc/Logger.html) from ruby's [stdlib](http://ruby-doc.org/stdlib/). Only one Logger instance is used for the class. Use #log_to to set the log destination and log level from inside or outside the class.
|
4
4
|
|
5
5
|
Usage
|
6
6
|
-----
|
@@ -45,13 +45,18 @@ Right now, Foo is spewing debug messages everywhere:
|
|
45
45
|
f = Foo.new
|
46
46
|
f.do_something_complicated
|
47
47
|
|
48
|
+
[2013-07-05 15:14:52] DEBUG: baz 15 is a Fixnum, not a string
|
49
|
+
[2013-07-05 15:14:52] DEBUG: about to do something complicated
|
50
|
+
[2013-07-05 15:14:52] DEBUG: whew! we made it!
|
51
|
+
|
52
|
+
|
48
53
|
This is because we set the default logging to :debug level, above:
|
49
54
|
|
50
55
|
class Foo
|
51
56
|
extend Lager
|
52
57
|
log_to $stdout, :debug # sets up @lager at the class layer
|
53
58
|
|
54
|
-
|
59
|
+
Let's calm things down a bit, shall we?
|
55
60
|
|
56
61
|
Foo.log_level :warn
|
57
62
|
Foo.new.do_something_complicated
|
@@ -60,25 +65,25 @@ We can tell Foo to log to a file:
|
|
60
65
|
|
61
66
|
Foo.log_to '/tmp/foo.log'
|
62
67
|
|
63
|
-
Note that this will
|
68
|
+
Note that this will replace the class's Logger instance. The old log level will be maintained unless you specify a new one.
|
64
69
|
|
65
70
|
Best practices
|
66
71
|
--------------
|
67
|
-
* Set default logging inside the class definition by calling log_to
|
72
|
+
* Set default logging inside the class definition by calling log_to just after extend Lager
|
68
73
|
* Set the instance layer's @lager within #initialize
|
69
74
|
* Only call message methods (debug, info, warn, error, fatal) on @lager in your class and instance methods.
|
70
75
|
* Beyond the class default, let the log destination and log level be managed from the outside, by the users of your class.
|
71
76
|
|
72
|
-
|
77
|
+
For Logger, generally: use block invocation of message methods.
|
73
78
|
|
74
|
-
debug { "hi" }
|
79
|
+
@lager.debug { "hi" }
|
75
80
|
# rather than
|
76
|
-
debug "hi"
|
81
|
+
@lager.debug "hi"
|
77
82
|
|
78
83
|
# By using the first form, the block will not be evaluated unless we
|
79
84
|
# are logging at DEBUG level.
|
80
85
|
# If using the second form, the message is evaluated no matter the current
|
81
|
-
# log level.
|
86
|
+
# log level. This can be significant when logging complicated messages.
|
82
87
|
|
83
88
|
Artifacts
|
84
89
|
---------
|
@@ -92,3 +97,26 @@ Artifacts
|
|
92
97
|
Now you have a unified interface for logging at both class and instance layers.
|
93
98
|
|
94
99
|
@lager.info { "So happy right now!" }
|
100
|
+
|
101
|
+
Use an existing Logger instance
|
102
|
+
-------------------------------
|
103
|
+
If your project already has an existing Logger, then you can set your class to use that Logger:
|
104
|
+
|
105
|
+
class Foo
|
106
|
+
extend Lager
|
107
|
+
log_to $LOG # the global Logger instance
|
108
|
+
# ...
|
109
|
+
end
|
110
|
+
|
111
|
+
Of course, $LOG will have to have already been defined at requiretime. You can set it the same way at runtime:
|
112
|
+
|
113
|
+
class Foo
|
114
|
+
extend Lager
|
115
|
+
log_to $stderr
|
116
|
+
# ...
|
117
|
+
end
|
118
|
+
|
119
|
+
# now, in an irb session or another file
|
120
|
+
# where Project.log is your project's Logger:
|
121
|
+
|
122
|
+
Foo.log_to Project.log
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.0.
|
1
|
+
0.2.0.4
|
data/lib/lager.rb
CHANGED
@@ -8,16 +8,19 @@ require 'logger'
|
|
8
8
|
# ...
|
9
9
|
# end
|
10
10
|
#
|
11
|
-
# It provides the class instance variable @lager, which may be used in class
|
11
|
+
# It provides the class instance variable @lager, which may be used in class
|
12
|
+
# methods, e.g.
|
12
13
|
#
|
13
|
-
#
|
14
|
+
# def self.foo
|
15
|
+
# @lager.debug { "example log message" }
|
16
|
+
# end
|
14
17
|
#
|
15
18
|
# Note that using the block invocation means that the block contents are
|
16
19
|
# not evaluated if the log level is above the message level.
|
17
20
|
#
|
18
21
|
# Make sure to call log_to within the class definition, so that class methods
|
19
|
-
# will already have @lager defined.
|
20
|
-
|
22
|
+
# will already have @lager defined at requiretime.
|
23
|
+
#
|
21
24
|
# For instance methods, you need to set @lager directly, within initialize
|
22
25
|
# Note: the instance layer and class layer each have their own independent
|
23
26
|
# @lager
|
@@ -40,11 +43,16 @@ module Lager
|
|
40
43
|
end
|
41
44
|
|
42
45
|
# create @lager
|
46
|
+
# if passed a Logger instance, set @lager to that instance and return
|
43
47
|
# supports IO and String (filename, presumably) for log destination
|
44
48
|
# (passed straight through to Logger.new)
|
45
49
|
# supports symbols, strings, and integers for log level
|
46
50
|
#
|
47
51
|
def log_to(dest = $stderr, level = nil)
|
52
|
+
if dest.is_a?(Logger)
|
53
|
+
@lager = dest
|
54
|
+
return
|
55
|
+
end
|
48
56
|
# use the old @lager's level by default, as appropriate
|
49
57
|
level ||= (defined?(@lager) ? @lager.level : :warn)
|
50
58
|
@lager = Logger.new dest
|