slogger 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +29 -8
- data/VERSION +1 -1
- data/lib/slogger/logger.rb +35 -17
- data/spec/slogger/logger_spec.rb +68 -27
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
Slogger is a Ruby library to help work with standard Ruby Syslog library.
|
4
4
|
|
5
|
+
== Install
|
6
|
+
|
7
|
+
Slogger is hosted on RubyGems (https://rubygems.org/gems/slogger), so to install it is really easy.
|
8
|
+
|
9
|
+
$ gem install slogger
|
10
|
+
|
5
11
|
== Features
|
6
12
|
|
7
13
|
=== Slogger::Logger
|
@@ -10,14 +16,27 @@ A more friendly wrapper on Ruby's Syslog.
|
|
10
16
|
|
11
17
|
Sample: simple message log
|
12
18
|
|
13
|
-
|
19
|
+
require "slogger"
|
20
|
+
|
21
|
+
# So in this sample:
|
22
|
+
#
|
23
|
+
# "sample_name": application name
|
24
|
+
# :info : severity level
|
25
|
+
# :local0 : facility
|
26
|
+
|
27
|
+
slogger = Slogger::Logger.new "sample_app", :info, :local0
|
14
28
|
slogger.info "A good info"
|
15
|
-
slogger.debug "A deep info"
|
29
|
+
slogger.debug "A deep info (oops!)" # it'll not be logged
|
30
|
+
|
31
|
+
slogger.severity = :debug
|
32
|
+
slogger.debug "A deep info (yay!)" # now it'll be logged
|
16
33
|
|
17
34
|
# and after, look at the syslog file of your SO ;)
|
18
35
|
|
19
36
|
Sample: message log preceded by spent time
|
20
37
|
|
38
|
+
require "slogger"
|
39
|
+
|
21
40
|
slogger = Slogger::Logger.new "sample_app", :debug, :local0
|
22
41
|
slogger.info "A really good info preceded by spent time" do
|
23
42
|
# do something
|
@@ -31,12 +50,14 @@ A Rack middleware to log incoming requests.
|
|
31
50
|
|
32
51
|
Sample:
|
33
52
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
53
|
+
require "slogger"
|
54
|
+
|
55
|
+
configure do
|
56
|
+
slogger = Slogger::Logger.new "sample_app", :debug, :local0
|
57
|
+
use Slogger::Rack::RequestLogger, slogger
|
58
|
+
end
|
38
59
|
|
39
|
-
|
60
|
+
# and after, look at the syslog file of your SO ;)
|
40
61
|
|
41
62
|
== Future
|
42
63
|
|
@@ -46,4 +67,4 @@ For now is it.
|
|
46
67
|
|
47
68
|
== Copyright
|
48
69
|
|
49
|
-
Copyright (c) 2011 Leandro Silva (CodeZone).
|
70
|
+
Copyright (c) 2011 Leandro Silva (CodeZone).
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/slogger/logger.rb
CHANGED
@@ -3,7 +3,7 @@ module Slogger
|
|
3
3
|
# The wrapper for standard Ruby Syslog library.
|
4
4
|
#
|
5
5
|
class Logger
|
6
|
-
|
6
|
+
SEVERITY = {
|
7
7
|
:crit => 0,
|
8
8
|
:emerg => 1,
|
9
9
|
:alert => 2,
|
@@ -36,43 +36,53 @@ module Slogger
|
|
36
36
|
:local7 => Syslog::LOG_LOCAL7
|
37
37
|
}
|
38
38
|
|
39
|
-
attr_reader :app_name, :
|
39
|
+
attr_reader :app_name, :severity, :facility
|
40
40
|
|
41
41
|
#
|
42
42
|
# To build a Slogger::Logger instance.
|
43
43
|
#
|
44
44
|
# +app_name+:: The appliaction name to be logged
|
45
|
-
# +
|
46
|
-
# :info, or :debug.
|
45
|
+
# +severity+:: The log severity: :crit, :emerg, :alert, :err, :warning, :notice,
|
46
|
+
# :info, or :debug. It can be changed at anytime.
|
47
47
|
# +facility+:: A typical syslog facility: :user, :mail, :daemon, :auth,
|
48
48
|
# :syslog, :lpr, :news, :uucp, :cron, :authpriv, :ftp,
|
49
49
|
# :local0, :local1, :local2, :local3, :local4, :local5,
|
50
50
|
# :local6, or :local7
|
51
51
|
#
|
52
|
-
# Raises an ArgumentError if app_name,
|
52
|
+
# Raises an ArgumentError if app_name, severity, or facility is nil.
|
53
53
|
#
|
54
|
-
def initialize(app_name,
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
def initialize(app_name, severity, facility)
|
55
|
+
raise_argument_error_to_required_parameter "app_name" unless app_name
|
56
|
+
raise_argument_error_to_required_parameter "severity" unless severity
|
57
|
+
raise_argument_error_to_required_parameter "facility" unless facility
|
58
|
+
|
59
|
+
raise_argument_error_to_invalid_parameter "severity", "SEVERITY" unless SEVERITY[severity]
|
60
|
+
raise_argument_error_to_invalid_parameter "facility", "FACILITY" unless FACILITY[facility]
|
58
61
|
|
59
62
|
@app_name = app_name
|
60
|
-
@
|
61
|
-
@
|
63
|
+
@severity = severity
|
64
|
+
@severity_as_int = SEVERITY[severity]
|
62
65
|
@facility = facility
|
63
66
|
@facility_as_int = FACILITY[facility]
|
64
67
|
end
|
65
68
|
|
66
|
-
|
67
|
-
define_method
|
68
|
-
log(
|
69
|
+
SEVERITY.each_key do |severity|
|
70
|
+
define_method severity do |message, &block|
|
71
|
+
log(severity, message, &block)
|
69
72
|
end
|
70
73
|
end
|
71
74
|
|
75
|
+
def severity=(value)
|
76
|
+
raise_argument_error_to_invalid_parameter "severity", "SEVERITY" unless SEVERITY[value]
|
77
|
+
|
78
|
+
@severity = value
|
79
|
+
@severity_as_int = SEVERITY[value]
|
80
|
+
end
|
81
|
+
|
72
82
|
private
|
73
83
|
|
74
|
-
def log(
|
75
|
-
return if
|
84
|
+
def log(severity, message, &block)
|
85
|
+
return if SEVERITY[severity] > @severity_as_int
|
76
86
|
|
77
87
|
if block_given?
|
78
88
|
began_at = Time.now
|
@@ -84,7 +94,15 @@ module Slogger
|
|
84
94
|
message = "[#{end_at}s] #{message}"
|
85
95
|
end
|
86
96
|
|
87
|
-
Syslog.open(@app_name, Syslog::LOG_PID, @facility_as_int) { |s| s.send
|
97
|
+
Syslog.open(@app_name, Syslog::LOG_PID, @facility_as_int) { |s| s.send severity, message }
|
98
|
+
end
|
99
|
+
|
100
|
+
def raise_argument_error_to_required_parameter(param)
|
101
|
+
raise ArgumentError, "The '#{param}' parameter is required."
|
102
|
+
end
|
103
|
+
|
104
|
+
def raise_argument_error_to_invalid_parameter(param, options)
|
105
|
+
raise ArgumentError, "The '#{param}' parameter is invalid. Inspect the #{options} constant to know the options."
|
88
106
|
end
|
89
107
|
end
|
90
108
|
end
|
data/spec/slogger/logger_spec.rb
CHANGED
@@ -1,52 +1,93 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "..", "/spec_helper")
|
2
2
|
|
3
3
|
describe Slogger::Logger do
|
4
|
-
|
5
|
-
subject { Slogger::Logger.new "test_app", :debug, :local0 }
|
4
|
+
subject { Slogger::Logger.new "test_app", :debug, :local0 }
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
describe "valid state" do
|
7
|
+
it "should have an app_name attribute" do
|
8
|
+
subject.app_name.should == "test_app"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should have a severity attribute" do
|
12
|
+
subject.severity.should == :debug
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have a facility attribute" do
|
16
|
+
subject.facility.should == :local0
|
17
|
+
end
|
10
18
|
end
|
11
19
|
|
12
20
|
describe "invalid state" do
|
13
|
-
it "should raise ArgumentError if
|
21
|
+
it "should raise ArgumentError if doesn't have app_name" do
|
14
22
|
lambda { Slogger::Logger.new nil, :debug, :local0 }.should raise_error
|
15
23
|
end
|
16
24
|
|
17
|
-
it "should raise ArgumentError if
|
25
|
+
it "should raise ArgumentError if doesn't have severity" do
|
18
26
|
lambda { Slogger::Logger.new "test_app", nil, :local0 }.should raise_error
|
19
27
|
end
|
20
28
|
|
21
|
-
it "should raise ArgumentError if
|
29
|
+
it "should raise ArgumentError if doesn't have facility" do
|
22
30
|
lambda { Slogger::Logger.new "test_app", :debug, nil }.should raise_error
|
23
31
|
end
|
32
|
+
|
33
|
+
it "should raise ArgumentError if severity level is invalid" do
|
34
|
+
lambda { Slogger::Logger.new "test_app", :junk, :local0 }.should raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should raise ArgumentError if facility is invalid" do
|
38
|
+
lambda { Slogger::Logger.new "test_app", :describe, :junk }.should raise_error
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "severity setup" do
|
43
|
+
it "should be possible to change severity attribute" do
|
44
|
+
subject.severity.should be :debug
|
45
|
+
subject.severity = :warning
|
46
|
+
subject.severity.should be :warning
|
47
|
+
subject.severity = :info
|
48
|
+
subject.severity.should be :info
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should raise ArgumentError if try to change severity attribute to a invalid one" do
|
52
|
+
lambda { subject.severity = :junk }.should raise_error
|
53
|
+
end
|
24
54
|
end
|
25
55
|
|
26
|
-
describe "
|
27
|
-
|
56
|
+
describe "logging" do
|
57
|
+
describe "when is in WARNING severity" do
|
58
|
+
subject { Slogger::Logger.new "test_app", :warning, :local0 }
|
28
59
|
|
29
|
-
|
30
|
-
|
31
|
-
|
60
|
+
it "should log WARNING messages" do
|
61
|
+
Syslog.stub!(:warning).with(anything).and_return(Syslog)
|
62
|
+
Syslog.should_receive(:warning).and_return(Syslog)
|
32
63
|
|
33
|
-
|
34
|
-
|
64
|
+
subject.warning "WARNING message"
|
65
|
+
end
|
35
66
|
|
36
|
-
|
37
|
-
|
38
|
-
|
67
|
+
it "shouldn't log INFO messages" do
|
68
|
+
Syslog.stub!(:info).with(anything).and_return(Syslog)
|
69
|
+
Syslog.should_not_receive(:info).and_return(Syslog)
|
70
|
+
|
71
|
+
subject.info "INFO message"
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "but when severity is changed to INFO" do
|
75
|
+
it "should log INFO messages" do
|
76
|
+
subject.severity = :info
|
39
77
|
|
40
|
-
|
78
|
+
Syslog.stub!(:info).with(anything).and_return(Syslog)
|
79
|
+
Syslog.should_receive(:info).and_return(Syslog)
|
80
|
+
|
81
|
+
subject.info "INFO message"
|
82
|
+
end
|
83
|
+
end
|
41
84
|
end
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
subject.info "a block wrapped by log" do
|
49
|
-
sleep(10)
|
85
|
+
|
86
|
+
describe "when a block is passed to log method" do
|
87
|
+
it "should add spent time to the message" do
|
88
|
+
subject.info "a block wrapped by log" do
|
89
|
+
sleep(2)
|
90
|
+
end
|
50
91
|
end
|
51
92
|
end
|
52
93
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slogger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Leandro Silva
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-21 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|