green_syslogger 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +1 -0
- data/README.md +25 -6
- data/Rakefile +1 -1
- data/green_syslogger.gemspec +3 -3
- data/lib/green_syslogger.rb +96 -31
- data/test/green_syslogger_test.rb +63 -22
- metadata +5 -5
data/Manifest
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,14 @@
|
|
4
4
|
|
5
5
|
Syslogger that makes your life greener
|
6
6
|
|
7
|
+
Custom Rails Logger
|
8
|
+
|
9
|
+
* Use Syslog like back storage
|
10
|
+
* Posibility to use custom 'facility'
|
11
|
+
* Posibility to use custom 'tag'
|
12
|
+
* Posibility to use custom 'tag' only in a concrete point so you can configure Syslog to filter this concrete message to another file
|
13
|
+
* Compatible with `config.logger.auto_flushing = false` so every log of a simple request will be written on an atomic way
|
14
|
+
|
7
15
|
## Install
|
8
16
|
|
9
17
|
$ [sudo] gem install green_syslogger
|
@@ -13,34 +21,45 @@ Syslogger that makes your life greener
|
|
13
21
|
require 'green_syslogger'
|
14
22
|
logger = GreenSyslogger.new
|
15
23
|
logger.debug( 'debug message' )
|
16
|
-
|
17
|
-
To configure it on the <RAILS_ENVIRONMENT>.rb:
|
18
24
|
|
25
|
+
To install it configure it on the <RAILS_ENVIRONMENT>.rb:
|
19
26
|
config.logger = GreenSyslogger.new(<default tag>, <facility>, <level>)
|
20
27
|
|
21
|
-
|
22
28
|
By default
|
23
|
-
|
24
29
|
GreenSyslogger.new('rails', 'local2', :debug)
|
25
30
|
|
31
|
+
Example for Rails configuration:
|
32
|
+
|
33
|
+
config.log_level = :info
|
34
|
+
config.logger = GreenSyslogger.new('myapp', 'local1', config.log_level)
|
35
|
+
config.colorize_logging = false
|
36
|
+
config.logger.auto_flushing = false
|
26
37
|
|
27
38
|
To use it:
|
28
39
|
|
29
40
|
logger.debug("my debug message")
|
30
41
|
logger.error("my error message")
|
31
42
|
|
32
|
-
|
33
43
|
To user another tag:
|
34
44
|
|
35
45
|
logger.custom("my custom message", <tag>, <level>)
|
36
46
|
|
37
|
-
|
38
47
|
By default:
|
39
48
|
|
40
49
|
logger.custom("my custom message", 'custom', :info)
|
41
50
|
|
42
51
|
## TODO
|
43
52
|
|
53
|
+
In the `GreenSyslogger.custom` there are two reopen connections just to be allowed to change the `tag`, could be better to change the `tag` without reconnect.
|
54
|
+
|
55
|
+
There is not `GreenSyslogger.silencer`. Maybe is a good idea to implemented.
|
56
|
+
|
57
|
+
## Change log
|
58
|
+
|
59
|
+
### v 0.2.0
|
60
|
+
|
61
|
+
* Compatible with `config.logger.auto_flushing = false` so every log of a simple request will be written on an atomic way
|
62
|
+
|
44
63
|
|
45
64
|
## Credits
|
46
65
|
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('green_syslogger', '0.
|
5
|
+
Echoe.new('green_syslogger', '0.2.0') do |p|
|
6
6
|
p.description = "Syslogger that makes your life greener"
|
7
7
|
p.url = "https://github.com/SponsorPay/GreenSyslogger"
|
8
8
|
p.author = "Fernando Guillen <http://fernandoguillen.info> & Juan Jose Vidal <@j2vidal>"
|
data/green_syslogger.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{green_syslogger}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.2.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Fernando Guillen <http://fernandoguillen.info> & Juan Jose Vidal <@j2vidal>"]
|
9
|
-
s.date = %q{2010-11-
|
9
|
+
s.date = %q{2010-11-12}
|
10
10
|
s.description = %q{Syslogger that makes your life greener}
|
11
11
|
s.email = %q{fguillen.mail@gmail.com}
|
12
12
|
s.extra_rdoc_files = ["README.md", "lib/green_syslogger.rb"]
|
13
|
-
s.files = ["Manifest", "README.md", "Rakefile", "lib/green_syslogger.rb", "test/green_syslogger_test.rb"
|
13
|
+
s.files = ["Manifest", "README.md", "Rakefile", "green_syslogger.gemspec", "lib/green_syslogger.rb", "test/green_syslogger_test.rb"]
|
14
14
|
s.homepage = %q{https://github.com/SponsorPay/GreenSyslogger}
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Green_syslogger", "--main", "README.md"]
|
16
16
|
s.require_paths = ["lib"]
|
data/lib/green_syslogger.rb
CHANGED
@@ -1,14 +1,27 @@
|
|
1
1
|
require 'syslog'
|
2
2
|
|
3
3
|
#
|
4
|
-
# Custom
|
4
|
+
# Custom Rails Logger
|
5
5
|
#
|
6
|
-
#
|
6
|
+
# * Use Syslog like back storage
|
7
|
+
# * Posibility to use custom 'facility'
|
8
|
+
# * Posibility to use custom 'tag'
|
9
|
+
# * Posibility to use custom 'tag' only in a concrete point so you can configure Syslog to filter this concrete message to another file
|
10
|
+
# * Compatible with `config.logger.auto_flushing = false` so every log of a simple request will be written on an atomic way
|
11
|
+
#
|
12
|
+
# To install it configure it on the <RAILS_ENVIRONMENT>.rb:
|
7
13
|
# config.logger = GreenSyslogger.new(<default tag>, <facility>, <level>)
|
8
14
|
#
|
9
15
|
# By default
|
10
16
|
# GreenSyslogger.new('rails', 'local2', :debug)
|
11
17
|
#
|
18
|
+
# Example for Rails configuration:
|
19
|
+
#
|
20
|
+
# config.log_level = :info
|
21
|
+
# config.logger = GreenSyslogger.new('myapp', 'local1', config.log_level)
|
22
|
+
# config.colorize_logging = false
|
23
|
+
# config.logger.auto_flushing = false
|
24
|
+
#
|
12
25
|
# To use it:
|
13
26
|
#
|
14
27
|
# logger.debug("my debug message")
|
@@ -32,62 +45,114 @@ class GreenSyslogger
|
|
32
45
|
:warn => [:warning , 2],
|
33
46
|
:error => [:err , 3],
|
34
47
|
:fatal => [:emerg , 4],
|
35
|
-
:unknown => [:
|
48
|
+
:unknown => [:debug , 5]
|
36
49
|
}
|
37
50
|
|
38
|
-
|
51
|
+
DEFAULT_MAX_BUFFER_SIZE = 500
|
39
52
|
|
40
53
|
def initialize(tag = 'rails', facility = 'local2', level = :debug)
|
41
54
|
@tag = tag
|
42
55
|
@facility = facility
|
43
56
|
@level = level
|
44
|
-
@
|
57
|
+
@max_buffer_size = 1
|
58
|
+
@buffer = {}
|
59
|
+
@guard = Mutex.new
|
60
|
+
@syslog = Syslog.open(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
|
45
61
|
end
|
46
62
|
|
47
63
|
def close
|
48
64
|
@syslog.close
|
49
65
|
end
|
50
|
-
|
51
|
-
def opened?
|
52
|
-
@syslog.opened?
|
53
|
-
end
|
54
66
|
|
67
|
+
# This method is a kind of a back door, using it you can
|
68
|
+
# change the 'tag' with this message is tagged.
|
69
|
+
#
|
55
70
|
# level default to 'info'
|
56
71
|
# tag default 'ids-custom'
|
57
72
|
def custom(message, tag = 'custom', level = :info)
|
58
|
-
|
59
|
-
self.log(message, level)
|
60
|
-
@syslog = Syslog.reopen( @tag, Syslog::LOG_PID, Syslog.const_get( "LOG_#{@facility.upcase}" ) )
|
61
|
-
end
|
62
|
-
|
63
|
-
# level default to 'info'
|
64
|
-
def log(message, level = :info)
|
65
|
-
# File.open("/tmp/ids.log", 'w') { |f| f.write(message) }
|
66
|
-
@syslog.send( LEVELS_MAP[level][0], clean( message ) )
|
73
|
+
add(tag, level, message)
|
67
74
|
end
|
68
75
|
|
69
76
|
LEVELS_MAP.each_key do |level|
|
70
77
|
define_method level do |message|
|
71
|
-
|
72
|
-
self.log(message, level)
|
73
|
-
end
|
78
|
+
add(@tag, level, message)
|
74
79
|
end
|
75
80
|
|
76
81
|
define_method "#{level}?" do
|
77
|
-
|
82
|
+
level?(level)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
# If it is set to 'true' every line will be sent in the time is wrotten
|
88
|
+
# If it is set to 'false' the buffer will be flushed at the end of the request by Rails
|
89
|
+
# There is a DEFAULT_MAX_BUFFER_SIZE security buffer size
|
90
|
+
#
|
91
|
+
# only true or false
|
92
|
+
def auto_flushing=(value)
|
93
|
+
@max_buffer_size = value ? 1 : DEFAULT_MAX_BUFFER_SIZE
|
94
|
+
end
|
95
|
+
|
96
|
+
def flush
|
97
|
+
@guard.synchronize do
|
98
|
+
buffer.each do |buff|
|
99
|
+
date = buff[0]
|
100
|
+
tag = buff[1]
|
101
|
+
level = buff[2]
|
102
|
+
message = buff[3]
|
103
|
+
|
104
|
+
message = "[#{date.strftime('%Y-%m-%d %H:%M:%S')}] #{message}"
|
105
|
+
|
106
|
+
if(tag == @tag)
|
107
|
+
@syslog.send(LEVELS_MAP[level][0], message)
|
108
|
+
else
|
109
|
+
@syslog = Syslog.reopen(tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
|
110
|
+
@syslog.send(LEVELS_MAP[level][0], message)
|
111
|
+
@syslog = Syslog.reopen(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
|
112
|
+
end
|
113
|
+
end
|
78
114
|
end
|
115
|
+
|
116
|
+
clear_buffer
|
79
117
|
end
|
80
118
|
|
81
|
-
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
def level?(level)
|
123
|
+
LEVELS_MAP[level][1] >= LEVELS_MAP[@level][1]
|
124
|
+
end
|
125
|
+
|
126
|
+
def add(tag, level, message)
|
127
|
+
if(level?(level))
|
128
|
+
# The init of the request
|
129
|
+
if( message.gsub!("\n\nStarted", 'Started') )
|
130
|
+
add(tag, level, '---')
|
131
|
+
end
|
132
|
+
|
133
|
+
buffer << [Time.now, tag, level, message]
|
134
|
+
auto_flush
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def auto_flush
|
139
|
+
self.flush if buffer.size >= @max_buffer_size
|
140
|
+
end
|
82
141
|
|
83
|
-
|
84
|
-
|
85
|
-
def clean(message)
|
86
|
-
message = message.to_s.dup
|
87
|
-
message.strip!
|
88
|
-
message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
|
89
|
-
message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
|
90
|
-
return message
|
142
|
+
def buffer
|
143
|
+
@buffer[Thread.current] ||= []
|
91
144
|
end
|
92
145
|
|
146
|
+
def clear_buffer
|
147
|
+
@buffer.delete(Thread.current)
|
148
|
+
end
|
149
|
+
|
150
|
+
# This is really only for debbugging
|
151
|
+
def log_to_file(message)
|
152
|
+
File.open("/tmp/my.log", 'w') do |f|
|
153
|
+
f.write(">> #{message}\n")
|
154
|
+
f.flush
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
93
158
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
3
|
require 'mocha'
|
4
|
+
require 'delorean'
|
4
5
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/green_syslogger')
|
5
6
|
|
6
7
|
|
@@ -18,43 +19,43 @@ class GreenSysloggerTest < Test::Unit::TestCase
|
|
18
19
|
def test_initialize_by_default
|
19
20
|
Syslog.expects(:open).with('rails', Syslog::LOG_PID, Syslog::LOG_LOCAL2).returns( @logger.instance_eval('@syslog') )
|
20
21
|
@logger = GreenSyslogger.new
|
22
|
+
|
23
|
+
assert_equal('rails', @logger.instance_eval("@tag"))
|
24
|
+
assert_equal('local2', @logger.instance_eval("@facility"))
|
21
25
|
assert_equal(:debug, @logger.instance_eval("@level"))
|
26
|
+
assert_equal(1, @logger.instance_eval("@max_buffer_size"))
|
22
27
|
end
|
23
28
|
|
24
29
|
def test_initialize_with_params
|
25
30
|
Syslog.expects(:open).with('my tag', Syslog::LOG_PID, Syslog::LOG_LOCAL3).returns( @logger.instance_eval('@syslog') )
|
26
31
|
@logger = GreenSyslogger.new('my tag', 'local3', :info)
|
32
|
+
|
33
|
+
assert_equal('my tag', @logger.instance_eval("@tag"))
|
34
|
+
assert_equal('local3', @logger.instance_eval("@facility"))
|
27
35
|
assert_equal(:info, @logger.instance_eval("@level"))
|
36
|
+
assert_equal(1, @logger.instance_eval("@max_buffer_size"))
|
28
37
|
end
|
29
38
|
|
30
39
|
def test_has_a_collection_of_methods
|
31
40
|
GreenSyslogger::LEVELS_MAP.each_key do |level|
|
32
|
-
@logger.expects(:
|
41
|
+
@logger.expects(:add).with('rails', level, @message)
|
33
42
|
@logger.send(level, @message)
|
34
43
|
end
|
35
44
|
end
|
36
45
|
|
37
|
-
def
|
38
|
-
@logger.
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
@logger.instance_eval('
|
45
|
-
@logger.log(@message, :debug)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_clean
|
49
|
-
dirty_message = "\x1B[[1m\x1B[[36mSQL (0.339ms)\x1B[[0m \x1B[[1m select landing_page_id, count(*) cnt from transactions \n where landing_page_id in (4278,4279,4334,4338,4340,4342) \n and status >= 45 and created_at >= '2010-11-08T11:30:22'\n group by landing_page_id\n\x1B[[0m"
|
50
|
-
clean_message = "SQL (0.339ms) select landing_page_id, count(*) cnt from transactions \n where landing_page_id in (4278,4279,4334,4338,4340,4342) \n and status >= 45 and created_at >= '2010-11-08T11:30:22'\n group by landing_page_id\n"
|
51
|
-
|
52
|
-
assert_equal(clean_message, @logger.send(:clean, dirty_message))
|
46
|
+
def test_debug_with_defaul_initialization
|
47
|
+
@logger.expects(:auto_flush)
|
48
|
+
|
49
|
+
Delorean.time_travel_to("2010-01-01 10:10:10") do
|
50
|
+
@logger.debug('wadus message')
|
51
|
+
end
|
52
|
+
|
53
|
+
assert_equal([Time.parse('2010-01-01 10:10:10').to_s, 'rails', 'debug', 'wadus message'], @logger.instance_eval('buffer')[0].map { |e| e.to_s })
|
53
54
|
end
|
54
55
|
|
55
56
|
def test_log_in_debug_level
|
56
57
|
@logger.level = :debug
|
57
|
-
@logger.expects(:
|
58
|
+
@logger.expects(:auto_flush).times(3)
|
58
59
|
@logger.debug(@message)
|
59
60
|
@logger.info(@message)
|
60
61
|
@logger.error(@message)
|
@@ -62,16 +63,56 @@ class GreenSysloggerTest < Test::Unit::TestCase
|
|
62
63
|
|
63
64
|
def test_log_in_info_level
|
64
65
|
@logger.level = :info
|
65
|
-
@logger.expects(:
|
66
|
+
@logger.expects(:auto_flush).times(2)
|
66
67
|
@logger.debug(@message)
|
67
68
|
@logger.info(@message)
|
68
69
|
@logger.error(@message)
|
69
70
|
end
|
70
71
|
|
71
72
|
def test_log_with_custom_tag
|
72
|
-
|
73
|
-
@logger.
|
74
|
-
|
73
|
+
@logger.expects(:add).with('my tag', :info, @message)
|
74
|
+
@logger.custom(@message, 'my tag')
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_auto_flush_activate
|
78
|
+
@logger.auto_flushing = true
|
79
|
+
@logger.expects(:flush).times(1)
|
80
|
+
@logger.debug('wadus message')
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_auto_flush_deactivate
|
84
|
+
old_max_buffer_size = GreenSyslogger::DEFAULT_MAX_BUFFER_SIZE
|
85
|
+
GreenSyslogger.send(:remove_const, :DEFAULT_MAX_BUFFER_SIZE)
|
86
|
+
GreenSyslogger.const_set(:DEFAULT_MAX_BUFFER_SIZE, 3)
|
87
|
+
|
88
|
+
@logger.auto_flushing = false
|
89
|
+
@logger.expects(:flush).times(1)
|
90
|
+
|
91
|
+
@logger.debug('wadus message')
|
92
|
+
@logger.debug('wadus message')
|
93
|
+
@logger.debug('wadus message')
|
94
|
+
|
95
|
+
GreenSyslogger.send(:remove_const, :DEFAULT_MAX_BUFFER_SIZE)
|
96
|
+
GreenSyslogger.const_set(:DEFAULT_MAX_BUFFER_SIZE, old_max_buffer_size)
|
75
97
|
end
|
98
|
+
|
99
|
+
def test_flush
|
100
|
+
@syslog = @logger.instance_eval('@syslog')
|
101
|
+
Syslog.expects(:reopen).returns(@syslog).times(2)
|
102
|
+
@syslog.expects(:debug).times(2)
|
103
|
+
@syslog.expects(:info).times(2)
|
104
|
+
|
105
|
+
@logger.auto_flushing = false
|
106
|
+
@logger.debug('wadus debug')
|
107
|
+
@logger.info('wadus info')
|
108
|
+
@logger.custom('wadus custom tag info', 'new tag', :info)
|
109
|
+
@logger.debug('wadus info again')
|
76
110
|
|
111
|
+
@logger.flush
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_especial_case_init_of_request
|
115
|
+
@logger.expects(:auto_flush).twice
|
116
|
+
@logger.info("\n\nStarted")
|
117
|
+
end
|
77
118
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: green_syslogger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 1
|
9
8
|
- 2
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Fernando Guillen <http://fernandoguillen.info> & Juan Jose Vidal <@j2vidal>
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-12 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -32,9 +32,9 @@ files:
|
|
32
32
|
- Manifest
|
33
33
|
- README.md
|
34
34
|
- Rakefile
|
35
|
+
- green_syslogger.gemspec
|
35
36
|
- lib/green_syslogger.rb
|
36
37
|
- test/green_syslogger_test.rb
|
37
|
-
- green_syslogger.gemspec
|
38
38
|
has_rdoc: true
|
39
39
|
homepage: https://github.com/SponsorPay/GreenSyslogger
|
40
40
|
licenses: []
|