slogger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -0
- data/Gemfile.lock +25 -0
- data/README.rdoc +40 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/slogger.rb +4 -0
- data/lib/slogger/logger.rb +82 -0
- data/lib/slogger/request_logger.rb +65 -0
- data/spec/slogger/logger_spec.rb +43 -0
- data/spec/spec_helper.rb +12 -0
- metadata +122 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
GEM
|
2
|
+
specs:
|
3
|
+
diff-lcs (1.1.2)
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.5.2)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.8.7)
|
10
|
+
rspec (2.5.0)
|
11
|
+
rspec-core (~> 2.5.0)
|
12
|
+
rspec-expectations (~> 2.5.0)
|
13
|
+
rspec-mocks (~> 2.5.0)
|
14
|
+
rspec-core (2.5.1)
|
15
|
+
rspec-expectations (2.5.0)
|
16
|
+
diff-lcs (~> 1.1.2)
|
17
|
+
rspec-mocks (2.5.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
bundler (~> 1.0.0)
|
24
|
+
jeweler (~> 1.5.2)
|
25
|
+
rspec
|
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= slogger
|
2
|
+
|
3
|
+
Slogger is a Ruby library to help work with standard Ruby Syslog library.
|
4
|
+
|
5
|
+
== Features
|
6
|
+
|
7
|
+
=== 1. Slogger::Logger
|
8
|
+
|
9
|
+
A more friendly wrapper on Ruby's Syslog.
|
10
|
+
|
11
|
+
Sample:
|
12
|
+
|
13
|
+
slogger = Slogger::Logger.new "sample_app", :debug, :local0
|
14
|
+
slogger.info "A good info"
|
15
|
+
slogger.debug "A deep info"
|
16
|
+
|
17
|
+
# and after, look at the syslog file of your SO ;)
|
18
|
+
|
19
|
+
=== 2. Slogger::Rack::RequestLogger
|
20
|
+
|
21
|
+
A Rack middleware to log incoming requests.
|
22
|
+
|
23
|
+
Sample:
|
24
|
+
|
25
|
+
configure do
|
26
|
+
slogger = Slogger::Logger.new "sample_app", :debug, :local0
|
27
|
+
use Slogger::Rack::RequestLogger, slogger
|
28
|
+
end
|
29
|
+
|
30
|
+
# and after, look at the syslog file of your SO ;)
|
31
|
+
|
32
|
+
== Future
|
33
|
+
|
34
|
+
I don't know. I think in adding more stuff sometime in the future. Let's see.
|
35
|
+
|
36
|
+
For now is it.
|
37
|
+
|
38
|
+
== Copyright
|
39
|
+
|
40
|
+
Copyright (c) 2011 Leandro Silva (CodeZone). Blog: http://leandrosilva.com.br.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "slogger"
|
16
|
+
gem.homepage = "http://github.com/leandrosilva/slogger"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Slogger is a Ruby library to help work with standard Ruby Syslog library.}
|
19
|
+
gem.description = %Q{Slogger is a Ruby library to help work with standard Ruby Syslog library. Yeah! Just it.}
|
20
|
+
gem.email = "leandrodoze@gmail.com"
|
21
|
+
gem.authors = ["Leandro Silva"]
|
22
|
+
gem.files = FileList["[A-Z]*", "{lib,sample,spec}/**/*"]
|
23
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
24
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
25
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
26
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rspec/core'
|
31
|
+
require 'rspec/core/rake_task'
|
32
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
33
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
34
|
+
end
|
35
|
+
|
36
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
37
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "slogger #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/slogger.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
module Slogger
|
2
|
+
#
|
3
|
+
# The wrapper for standard Ruby Syslog library.
|
4
|
+
#
|
5
|
+
class Logger
|
6
|
+
LEVEL = {
|
7
|
+
:crit => 0,
|
8
|
+
:emerg => 1,
|
9
|
+
:alert => 2,
|
10
|
+
:err => 3,
|
11
|
+
:warning => 4,
|
12
|
+
:notice => 5,
|
13
|
+
:info => 6,
|
14
|
+
:debug => 7
|
15
|
+
}
|
16
|
+
|
17
|
+
FACILITY = {
|
18
|
+
:user => Syslog::LOG_USER,
|
19
|
+
:mail => Syslog::LOG_MAIL,
|
20
|
+
:daemon => Syslog::LOG_DAEMON,
|
21
|
+
:auth => Syslog::LOG_AUTH,
|
22
|
+
:syslog => Syslog::LOG_SYSLOG,
|
23
|
+
:lpr => Syslog::LOG_LPR,
|
24
|
+
:news => Syslog::LOG_NEWS,
|
25
|
+
:uucp => Syslog::LOG_UUCP,
|
26
|
+
:cron => Syslog::LOG_CRON,
|
27
|
+
:authpriv => Syslog::LOG_AUTHPRIV,
|
28
|
+
:ftp => Syslog::LOG_FTP,
|
29
|
+
:local0 => Syslog::LOG_LOCAL0,
|
30
|
+
:local1 => Syslog::LOG_LOCAL1,
|
31
|
+
:local2 => Syslog::LOG_LOCAL2,
|
32
|
+
:local3 => Syslog::LOG_LOCAL3,
|
33
|
+
:local4 => Syslog::LOG_LOCAL4,
|
34
|
+
:local5 => Syslog::LOG_LOCAL5,
|
35
|
+
:local6 => Syslog::LOG_LOCAL6,
|
36
|
+
:local7 => Syslog::LOG_LOCAL7
|
37
|
+
}
|
38
|
+
|
39
|
+
attr_reader :app_name, :level, :facility
|
40
|
+
|
41
|
+
#
|
42
|
+
# To build a Slogger::Logger instance.
|
43
|
+
#
|
44
|
+
# +app_name+:: The appliaction name to be logged
|
45
|
+
# +level+:: The log level: :crit, :emerg, :alert, :err, :warning, :notice,
|
46
|
+
# :info, or :debug.
|
47
|
+
# +facility+:: A typical syslog facility: :user, :mail, :daemon, :auth,
|
48
|
+
# :syslog, :lpr, :news, :uucp, :cron, :authpriv, :ftp,
|
49
|
+
# :local0, :local1, :local2, :local3, :local4, :local5,
|
50
|
+
# :local6, or :local7
|
51
|
+
#
|
52
|
+
# Raises an ArgumentError if app_name, level, or facility is nil.
|
53
|
+
#
|
54
|
+
def initialize(app_name, level, facility)
|
55
|
+
raise ArgumentError, "The 'app_name' parameter is required" unless app_name
|
56
|
+
raise ArgumentError, "The 'level' parameter is required" unless level
|
57
|
+
raise ArgumentError, "The 'facility' parameter is required" unless facility
|
58
|
+
|
59
|
+
@app_name = app_name
|
60
|
+
@level = level
|
61
|
+
@level_as_int = LEVEL[level]
|
62
|
+
@facility = facility
|
63
|
+
@facility_as_int = FACILITY[facility]
|
64
|
+
end
|
65
|
+
|
66
|
+
def method_missing(name, *args)
|
67
|
+
if LEVEL.key? name
|
68
|
+
log(name, args[0])
|
69
|
+
else
|
70
|
+
super name, *args
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def log(level, message)
|
77
|
+
return if LEVEL[level] > @level_as_int
|
78
|
+
|
79
|
+
Syslog.open(@app_name, Syslog::LOG_PID, @facility_as_int) { |s| s.send level, message }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Slogger
|
2
|
+
module Rack
|
3
|
+
#
|
4
|
+
# Slogger::Rack::RequestLogger is a kind of Rack middleware. It forwards every
|
5
|
+
# request to an +app+ given, and logs a line in the syslog using Slogger::Logger.
|
6
|
+
#
|
7
|
+
# Yes, it's based on Rack::CommonLogger code.
|
8
|
+
#
|
9
|
+
class RequestLogger
|
10
|
+
FORMAT = %{%s - %s "%s %s%s %s" %d %s %0.4f}
|
11
|
+
|
12
|
+
#
|
13
|
+
# To build a Slogger::Rack::RequestLogger instance.
|
14
|
+
#
|
15
|
+
# +app+:: The Rack application
|
16
|
+
# +logger+:: A Slogger::Logger instance
|
17
|
+
#
|
18
|
+
def initialize(app, slogger)
|
19
|
+
@app = app
|
20
|
+
@logger = slogger
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(env)
|
24
|
+
began_at = Time.now
|
25
|
+
status, header, body = @app.call env
|
26
|
+
header = ::Rack::Utils::HeaderHash.new header
|
27
|
+
|
28
|
+
log env, status, header, began_at
|
29
|
+
|
30
|
+
[status, header, body]
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def log(env, status, header, began_at)
|
36
|
+
now = Time.now
|
37
|
+
length = extract_content_length header
|
38
|
+
|
39
|
+
message = FORMAT % [
|
40
|
+
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
|
41
|
+
env["REMOTE_USER"] || "-",
|
42
|
+
env["REQUEST_METHOD"],
|
43
|
+
env["PATH_INFO"],
|
44
|
+
env["QUERY_STRING"].empty? ? "" : "?#{env['QUERY_STRING']}",
|
45
|
+
env["HTTP_VERSION"],
|
46
|
+
status.to_s[0..3],
|
47
|
+
length,
|
48
|
+
now - began_at ]
|
49
|
+
|
50
|
+
sanitize! message
|
51
|
+
|
52
|
+
@logger.info message
|
53
|
+
end
|
54
|
+
|
55
|
+
def extract_content_length(headers)
|
56
|
+
value = headers["Content-Length"] or return "-"
|
57
|
+
value.to_s == "0" ? "-" : value
|
58
|
+
end
|
59
|
+
|
60
|
+
def sanitize!(string)
|
61
|
+
string.gsub!("%", "%%")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "/spec_helper")
|
2
|
+
|
3
|
+
describe Slogger::Logger do
|
4
|
+
describe "valid state" do
|
5
|
+
subject { Slogger::Logger.new "test_app", :debug, :local0 }
|
6
|
+
|
7
|
+
its(:app_name) { should == "test_app" }
|
8
|
+
its(:level) { should == :debug }
|
9
|
+
its(:facility) { should == :local0 }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "invalid state" do
|
13
|
+
it "should raise ArgumentError if doen't have app_name" do
|
14
|
+
lambda { Slogger::Logger.new nil, :debug, :local0 }.should raise_error
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should raise ArgumentError if doen't have level" do
|
18
|
+
lambda { Slogger::Logger.new "test_app", nil, :local0 }.should raise_error
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should raise ArgumentError if doen't have facility" do
|
22
|
+
lambda { Slogger::Logger.new "test_app", :debug, nil }.should raise_error
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "when in warning level" do
|
27
|
+
subject { Slogger::Logger.new "test_app", :warning, :local0 }
|
28
|
+
|
29
|
+
it "should log WARNING messages" do
|
30
|
+
Syslog.stub!(:warning).with(anything).and_return(Syslog)
|
31
|
+
Syslog.should_receive(:warning).and_return(Syslog)
|
32
|
+
|
33
|
+
subject.warning "WARNING message"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "shouldn't log INFO messages" do
|
37
|
+
Syslog.stub!(:info).with(anything).and_return(Syslog)
|
38
|
+
Syslog.should_not_receive(:info).and_return(Syslog)
|
39
|
+
|
40
|
+
subject.info "INFO message"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slogger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Leandro Silva
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-17 00:00:00 -02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
prerelease: false
|
33
|
+
type: :development
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bundler
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 23
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
version: 1.0.0
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: jeweler
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 7
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 5
|
62
|
+
- 2
|
63
|
+
version: 1.5.2
|
64
|
+
prerelease: false
|
65
|
+
type: :development
|
66
|
+
requirement: *id003
|
67
|
+
description: Slogger is a Ruby library to help work with standard Ruby Syslog library. Yeah! Just it.
|
68
|
+
email: leandrodoze@gmail.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files:
|
74
|
+
- README.rdoc
|
75
|
+
files:
|
76
|
+
- Gemfile
|
77
|
+
- Gemfile.lock
|
78
|
+
- README.rdoc
|
79
|
+
- Rakefile
|
80
|
+
- VERSION
|
81
|
+
- lib/slogger.rb
|
82
|
+
- lib/slogger/logger.rb
|
83
|
+
- lib/slogger/request_logger.rb
|
84
|
+
- spec/slogger/logger_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
has_rdoc: true
|
87
|
+
homepage: http://github.com/leandrosilva/slogger
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirements: []
|
114
|
+
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.5.2
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Slogger is a Ruby library to help work with standard Ruby Syslog library.
|
120
|
+
test_files:
|
121
|
+
- spec/slogger/logger_spec.rb
|
122
|
+
- spec/spec_helper.rb
|