lager 0.2.0.1 → 0.2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +81 -12
- data/VERSION +1 -1
- data/lib/lager.rb +3 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,25 +1,94 @@
|
|
1
1
|
Lager
|
2
2
|
=====
|
3
|
-
Lager is a logging mixin. It is designed to add class methods for logging
|
4
|
-
|
5
|
-
How it works
|
6
|
-
------------
|
7
|
-
Lager is a mixin. By design, it mixes in class methods.
|
3
|
+
Lager is a logging mixin. It is designed to add class methods for logging, via extend. It aims to provide a unified logging interface that you can use in both class and instance methods. Only one Logger instance is used for this. You are able to set the log destination and log level from within the class, via instantiation, or from outside.
|
8
4
|
|
5
|
+
Usage
|
6
|
+
-----
|
9
7
|
require 'lager'
|
10
8
|
|
11
9
|
class Foo
|
12
10
|
extend Lager
|
13
|
-
|
11
|
+
log_to $stdout, :debug # sets up @lager at the class layer
|
12
|
+
# ...
|
14
13
|
|
15
|
-
Now, within Foo, you can use the class variable
|
16
|
-
|
17
|
-
class Foo
|
18
|
-
extend Lager
|
14
|
+
Now, within Foo, you can use the class instance variable @lager for logging.
|
19
15
|
|
16
|
+
# ...
|
20
17
|
def self.bar(baz)
|
21
|
-
lager unless defined?(@@lager)
|
22
18
|
unless baz.is_a?(String)
|
23
|
-
|
19
|
+
@lager.debug { "baz #{baz} is a #{baz.class}, not a string" }
|
24
20
|
end
|
25
21
|
end
|
22
|
+
# ...
|
23
|
+
|
24
|
+
What about instance methods, you ask? Well, you will need to assign @lager yourself, within #initialize.
|
25
|
+
|
26
|
+
# ...
|
27
|
+
def initialize
|
28
|
+
# set the instance layer's @lager to the class layer's @lager
|
29
|
+
@lager = self.class.lager
|
30
|
+
# now both layers are using the same instance
|
31
|
+
end
|
32
|
+
|
33
|
+
def do_something_complicated
|
34
|
+
@lager.debug { "about to do something complicated" }
|
35
|
+
# ...
|
36
|
+
@lager.debug { "whew! we made it!" }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
Everything under control
|
41
|
+
------------------------
|
42
|
+
Right now, Foo is spewing debug messages everywhere:
|
43
|
+
|
44
|
+
Foo.bar(15)
|
45
|
+
f = Foo.new
|
46
|
+
f.do_something_complicated
|
47
|
+
|
48
|
+
This is because we set the default logging to :debug level, above:
|
49
|
+
|
50
|
+
class Foo
|
51
|
+
extend Lager
|
52
|
+
log_to $stdout, :debug # sets up @lager at the class layer
|
53
|
+
|
54
|
+
Now let's calm things down:
|
55
|
+
|
56
|
+
Foo.log_level :warn
|
57
|
+
Foo.new.do_something_complicated
|
58
|
+
|
59
|
+
We can tell Foo to log to a file:
|
60
|
+
|
61
|
+
Foo.log_to '/tmp/foo.log'
|
62
|
+
|
63
|
+
Note that this will create a new Logger instance. The old log level will be maintained unless you specify a new one.
|
64
|
+
|
65
|
+
Best practices
|
66
|
+
--------------
|
67
|
+
* Set default logging inside the class definition by calling log_to
|
68
|
+
* Set the instance layer's @lager within #initialize
|
69
|
+
* Only call message methods (debug, info, warn, error, fatal) on @lager in your class and instance methods.
|
70
|
+
* Beyond the class default, let the log destination and log level be managed from the outside, by the users of your class.
|
71
|
+
|
72
|
+
Use block invocation of message methods
|
73
|
+
|
74
|
+
debug { "hi" }
|
75
|
+
# rather than
|
76
|
+
debug "hi"
|
77
|
+
|
78
|
+
# By using the first form, the block will not be evaluated unless we
|
79
|
+
# are logging at DEBUG level.
|
80
|
+
# If using the second form, the message is evaluated no matter the current
|
81
|
+
# log level.
|
82
|
+
|
83
|
+
Artifacts
|
84
|
+
---------
|
85
|
+
* By mixing in Lager via extend, you introduce these class methods:
|
86
|
+
* lager
|
87
|
+
* log_to
|
88
|
+
* log_level
|
89
|
+
* By calling log_to, you introduce the class instance variable @lager
|
90
|
+
* By assigning @lager within initialize, you introduce the instance variable @lager
|
91
|
+
|
92
|
+
Now you have a unified interface for logging at both class and instance layers.
|
93
|
+
|
94
|
+
@lager.info { "So happy right now!" }
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.0.
|
1
|
+
0.2.0.2
|
data/lib/lager.rb
CHANGED
@@ -44,7 +44,9 @@ module Lager
|
|
44
44
|
# (passed straight through to Logger.new)
|
45
45
|
# supports symbols, strings, and integers for log level
|
46
46
|
#
|
47
|
-
def log_to(dest = $stderr, level =
|
47
|
+
def log_to(dest = $stderr, level = nil)
|
48
|
+
# use the old @lager's level by default, as appropriate
|
49
|
+
level ||= (defined?(@lager) ? @lager.level : :warn)
|
48
50
|
@lager = Logger.new dest
|
49
51
|
@lager.formatter = proc { |sev, time, progname, msg|
|
50
52
|
line = "[#{time.strftime('%Y-%m-%d %H:%M:%S')}] #{sev.to_s.upcase}: "
|