syslog_ruby 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05ceabc82694c02a9a63e44f0b5106563031f66d
4
+ data.tar.gz: e331f6898d9bfec6d53ec2c043e1769e50e87be9
5
+ SHA512:
6
+ metadata.gz: 65d8afee0f7fdaa456c5e067de270e15c688f49bcab28c9ab605916082b11cc329b86acb6e649e8ecdc8d0b86f0053a89e2e8ff7f0827f561487c8648a31df33
7
+ data.tar.gz: 1ba41b4558889326259bfe17f053720e9a327476e9e37fae06d932ab1cd75002475fdb680a1e759e3e65c818502f1738ef64b51ef87417eee01121c2d0cc0a43
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in syslog_ruby.gemspec
4
+ gemspec
5
+
6
+ # Used to benchmark
7
+ gem 'syslog-logger'
8
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2009-2015 johnskopis. https://github.com/johnskopis/syslog_ruby
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.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # SyslogRuby
2
+
3
+ This gem implements the syslog protocol in pure ruby. The Syslog module that
4
+ uses the openlog(3) syscall can only have a single facility open. SyslogRuby
5
+ allows you to have as many loggers as you want.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'syslog_ruby'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install syslog_ruby
20
+
21
+ ## Usage
22
+
23
+ The syslog_ruby gem also implements a pure ruby version of the Syslog module:
24
+
25
+ require 'syslog_compat'
26
+
27
+ To use the pure ruby version
28
+
29
+ tcp = SyslogRuby::Logger.new('ruby', :LOCAL3, uri: 'tcp://127.0.0.1:514')
30
+ udp = SyslogRuby::Logger.new('ruby', :LOCAL4, uri: 'udp://127.0.0.1:514')
31
+ local = SyslogRuby::Logger.new('ruby', :LOCAL5, uri: '/dev/log')
32
+ local.info "a message"
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/benchmark.rb ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ require 'benchmark'
4
+
5
+ # pass an arg to use the pure ruby Syslog module
6
+ if !!ARGV[0]
7
+ puts "monkey patching Syslog module..."
8
+ require 'syslog_compat'
9
+ end
10
+ require 'syslog-logger'
11
+ require 'syslog_ruby'
12
+
13
+
14
+ Benchmark.bm(20) do |x|
15
+ x.report('single syslog') do
16
+ Syslog.open
17
+ 225.times do
18
+ Syslog.info "(single syslog) Logged via Syslog"
19
+ end
20
+ Syslog.close if Syslog.opened?
21
+ end
22
+
23
+ sleep 1
24
+
25
+ x.report('single syslogger') do
26
+ l1 = Logger::Syslog.new('test', Syslog::LOG_SYSLOG)
27
+ 225.times do
28
+ l1.info "(single syslogger) Logged via Logger::Syslog (1)"
29
+ end
30
+ end
31
+
32
+ sleep 1
33
+
34
+ x.report('single pure ruby') do
35
+ rl1 = SyslogRuby::Logger.new('pure-test1', :SYSLOG, uri: '/dev/log')
36
+ 225.times do
37
+ rl1.info "(single pure ruby) logged via SyslogRuby::Logger (1)"
38
+ end
39
+ end
40
+
41
+ sleep 1
42
+
43
+ x.report('2 sysloggers') do
44
+ l1 = Logger::Syslog.new('test2-1', Syslog::LOG_SYSLOG)
45
+ l2 = Logger::Syslog.new('test2-2', Syslog::LOG_SYSLOG)
46
+ 225.times do
47
+ l1.info "(2 sysloggers) logged via Logger::Syslog (1)"
48
+ l2.info "(2 sysloggers) logged via Logger::Syslog (2)"
49
+ end
50
+ end
51
+
52
+ sleep 1
53
+
54
+ x.report('2 pure ruby') do
55
+ rl1 = SyslogRuby::Logger.new('pure-test2-1', :SYSLOG, uri: '/dev/log')
56
+ rl2 = SyslogRuby::Logger.new('pure-test2-2', :SYSLOG, uri: '/dev/log')
57
+ 225.times do
58
+ rl1.info "(2 pure ruby) logged via SyslogRuby::Logger (1)"
59
+ rl2.info "(2 pure ruby) logged via SyslogRuby::Logger (2)"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,104 @@
1
+ require 'syslog_ruby'
2
+
3
+ module Syslog
4
+ include SyslogRuby::Severity
5
+ include SyslogRuby::Facility
6
+ @@logger = nil
7
+
8
+ class << self
9
+ def setup_severity_methods
10
+ SyslogRuby::Severity.constants.each do |level_name|
11
+ level = SyslogRuby::Severity.const_get level_name
12
+
13
+ self.class.send :define_method, level_name.downcase do |*args|
14
+ @@logger.log level, *args
15
+ end
16
+ self.class.send :define_method, level_name.downcase.to_sym do |*args|
17
+ @@logger.log level, *args
18
+ end
19
+ end
20
+ end
21
+
22
+ def open(ident = 'ruby', logopt = nil, facility = Syslog::LOG_LOCAL6)
23
+ open_opt(ident, logopt, facility)
24
+ end
25
+
26
+ def open_opt(ident = 'ruby', logopt = nil, facility = Syslog::LOG_LOCAL6, options = {})
27
+ options[:uri] = options[:uri] || find_syslog_socket
28
+ @@logger ||= ::SyslogRuby::Logger.new(ident, facility, options)
29
+ if block_given?
30
+ yield @@logger
31
+ @@logger.close
32
+ else
33
+ @@logger
34
+ end
35
+ end
36
+
37
+ def find_syslog_socket
38
+ %w[
39
+ /dev/log
40
+ /var/run/syslog
41
+ ].find do |file|
42
+ file if File.exists?(file)
43
+ end
44
+ end
45
+
46
+ def info(*args)
47
+ @@logger.info(*args)
48
+ end
49
+
50
+ def log(*args)
51
+ @@logger.log(*args)
52
+ end
53
+
54
+ def inspect
55
+ @@logger.inspect
56
+ end
57
+
58
+ def close
59
+ raise RuntimeError.new "syslog not open" unless @@logger
60
+ @@logger.close
61
+ ensure
62
+ @@logger = nil
63
+ end
64
+
65
+ def reopen(*args)
66
+ self.close
67
+ self.open(*args)
68
+ end
69
+
70
+ def opened?
71
+ @@logger ? @@logger.opened? : false
72
+ end
73
+
74
+ def mask
75
+ @@logger.mask if @@logger
76
+ end
77
+
78
+ def mask=(priority_mask)
79
+ @@logger.mask = priority_mask if @@logger
80
+ end
81
+
82
+ def LOG_MASK(level)
83
+ @@logger.LOG_MASK(level) if @@logger
84
+ end
85
+
86
+ def LOG_UPTO(level)
87
+ @@logger.LOG_UPTO(level) if @@logger
88
+ end
89
+
90
+ def instance
91
+ @@logger if @@logger
92
+ end
93
+
94
+ def ident
95
+ @@logger.ident if @@logger
96
+ end
97
+
98
+ def options
99
+ @@logger.options if @@logger
100
+ end
101
+ end
102
+
103
+ self.setup_severity_methods
104
+ end
@@ -0,0 +1,4 @@
1
+ require 'syslog_ruby/version'
2
+ require 'syslog_ruby/severity'
3
+ require 'syslog_ruby/facility'
4
+ require 'syslog_ruby/logger'
@@ -0,0 +1,32 @@
1
+ require 'syslog_ruby/lookup_from_const'
2
+
3
+ module SyslogRuby
4
+ module Facility
5
+ extend LookupFromConst
6
+ KERN = 0 << 3
7
+ USER = 1 << 3
8
+ MAIL = 2 << 3
9
+ DAEMON = 3 << 3
10
+ AUTH = 4 << 3
11
+ SYSLOG = 5 << 3
12
+ LPR = 6 << 3
13
+ NEWS = 7 << 3
14
+ UUCP = 8 << 3
15
+ CRON = 9 << 3
16
+ AUTHPRIV = 10 << 3
17
+ FTP = 11 << 3
18
+ NTP = 12 << 3
19
+ SECURITY = 13 << 3
20
+ CONSOLE = 14 << 3
21
+ RAS = 15 << 3
22
+ LOCAL0 = 16 << 3
23
+ LOCAL1 = 17 << 3
24
+ LOCAL2 = 18 << 3
25
+ LOCAL3 = 19 << 3
26
+ LOCAL4 = 20 << 3
27
+ LOCAL5 = 21 << 3
28
+ LOCAL6 = 22 << 3
29
+ LOCAL7 = 23 << 3
30
+ NONE = SYSLOG
31
+ end
32
+ end
@@ -0,0 +1,169 @@
1
+ require 'socket'
2
+
3
+ module SyslogRuby
4
+ class Logger
5
+ attr_accessor :ident, :facility, :socket, :hostname, :mask, :log_uri
6
+ class << self
7
+ def setup_severity_methods
8
+ SyslogRuby::Severity.constants.each do |level_name|
9
+ level = SyslogRuby::Severity.const_get level_name
10
+
11
+ define_method(level_name.downcase) do |*args|
12
+ message = args.shift
13
+ facility = args.shift || send(:facility)
14
+ _log(facility, level, message)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ @@facilities = {}
21
+ @@severities = {}
22
+ Facility.setup_constants @@facilities
23
+ Severity.setup_constants @@severities
24
+ self.setup_severity_methods
25
+
26
+ def initialize(ident = 'ruby',
27
+ facility = Facility::LOCAL6,
28
+ options = {})
29
+ @ident = "#{ident}[#{$$}]"
30
+ @facility = _configure_facility(facility)
31
+ @mask = LOG_UPTO(Severity::DEBUG)
32
+ @log_uri = options.fetch(:uri, 'testing')
33
+ @hostname = Socket.gethostname.split('.').first
34
+ _open
35
+ end
36
+
37
+ def _configure_facility(facility)
38
+ case facility
39
+ when Integer || Fixnum
40
+ facility
41
+ when Symbol || String
42
+ Facility[Facility]
43
+ else
44
+ Facility::NONE
45
+ end
46
+ end
47
+
48
+ def log(level, message, *message_args)
49
+ level = case level
50
+ when Integer || Fixnum
51
+ level
52
+ when Symbol || String
53
+ Severity[level]
54
+ else
55
+ Severity::INFO
56
+ end
57
+ _log(facility, level, message, *message_args)
58
+ end
59
+
60
+ def close
61
+ raise RuntimeError.new "syslog not open" if socket.closed?
62
+ socket.close
63
+ end
64
+
65
+ def reopen(*args)
66
+ close && _open
67
+ end
68
+
69
+ def LOG_MASK(level)
70
+ Severity[level]
71
+ end
72
+
73
+ def LOG_UPTO(level)
74
+ (0...Severity[level]).inject(Integer(0)) do |mask, i|
75
+ mask|i
76
+ end
77
+ end
78
+
79
+ def opened?
80
+ socket && !socket.closed?
81
+ end
82
+
83
+ def options
84
+ 0
85
+ end
86
+
87
+ private
88
+
89
+ def _connect_unix(path)
90
+ @local = true
91
+ begin
92
+ @socket = UNIXSocket.new(path)
93
+ rescue Errno::EPROTOTYPE => e
94
+ raise unless e.message =~ /Protocol wrong type for socket/
95
+ @socket = Socket.new Socket::PF_UNIX, Socket::SOCK_DGRAM
96
+ @socket.connect Socket.pack_sockaddr_un(path)
97
+ end
98
+ end
99
+
100
+ def _connect_udp(uri)
101
+ host, port = uri[6..-1].split(':')
102
+ @socket = UDPSocket.new
103
+ @socket.connect(host, port.to_i)
104
+ end
105
+
106
+ def _connect_tcp(uri)
107
+ host, port = uri[6..-1].split(':')
108
+ @socket = TCPSocket.new(host, port.to_i)
109
+ end
110
+
111
+ def _connect_test
112
+ @socket = STDERR.dup.fileno
113
+ end
114
+
115
+ def _open
116
+ if @log_uri.start_with? '/'
117
+ _connect_unix(@log_uri)
118
+ elsif @log_uri.start_with? 'tcp://'
119
+ _connect_tcp(@log_uri)
120
+ elsif @log_uri.start_with? 'udp://'
121
+ _connect_udp(@log_uri)
122
+ elsif @log_uri == 'testing'
123
+ _connect_test
124
+ else
125
+ raise RuntimeError.new 'unknown :uri ' \
126
+ 'must be one of path, tcp://, or udp://'
127
+ end
128
+ rescue => e
129
+ STDERR.puts "An exception (#{e.message}) occured while connecting to: "\
130
+ "#{@log_uri}."
131
+ end
132
+
133
+ def _log(facility, level, message, *message_args)
134
+ return unless level & mask
135
+ msg = if message_args.length > 0
136
+ message % message_args
137
+ else
138
+ message
139
+ end
140
+
141
+ proto_msg = if @local
142
+ "<#{facility + level}> "\
143
+ "#{ident}" \
144
+ ": " \
145
+ "#{msg}\n"
146
+ else
147
+ "<#{facility + level}> "\
148
+ "#{Time.now.strftime('%b %d %H:%M:%S')} " \
149
+ "#{hostname} " \
150
+ "#{ident}" \
151
+ ": " \
152
+ "#{msg}\n"
153
+ end
154
+
155
+ begin
156
+ !!socket.write(proto_msg)
157
+ rescue => e
158
+ retries ||= 0
159
+ retries += 1
160
+ if retries < 10
161
+ _open
162
+ retry if opened?
163
+ else
164
+ STDERR.puts "syslog is down!! tried to log: #{proto_msg}"
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,34 @@
1
+ module SyslogRuby
2
+ module LookupFromConst
3
+ def setup_constants(dst)
4
+ constants.each do |pri|
5
+ cval = const_get pri
6
+
7
+ dst[pri] = cval
8
+ dst[pri.downcase] = cval
9
+
10
+ dst[:"LOG_#{pri.to_s}"] = cval
11
+ dst[:"LOG_#{pri.downcase.to_s}"] = cval
12
+ const_set :"LOG_#{pri.to_s}", cval
13
+
14
+ dst[pri.to_s] = cval
15
+ dst[pri.downcase.to_s] = cval
16
+
17
+ dst[cval] = cval
18
+ end
19
+
20
+ self.class.send(:define_method, :keys) do
21
+ dst.keys
22
+ end
23
+
24
+ self.class.send(:define_method, :[]) do |key|
25
+ value_none = const_get :NONE
26
+ dst.fetch(key, value_none)
27
+ end
28
+
29
+ self.class.send(:define_method, :[]=) do |key, value|
30
+ raise RuntimeError.new "#{self.class} is read only"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ require 'syslog_ruby/lookup_from_const'
2
+
3
+ module SyslogRuby
4
+ module Severity
5
+ extend LookupFromConst
6
+ EMERG = PANIC = 0
7
+ ALERT = 1
8
+ CRIT = 2
9
+ ERR = ERROR = 3
10
+ WARN = WARNING = 4
11
+ NOTICE = 5
12
+ INFO = 6
13
+ DEBUG = 7
14
+ NONE = 10
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module SyslogRuby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/syslog_ruby/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["John Skopis"]
6
+ gem.email = ["john.skopis@causes.com"]
7
+ gem.description = %q{Ruby Logger that sends directly to a variety of syslog endpoints}
8
+ gem.summary = %q{Ruby Logger that sends directly to a variety of syslog endpoints}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "syslog_ruby"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SyslogRuby::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syslog_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Skopis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby Logger that sends directly to a variety of syslog endpoints
14
+ email:
15
+ - john.skopis@causes.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - benchmark.rb
26
+ - lib/syslog_compat.rb
27
+ - lib/syslog_ruby.rb
28
+ - lib/syslog_ruby/facility.rb
29
+ - lib/syslog_ruby/logger.rb
30
+ - lib/syslog_ruby/lookup_from_const.rb
31
+ - lib/syslog_ruby/severity.rb
32
+ - lib/syslog_ruby/version.rb
33
+ - syslog_ruby.gemspec
34
+ homepage: ''
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.2.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Ruby Logger that sends directly to a variety of syslog endpoints
57
+ test_files: []
58
+ has_rdoc: