tagged_logging 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +15 -50
- data/lib/tagged_logging/formatter.rb +9 -0
- data/lib/tagged_logging/logger.rb +14 -50
- data/lib/tagged_logging/version.rb +1 -1
- data/test/logger_test.rb +1 -7
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# TaggedLogging
|
1
|
+
# TaggedLogging [![Build Status](https://travis-ci.org/ketan/tagged-logging.png?branch=master)](https://travis-ci.org/ketan/tagged-logging)
|
2
2
|
|
3
|
-
The rails tagged logger is awesome, but it's only available in rails. This gem makes it available for non-rails applications
|
3
|
+
The rails tagged logger is awesome, but it's only available in rails. This gem makes it available for non-rails applications.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -19,72 +19,37 @@ Or install it yourself as:
|
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
```{ruby}
|
22
|
-
|
23
|
-
include TaggedLogging
|
24
|
-
push_tags(MyApplication, Process.pid)
|
22
|
+
logger = TaggedLogging.new(Logger.new(STDERR))
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
def perform
|
31
|
-
tagged("Perform") do
|
32
|
-
info("performing some task")
|
33
|
-
end
|
34
|
-
end
|
24
|
+
logger.tagged('MyApplication', Process.pid) do |l|
|
25
|
+
l.debug 'Initializing application'
|
35
26
|
|
36
|
-
|
37
|
-
info(
|
27
|
+
tagged("Perform") do
|
28
|
+
info("performing some task")
|
38
29
|
end
|
39
30
|
end
|
40
|
-
|
41
|
-
app = MyApplication.new
|
42
|
-
app.perform
|
43
31
|
```
|
44
32
|
|
45
33
|
The above will print the following:
|
46
34
|
|
47
35
|
[2012-12-15T14:52:10+05:30] - INFO - [MyApplication] [11321] - Initializing application
|
48
36
|
[2012-12-15T14:52:10+05:30] - INFO - [MyApplication] [11321] [Perform] - performing some task
|
49
|
-
[2012-12-15T14:52:10+05:30] - INFO - [MyApplication] [11321] - Exiting application
|
50
|
-
|
51
|
-
If you'd rather prefer to not pollute your class with the logger methods:
|
52
|
-
```{ruby}
|
53
|
-
class MyApplication
|
54
|
-
class MyLogger
|
55
|
-
include TaggedLogging
|
56
|
-
end
|
57
|
-
|
58
|
-
class <<self
|
59
|
-
attr_accessor :logger
|
60
|
-
end
|
61
37
|
|
62
|
-
|
63
|
-
self.class.logger
|
64
|
-
end
|
38
|
+
You can also push and pop tags as required to the same effect
|
65
39
|
|
66
|
-
|
67
|
-
logger.
|
40
|
+
```{ruby}
|
41
|
+
logger = TaggedLogging.new(Logger.new(STDERR))
|
68
42
|
|
69
|
-
|
70
|
-
|
71
|
-
end
|
43
|
+
logger.push_tags('MyApplication', Po)
|
44
|
+
l.debug 'Initializing application'
|
72
45
|
|
73
|
-
|
74
|
-
|
75
|
-
logger.info("performing some task")
|
46
|
+
tagged("Perform") do
|
47
|
+
info("performing some task")
|
76
48
|
end
|
77
49
|
end
|
78
|
-
|
79
|
-
at_exit do
|
80
|
-
logger.info('Exiting application')
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
app = MyApplication.new
|
85
|
-
app.perform
|
86
50
|
```
|
87
51
|
|
52
|
+
|
88
53
|
## Contributing
|
89
54
|
|
90
55
|
1. Fork it
|
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'time'
|
2
|
+
require 'tagged_logging/blank_ext'
|
3
|
+
|
2
4
|
module TaggedLogging
|
3
5
|
class Formatter < ::Logger::Formatter
|
4
6
|
|
@@ -17,6 +19,13 @@ module TaggedLogging
|
|
17
19
|
FORMAT % [format_datetime(time), severity, tags_text, msg]
|
18
20
|
end
|
19
21
|
|
22
|
+
def tagged(*tags)
|
23
|
+
new_tags = push_tags(*tags)
|
24
|
+
yield self
|
25
|
+
ensure
|
26
|
+
pop_tags(tags.size)
|
27
|
+
end
|
28
|
+
|
20
29
|
def push_tags(*tags)
|
21
30
|
tags.flatten.reject(&:blank?).tap do |new_tags|
|
22
31
|
current_tags.concat new_tags
|
@@ -1,61 +1,25 @@
|
|
1
1
|
require 'logger'
|
2
|
+
require 'tagged_logging/formatter'
|
2
3
|
|
3
4
|
module TaggedLogging
|
4
|
-
def self.included(receiver)
|
5
|
-
receiver.class_eval do
|
6
|
-
extend ClassMethods
|
7
|
-
include InstanceMethods
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
module ClassMethods
|
12
|
-
def tagged(*new_tags, &block)
|
13
|
-
new_tags = push_tags(*new_tags)
|
14
|
-
yield(self)
|
15
|
-
ensure
|
16
|
-
pop_tags(new_tags.size)
|
17
|
-
end
|
18
|
-
|
19
|
-
[:push_tags, :pop_tags, :clear_tags!].each do |method_name|
|
20
|
-
define_method(method_name) do |*args, &block|
|
21
|
-
logger.formatter.send(method_name, *args, &block)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def logger
|
26
|
-
@@logger ||= ::Logger.new(STDOUT).tap do |l|
|
27
|
-
l.level = ::Logger::INFO
|
28
|
-
l.formatter = ::TaggedLogging::Formatter.new
|
29
|
-
end
|
30
|
-
end
|
31
5
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
def flush
|
37
|
-
clear_tags!
|
38
|
-
logger.flush if defined?(logger.super)
|
39
|
-
end
|
6
|
+
def self.new(logger)
|
7
|
+
logger.formatter = TaggedLogging::Formatter.new
|
8
|
+
logger.extend(self)
|
9
|
+
end
|
40
10
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
11
|
+
[:push_tags, :pop_tags, :clear_tags!].each do |method_name|
|
12
|
+
define_method(method_name) do |*args, &block|
|
13
|
+
formatter.send(method_name, *args, &block)
|
45
14
|
end
|
46
15
|
end
|
47
16
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
self.class.logger.send(method_name, *args, &block)
|
52
|
-
end
|
53
|
-
end
|
17
|
+
def tagged(*tags)
|
18
|
+
formatter.tagged(*tags) { yield(self) }
|
19
|
+
end
|
54
20
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
21
|
+
def flush
|
22
|
+
clear_tags!
|
23
|
+
super if defined?(super)
|
60
24
|
end
|
61
25
|
end
|
data/test/logger_test.rb
CHANGED
@@ -4,15 +4,9 @@ require 'tagged_logging/blank_ext'
|
|
4
4
|
|
5
5
|
class LoggerTest < Test::Unit::TestCase
|
6
6
|
|
7
|
-
class MyLogger
|
8
|
-
include TaggedLogging
|
9
|
-
end
|
10
|
-
|
11
7
|
setup do
|
12
8
|
@output = StringIO.new
|
13
|
-
@logger =
|
14
|
-
@logger.logger = ::Logger.new(@output)
|
15
|
-
@logger.logger.formatter = TaggedLogging::Formatter.new
|
9
|
+
@logger = TaggedLogging.new(::Logger.new(@output))
|
16
10
|
@logger.flush
|
17
11
|
end
|
18
12
|
|