yell-adapters-syslog 0.4.0

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in yell-adapters-syslog.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2012 Rudolf Schmidt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ # Copyright (c) 2012 Rudolf Schmidt
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ require 'yell'
25
+
26
+ require File.dirname(__FILE__) + '/yell/adapters/syslog'
27
+
28
+ module Yell #:nodoc:
29
+ end
30
+
@@ -0,0 +1,150 @@
1
+ # encoding: utf-8
2
+
3
+ require 'syslog'
4
+
5
+ module Yell #:nodoc:
6
+ module Adapters #:nodoc:
7
+
8
+ class Syslog < Yell::Adapters::Base
9
+
10
+ # Syslog severities
11
+ #
12
+ # `man syslog` to see the severities
13
+ Severities = [7, 6, 4, 3, 2, 1]
14
+
15
+ # Map Syslog severities to internal represenation
16
+ # 'DEBUG' => 7
17
+ # 'INFO' => 6
18
+ # 'WARN' => 4
19
+ # 'ERROR' => 3
20
+ # 'FATAL' => 2
21
+ # 'UNKNOWN' => 1
22
+ SeverityMap = Hash[ *(Yell::Severities.zip(Severities).flatten) ]
23
+
24
+ # Map Syslog options to internal representation
25
+ OptionMap = {
26
+ :cons => ::Syslog::LOG_CONS,
27
+ :ndelay => ::Syslog::LOG_NDELAY,
28
+ :nowait => ::Syslog::LOG_NOWAIT,
29
+ :odelay => ::Syslog::LOG_ODELAY,
30
+ :perror => ::Syslog::LOG_PERROR,
31
+ :pid => ::Syslog::LOG_PID
32
+ }
33
+
34
+ # Map Syslog facilities to internal represenation
35
+ FacilityMap = {
36
+ :auth => ::Syslog::LOG_AUTH,
37
+ :authpriv => ::Syslog::LOG_AUTHPRIV,
38
+ # :console => ::Syslog::LOG_CONSOLE, # described in 1.9.3 docu, but not defined
39
+ :cron => ::Syslog::LOG_CRON,
40
+ :daemon => ::Syslog::LOG_DAEMON,
41
+ :ftp => ::Syslog::LOG_FTP,
42
+ :kern => ::Syslog::LOG_KERN,
43
+ # :lrp => ::Syslog::LOG_LRP, # described in 1.9.3 docu, but not defined
44
+ :mail => ::Syslog::LOG_MAIL,
45
+ :news => ::Syslog::LOG_NEWS,
46
+ # :ntp => ::Syslog::LOG_NTP, # described in 1.9.3 docu, but not defined
47
+ # :security => ::Syslog::LOG_SECURITY, # described in 1.9.3 docu, but not defined
48
+ :syslog => ::Syslog::LOG_SYSLOG,
49
+ :user => ::Syslog::LOG_USER,
50
+ :uucp => ::Syslog::LOG_UUCP,
51
+ :local0 => ::Syslog::LOG_LOCAL0,
52
+ :local1 => ::Syslog::LOG_LOCAL1,
53
+ :local2 => ::Syslog::LOG_LOCAL2,
54
+ :local3 => ::Syslog::LOG_LOCAL3,
55
+ :local4 => ::Syslog::LOG_LOCAL4,
56
+ :local5 => ::Syslog::LOG_LOCAL5,
57
+ :local6 => ::Syslog::LOG_LOCAL6,
58
+ :local7 => ::Syslog::LOG_LOCAL7
59
+ }
60
+
61
+ def initialize( options = {}, &block )
62
+ ident options[:ident] || $0
63
+ options options[:options] || [:pid, :cons]
64
+ facility options[:facility]
65
+
66
+ super( options, &block )
67
+ end
68
+
69
+ def stream
70
+ @stream ||= _new_stream
71
+ end
72
+
73
+ def close
74
+ @stream.close if @stream.respond_to? :close
75
+ rescue
76
+ # do nothing
77
+ ensure
78
+ @stream = nil
79
+ end
80
+
81
+ # Identify the calling program
82
+ def ident( val = nil )
83
+ @ident = val
84
+ end
85
+
86
+ # Set the log facility for Syslog
87
+ #
88
+ # See {Yell::Adapters::Syslog::OptionMap OptionMap} for allowed values.
89
+ #
90
+ # @example Direct or Symbol
91
+ # facility( Syslog::LOG_NDELAY )
92
+ # facility( :ndelay )
93
+ #
94
+ # @example Multiple
95
+ # facility( :ndelay, Syslog::LOG_NDELAY ):
96
+ def options( *values )
97
+ @syslog_options = values.flatten.map do |v|
98
+ v.is_a?(Symbol) ? OptionMap[v] : v
99
+ end.inject { |a, b| a | b }
100
+ end
101
+
102
+ # Set the log facility for Syslog
103
+ #
104
+ # See {Yell::Adapters::Syslog::FacilityMap FacilityMap} for allowed values.
105
+ # @example Direct or Symbol
106
+ # facility( :user )
107
+ # facility( Syslog::LOG_CONSOLE )
108
+ #
109
+ # @example Multiple
110
+ # facility( :user, Syslog::LOG_CONSOLE )
111
+ def facility( *values )
112
+ @facility = values.flatten.map do |v|
113
+ v.is_a?(Symbol) ? FacilityMap[v] : v
114
+ end.inject { |a, b| a | b }
115
+ end
116
+
117
+
118
+ private
119
+
120
+ def write!( event )
121
+ stream.log( SeverityMap[event.level], clean(event.message) )
122
+ rescue Exception => e
123
+ close
124
+
125
+ # re-raise the exception
126
+ raise( e )
127
+ end
128
+
129
+ def _new_stream
130
+ return ::Syslog if ::Syslog.opened?
131
+
132
+ ::Syslog.open( @ident, @syslog_options, @facility )
133
+ end
134
+
135
+ # Borrowed from [SyslogLogger](https://github.com/seattlerb/sysloglogger)
136
+ def clean( message )
137
+ message = message.to_s.dup
138
+ message.strip!
139
+ message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
140
+ message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
141
+
142
+ message
143
+ end
144
+
145
+ end
146
+
147
+ register( :syslog, Yell::Adapters::Syslog )
148
+
149
+ end
150
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ module Yell #:nodoc:
4
+ module Adapters #:nodoc:
5
+
6
+ class Syslog
7
+ VERSION = "0.4.0"
8
+
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,14 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'yell-adapters-syslog'
5
+
6
+ require 'rspec'
7
+ require 'rr'
8
+ require 'timecop'
9
+
10
+ RSpec.configure do |config|
11
+ config.mock_framework = :rr
12
+
13
+ end
14
+
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yell::Adapters::Syslog do
4
+ before do
5
+ Syslog.close if Syslog.opened?
6
+ end
7
+
8
+ shared_examples_for "a Syslog adapter" do
9
+ let( :adapter ) { Yell::Adapters::Syslog.new }
10
+
11
+ it "should call Syslog correctly" do
12
+ mock( Syslog ).log( Yell::Adapters::Syslog::SeverityMap[subject], "Hello World" )
13
+
14
+ adapter.write Yell::Event.new( subject, "Hello World" )
15
+ end
16
+ end
17
+
18
+ it_behaves_like( "a Syslog adapter" ) { subject { 'DEBUG' } }
19
+ it_behaves_like( "a Syslog adapter" ) { subject { 'INFO' } }
20
+ it_behaves_like( "a Syslog adapter" ) { subject { 'WARN' } }
21
+ it_behaves_like( "a Syslog adapter" ) { subject { 'ERROR' } }
22
+ it_behaves_like( "a Syslog adapter" ) { subject { 'FATAL' } }
23
+ it_behaves_like( "a Syslog adapter" ) { subject { 'UNKNOWN' } }
24
+
25
+ context "a new Yell::Adapters::Syslog instance" do
26
+ subject { Yell::Adapters::Syslog.new }
27
+
28
+ it { subject.instance_variable_get(:@ident).should == $0 }
29
+ it { subject.instance_variable_get(:@syslog_options).should == (Syslog::LOG_PID | Syslog::LOG_CONS) }
30
+ it { subject.instance_variable_get(:@facility).should be_nil }
31
+ end
32
+
33
+ context "OptionMap" do
34
+ subject { Yell::Adapters::Syslog::OptionMap }
35
+
36
+ it { subject[:cons].should == Syslog::LOG_CONS }
37
+ it { subject[:ndelay].should == Syslog::LOG_NDELAY }
38
+ it { subject[:nowait].should == Syslog::LOG_NOWAIT }
39
+ it { subject[:odelay].should == Syslog::LOG_ODELAY }
40
+ it { subject[:perror].should == Syslog::LOG_PERROR }
41
+ it { subject[:pid].should == Syslog::LOG_PID }
42
+ end
43
+
44
+ context "FacilityMap" do
45
+ subject { Yell::Adapters::Syslog::FacilityMap }
46
+
47
+ # :console => ::Syslog::LOG_CONSOLE, # described in 1.9.3 docu, but not defined
48
+ # :lrp => ::Syslog::LOG_LRP, # described in 1.9.3 docu, but not defined
49
+ # :ntp => ::Syslog::LOG_NTP, # described in 1.9.3 docu, but not defined
50
+ # :security => ::Syslog::LOG_SECURITY, # described in 1.9.3 docu, but not defined
51
+ it { subject[:auth].should == Syslog::LOG_AUTH }
52
+ it { subject[:authpriv].should == Syslog::LOG_AUTHPRIV }
53
+ it { subject[:cron].should == Syslog::LOG_CRON }
54
+ it { subject[:daemon].should == Syslog::LOG_DAEMON }
55
+ it { subject[:ftp].should == Syslog::LOG_FTP }
56
+ it { subject[:kern].should == Syslog::LOG_KERN }
57
+ it { subject[:mail].should == Syslog::LOG_MAIL }
58
+ it { subject[:news].should == Syslog::LOG_NEWS }
59
+ it { subject[:syslog].should == Syslog::LOG_SYSLOG }
60
+ it { subject[:user].should == Syslog::LOG_USER }
61
+ it { subject[:uucp].should == Syslog::LOG_UUCP }
62
+ it { subject[:local0].should == Syslog::LOG_LOCAL0 }
63
+ it { subject[:local1].should == Syslog::LOG_LOCAL1 }
64
+ it { subject[:local2].should == Syslog::LOG_LOCAL2 }
65
+ it { subject[:local3].should == Syslog::LOG_LOCAL3 }
66
+ it { subject[:local4].should == Syslog::LOG_LOCAL4 }
67
+ it { subject[:local5].should == Syslog::LOG_LOCAL5 }
68
+ it { subject[:local6].should == Syslog::LOG_LOCAL6 }
69
+ it { subject[:local7].should == Syslog::LOG_LOCAL7 }
70
+ end
71
+
72
+ context :ident do
73
+ subject { Yell::Adapters::Syslog.new }
74
+
75
+ it "should be passed" do
76
+ mock.proxy( Syslog ).open( "my ident", anything, anything )
77
+
78
+ subject.ident "my ident"
79
+ subject.write Yell::Event.new("INFO", "Hello World")
80
+ end
81
+ end
82
+
83
+ context :options do
84
+ subject { Yell::Adapters::Syslog.new }
85
+
86
+ it "should be passed" do
87
+ mock.proxy( Syslog ).open( anything, Syslog::LOG_NDELAY, anything )
88
+
89
+ subject.options :ndelay
90
+ subject.write Yell::Event.new("INFO", "Hello World")
91
+ end
92
+
93
+ it "should work with multiple params" do
94
+ mock.proxy( Syslog ).open( anything, Syslog::LOG_PID|Syslog::LOG_NDELAY, anything )
95
+
96
+ subject.options :pid, :ndelay
97
+ subject.write Yell::Event.new("INFO", "Hello World")
98
+ end
99
+ end
100
+
101
+ context :facility do
102
+ subject { Yell::Adapters::Syslog.new }
103
+
104
+ it "should be passed" do
105
+ mock.proxy( Syslog ).open( anything, anything, Syslog::LOG_USER )
106
+
107
+ subject.facility :user
108
+ subject.write Yell::Event.new("INFO", "Hello World")
109
+ end
110
+
111
+ it "should work with multiple params" do
112
+ mock.proxy( Syslog ).open( anything, anything, Syslog::LOG_DAEMON|Syslog::LOG_USER )
113
+
114
+ subject.facility :daemon, :user
115
+ subject.write Yell::Event.new("INFO", "Hello World")
116
+ end
117
+ end
118
+
119
+ end
120
+
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "yell/adapters/syslog/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "yell-adapters-syslog"
7
+ s.version = Yell::Adapters::Syslog::VERSION
8
+ s.authors = ["Rudolf Schmidt"]
9
+
10
+ s.homepage = "http://rubygems.org/gems/yell"
11
+ s.summary = %q{Yell - Your Extensible Logging Library }
12
+ s.description = %q{Syslog adapter for Yell}
13
+
14
+ s.rubyforge_project = "yell"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "yell", "~> 0.4"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "rr"
24
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yell-adapters-syslog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rudolf Schmidt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yell
16
+ requirement: &70357165504240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70357165504240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70357165503840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70357165503840
36
+ - !ruby/object:Gem::Dependency
37
+ name: rr
38
+ requirement: &70357165503380 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70357165503380
47
+ description: Syslog adapter for Yell
48
+ email:
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - Rakefile
57
+ - lib/yell-adapters-syslog.rb
58
+ - lib/yell/adapters/syslog.rb
59
+ - lib/yell/adapters/syslog/version.rb
60
+ - spec/spec_helper.rb
61
+ - spec/yell/adapters/syslog_spec.rb
62
+ - yell-adapters-syslog.gemspec
63
+ homepage: http://rubygems.org/gems/yell
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project: yell
83
+ rubygems_version: 1.8.17
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Yell - Your Extensible Logging Library
87
+ test_files:
88
+ - spec/spec_helper.rb
89
+ - spec/yell/adapters/syslog_spec.rb
90
+ has_rdoc: