Skip to content
Browse files

ST: Added syslog adapter for Yell

  • Loading branch information...
1 parent 0e0b51a commit c4d3d20947a340e0f7902d73bbf157234bd5b4fe @rudionrails committed
View
37 lib/yell-adapters-syslog.rb
@@ -1,9 +1,30 @@
-require "yell-adapters-syslog/version"
-
-module Yell
- module Adapters
- module Syslog
- # Your code goes here...
- end
- end
+# encoding: utf-8
+
+# Copyright (c) 2012 Rudolf Schmidt
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+require 'yell'
+
+require File.dirname(__FILE__) + '/yell/adapters/syslog'
+
+module Yell #:nodoc:
end
+
View
7 lib/yell-adapters-syslog/version.rb
@@ -1,7 +0,0 @@
-module Yell
- module Adapters
- module Syslog
- VERSION = "0.0.1"
- end
- end
-end
View
138 lib/yell/adapters/syslog.rb
@@ -0,0 +1,138 @@
+# encoding: utf-8
+
+require 'syslog'
+
+module Yell #:nodoc:
+ module Adapters #:nodoc:
+
+ class Syslog < Yell::Adapters::Base
+
+ # Syslog severities
+ #
+ # `man syslog` to see the severities
+ Severities = [7, 6, 4, 3, 2, 1]
+
+ # Map Syslog severities to internal represenation
+ # 'DEBUG' => 7
+ # 'INFO' => 6
+ # 'WARN' => 4
+ # 'ERROR' => 3
+ # 'FATAL' => 2
+ # 'UNKNOWN' => 1
+ SeverityMap = Hash[ *(Yell::Severities.zip(Severities).flatten) ]
+
+ # Map Syslog options to internal representation
+ OptionMap = {
+ :cons => ::Syslog::LOG_CONS,
+ :ndelay => ::Syslog::LOG_NDELAY,
+ :nowait => ::Syslog::LOG_NOWAIT,
+ :odelay => ::Syslog::LOG_ODELAY,
+ :perror => ::Syslog::LOG_PERROR,
+ :pid => ::Syslog::LOG_PID
+ }
+
+ # Map Syslog facilities to internal represenation
+ FacilityMap = {
+ :auth => ::Syslog::LOG_AUTH,
+ :authpriv => ::Syslog::LOG_AUTHPRIV,
+ # :console => ::Syslog::LOG_CONSOLE, # described in 1.9.3 docu, but not defined
+ :cron => ::Syslog::LOG_CRON,
+ :daemon => ::Syslog::LOG_DAEMON,
+ :ftp => ::Syslog::LOG_FTP,
+ :kern => ::Syslog::LOG_KERN,
+ # :lrp => ::Syslog::LOG_LRP, # described in 1.9.3 docu, but not defined
+ :mail => ::Syslog::LOG_MAIL,
+ :news => ::Syslog::LOG_NEWS,
+ # :ntp => ::Syslog::LOG_NTP, # described in 1.9.3 docu, but not defined
+ # :security => ::Syslog::LOG_SECURITY, # described in 1.9.3 docu, but not defined
+ :syslog => ::Syslog::LOG_SYSLOG,
+ :user => ::Syslog::LOG_USER,
+ :uucp => ::Syslog::LOG_UUCP,
+ :local0 => ::Syslog::LOG_LOCAL0,
+ :local1 => ::Syslog::LOG_LOCAL1,
+ :local2 => ::Syslog::LOG_LOCAL2,
+ :local3 => ::Syslog::LOG_LOCAL3,
+ :local4 => ::Syslog::LOG_LOCAL4,
+ :local5 => ::Syslog::LOG_LOCAL5,
+ :local6 => ::Syslog::LOG_LOCAL6,
+ :local7 => ::Syslog::LOG_LOCAL7
+ }
+
+ def initialize( options = {}, &block )
+ ident options[:ident] || $0
+ options options[:options] || [:pid, :cons]
+ facility options[:facility]
+
+ super( options, &block )
+ end
+
+ def stream
+ @stream ||= _new_stream
+ end
+
+ def close
+ @stream.close if @stream.respond_to? :close
+ rescue
+ # do nothing
+ ensure
+ @stream = nil
+ end
+
+ # Identify the calling program
+ def ident( val = nil )
+ @ident = val
+ end
+
+ # Set the log facility for Syslog
+ #
+ # See {Yell::Adapters::Syslog::OptionMap OptionMap} for allowed values.
+ #
+ # @example Direct or Symbol
+ # facility( Syslog::LOG_NDELAY )
+ # facility( :ndelay )
+ #
+ # @example Multiple
+ # facility( :ndelay, Syslog::LOG_NDELAY ):
+ def options( *values )
+ @syslog_options = values.flatten.map do |v|
+ v.is_a?(Symbol) ? OptionMap[v] : v
+ end.inject { |a, b| a | b }
+ end
+
+ # Set the log facility for Syslog
+ #
+ # See {Yell::Adapters::Syslog::FacilityMap FacilityMap} for allowed values.
+ # @example Direct or Symbol
+ # facility( :user )
+ # facility( Syslog::LOG_CONSOLE )
+ #
+ # @example Multiple
+ # facility( :user, Syslog::LOG_CONSOLE )
+ def facility( *values )
+ @facility = values.flatten.map do |v|
+ v.is_a?(Symbol) ? FacilityMap[v] : v
+ end.inject { |a, b| a | b }
+ end
+
+
+ private
+
+ def write!( event )
+ stream.log( SeverityMap[event.level], event.message )
+ rescue Exception => e
+ close
+
+ # re-raise the exception
+ raise( e, caller )
+ end
+
+ def _new_stream
+ close if ::Syslog.opened?
+ ::Syslog.open( @ident, @syslog_options, @facility )
+ end
+
+ end
+
+ register( :syslog, Yell::Adapters::Syslog )
+ end
+end
View
12 lib/yell/adapters/syslog/version.rb
@@ -0,0 +1,12 @@
+# encoding: utf-8
+
+module Yell #:nodoc:
+ module Adapters #:nodoc:
+
+ class Syslog
+ VERSION = "0.4.0"
+
+ end
+ end
+end
+
View
16 yell-adapters-syslog.gemspec
@@ -1,24 +1,22 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
-require "yell-adapters-syslog/version"
+require "yell/adapters/syslog/version"
Gem::Specification.new do |s|
s.name = "yell-adapters-syslog"
s.version = Yell::Adapters::Syslog::VERSION
s.authors = ["Rudolf Schmidt"]
- s.email = ["me@rudionrails.com"]
- s.homepage = ""
- s.summary = %q{TODO: Write a gem summary}
- s.description = %q{TODO: Write a gem description}
- s.rubyforge_project = "yell-adapters-syslog"
+ s.homepage = "http://rubygems.org/gems/yell"
+ s.summary = %q{Yell - Your Extensible Logging Library }
+ s.description = %q{Syslog adapter for Yell}
+
+ s.rubyforge_project = "yell"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- # specify any dependencies here; for example:
- # s.add_development_dependency "rspec"
- # s.add_runtime_dependency "rest-client"
+ s.add_runtime_dependency "yell", "~> 0.4"
end

0 comments on commit c4d3d20

Please sign in to comment.
Something went wrong with that request. Please try again.