lager 1.1.0.1 → 1.1.2.1
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/README.md +95 -65
- data/VERSION +1 -1
- data/lib/lager.rb +1 -10
- metadata +1 -1
data/README.md
CHANGED
@@ -4,66 +4,71 @@ Lager is a logging mixin. It is designed to add class methods for logging, via
|
|
4
4
|
|
5
5
|
Usage
|
6
6
|
-----
|
7
|
-
|
7
|
+
```ruby
|
8
|
+
require 'lager'
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
class Foo
|
11
|
+
extend Lager
|
12
|
+
log_to $stdout, :debug # sets up @lager at the class layer
|
13
|
+
# ...
|
14
|
+
```
|
13
15
|
|
14
16
|
Now, within Foo, you can use the class instance variable @lager for logging.
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
```ruby
|
19
|
+
# ...
|
20
|
+
def self.bar(baz)
|
21
|
+
unless baz.is_a?(String)
|
22
|
+
@lager.debug { "baz #{baz} is a #{baz.class}, not a string" }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
# ...
|
26
|
+
```
|
23
27
|
|
24
28
|
What about instance methods, you ask? Well, you will need to assign @lager yourself, within #initialize.
|
25
29
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
```ruby
|
31
|
+
# ...
|
32
|
+
def initialize
|
33
|
+
# set the instance layer's @lager to the class layer's @lager
|
34
|
+
@lager = self.class.lager
|
35
|
+
# now both layers are using the same instance
|
36
|
+
end
|
37
|
+
|
38
|
+
def do_something_complicated
|
39
|
+
@lager.debug { "about to do something complicated" }
|
40
|
+
# ...
|
41
|
+
@lager.debug { "whew! we made it!" }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
39
45
|
|
40
46
|
Everything under control
|
41
47
|
------------------------
|
42
48
|
Right now, Foo is spewing debug messages everywhere:
|
43
49
|
|
44
|
-
|
45
|
-
|
46
|
-
|
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!
|
50
|
+
```ruby
|
51
|
+
Foo.bar(15)
|
52
|
+
f = Foo.new
|
53
|
+
f.do_something_complicated
|
51
54
|
|
55
|
+
[2013-07-05 15:14:52] DEBUG: baz 15 is a Fixnum, not a string
|
56
|
+
[2013-07-05 15:14:52] DEBUG: about to do something complicated
|
57
|
+
[2013-07-05 15:14:52] DEBUG: whew! we made it!
|
58
|
+
```
|
52
59
|
|
53
|
-
This is because we set the default logging to :debug level, above
|
60
|
+
This is because we set the default logging to :debug level, above. Let's calm things down a bit, shall we?
|
54
61
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
Let's calm things down a bit, shall we?
|
60
|
-
|
61
|
-
Foo.log_level :warn
|
62
|
-
Foo.new.do_something_complicated
|
62
|
+
```ruby
|
63
|
+
Foo.log_level = :warn
|
64
|
+
Foo.new.do_something_complicated
|
65
|
+
```
|
63
66
|
|
64
67
|
We can tell Foo to log to a file:
|
65
68
|
|
66
|
-
|
69
|
+
```ruby
|
70
|
+
Foo.log_to '/tmp/foo.log'
|
71
|
+
```
|
67
72
|
|
68
73
|
Note that this will replace the class's Logger instance. The old log level will be maintained unless you specify a new one.
|
69
74
|
|
@@ -76,14 +81,16 @@ Best practices
|
|
76
81
|
|
77
82
|
For Logger, generally: use block invocation of message methods.
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
|
84
|
+
```ruby
|
85
|
+
@lager.debug { "hi" }
|
86
|
+
# rather than
|
87
|
+
@lager.debug "hi"
|
82
88
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
89
|
+
# By using the first form, the block will not be evaluated unless we
|
90
|
+
# are logging at DEBUG level.
|
91
|
+
# If using the second form, the message is evaluated no matter the current
|
92
|
+
# log level. This can be significant when logging complicated messages.
|
93
|
+
```
|
87
94
|
|
88
95
|
Artifacts
|
89
96
|
---------
|
@@ -91,32 +98,55 @@ Artifacts
|
|
91
98
|
* lager
|
92
99
|
* log_to
|
93
100
|
* log_level
|
94
|
-
*
|
95
|
-
* By
|
101
|
+
* log_level=
|
102
|
+
* By calling log_to, you introduce the *class instance variable* @lager
|
103
|
+
* By assigning @lager within initialize, you introduce the *instance variable* @lager
|
96
104
|
|
97
105
|
Now you have a unified interface for logging at both class and instance layers.
|
98
106
|
|
99
|
-
|
107
|
+
```ruby
|
108
|
+
@lager.info { "So happy right now!" }
|
109
|
+
```
|
100
110
|
|
101
111
|
Use an existing Logger instance
|
102
112
|
-------------------------------
|
103
113
|
If your project already has an existing Logger, then you can set your class to use that Logger:
|
104
114
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
115
|
+
```ruby
|
116
|
+
class Foo
|
117
|
+
extend Lager
|
118
|
+
log_to $LOG # the global Logger instance
|
119
|
+
# ...
|
120
|
+
end
|
121
|
+
```
|
110
122
|
|
111
123
|
Of course, $LOG will have to have already been defined at requiretime. You can set it the same way at runtime:
|
112
124
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
125
|
+
```ruby
|
126
|
+
class Foo
|
127
|
+
extend Lager
|
128
|
+
log_to $stderr
|
129
|
+
# ...
|
130
|
+
end
|
131
|
+
|
132
|
+
# now, in an irb session or another file
|
133
|
+
# where Project.log is your project's Logger:
|
134
|
+
|
135
|
+
Foo.log_to Project.log
|
136
|
+
```
|
137
|
+
|
138
|
+
Inheritance
|
139
|
+
-----------
|
140
|
+
```ruby
|
141
|
+
class Foo
|
142
|
+
extend Lager
|
143
|
+
end
|
144
|
+
|
145
|
+
class Bar < Foo
|
146
|
+
end
|
118
147
|
|
119
|
-
|
120
|
-
|
148
|
+
# Bar will have Lager mixed in. Its @lager is independent of Foo's.
|
149
|
+
# You can set Bar's to Foo's
|
121
150
|
|
122
|
-
|
151
|
+
Bar.log_to Foo.lager
|
152
|
+
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.2.1
|
data/lib/lager.rb
CHANGED
@@ -15,9 +15,6 @@ require 'logger'
|
|
15
15
|
# @lager.debug { "example log message" }
|
16
16
|
# end
|
17
17
|
#
|
18
|
-
# Note that using the block invocation means that the block contents are
|
19
|
-
# not evaluated if the log level is above the message level.
|
20
|
-
#
|
21
18
|
# Make sure to call log_to within the class definition, so that class methods
|
22
19
|
# will already have @lager defined at requiretime.
|
23
20
|
#
|
@@ -30,12 +27,6 @@ require 'logger'
|
|
30
27
|
# @lager = self.class.lager
|
31
28
|
# end
|
32
29
|
#
|
33
|
-
# Outside of initialize or a call to log_to within the class definition,
|
34
|
-
# you should only ever call the message methods: debug, info, warn, error,
|
35
|
-
# and fatal within your class code.
|
36
|
-
#
|
37
|
-
# Let the destination and log level be managed from outside.
|
38
|
-
#
|
39
30
|
module Lager
|
40
31
|
def self.version
|
41
32
|
file = File.expand_path('../../VERSION', __FILE__)
|
@@ -59,7 +50,7 @@ module Lager
|
|
59
50
|
line << "(#{progname})" if progname
|
60
51
|
line << " #{msg}\n"
|
61
52
|
}
|
62
|
-
log_level = level
|
53
|
+
self.log_level = level
|
63
54
|
dest # don't expose @lager here
|
64
55
|
end
|
65
56
|
|