From 383f5fd02eecd65e6c3f4ef9186e085666339d04 Mon Sep 17 00:00:00 2001 From: Valentin Mihov Date: Wed, 2 May 2012 16:46:58 +0300 Subject: [PATCH 1/2] Making it possible to log to have several loggers that log to different facilities. The facility argument is taken into account when logging a message to the syslog and is not only used as a default when creating the syslog for the first time. --- lib/syslog-logger.rb | 28 ++++++++++++++++++++-------- test/test_syslog_logger.rb | 14 +++++--------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/syslog-logger.rb b/lib/syslog-logger.rb index 041a2ad..6bdac31 100644 --- a/lib/syslog-logger.rb +++ b/lib/syslog-logger.rb @@ -27,12 +27,12 @@ class Logger::Syslog # Maps Logger warning types to syslog(3) warning types. LOGGER_MAP = { - :unknown => :alert, - :fatal => :crit, - :error => :err, - :warn => :warning, - :info => :info, - :debug => :debug + :unknown => Syslog::LOG_ALERT, + :fatal => Syslog::LOG_CRIT, + :error => Syslog::LOG_ERR, + :warn => Syslog::LOG_WARNING, + :info => Syslog::LOG_INFO, + :debug => Syslog::LOG_DEBUG } # Maps Logger log levels to their values so we can silence. @@ -84,6 +84,10 @@ def datetime_format # when no formatter is set. attr_accessor :formatter + # This is the facility the logger will log to. You can have several loggers + # that log to different facilities. + attr_accessor :facility + alias sev_threshold level alias sev_threshold= level= @@ -98,6 +102,7 @@ def initialize(program_name = 'rails', facility = Syslog::LOG_USER, logopts=nil) @formatter = nil @progname = nil @level = Logger::DEBUG + @facility = facility return if defined? SYSLOG self.class.const_set :SYSLOG, Syslog.open(program_name, logopts, facility) @@ -117,7 +122,7 @@ def add(severity, message = nil, progname = nil, &block) progname = @progname end end - SYSLOG.send(LEVEL_LOGGER_MAP[severity], format_message(format_severity(severity), Time.now, progname, clean(message))) + SYSLOG.log(LEVEL_LOGGER_MAP[severity] | @facility, format_message(format_severity(severity), Time.now, progname, clean(message))) true end @@ -139,7 +144,14 @@ def <<(message) private # Severity label for logging. (max 5 char) - SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY) + SEV_LABEL = { + Logger::DEBUG => "DEBUG", + Logger::INFO => "INFO", + Logger::WARN => "WARN", + Logger::ERROR => "ERROR", + Logger::FATAL => "FATAL", + Logger::UNKNOWN => "ANY" + } def format_severity(severity) SEV_LABEL[severity] || 'ANY' diff --git a/test/test_syslog_logger.rb b/test/test_syslog_logger.rb index d768f76..f860121 100644 --- a/test/test_syslog_logger.rb +++ b/test/test_syslog_logger.rb @@ -8,14 +8,6 @@ class << MockSyslog @line = nil - Logger::Syslog::LOGGER_MAP.values.uniq.each do |level| - eval <<-EOM - def #{level}(message) - @line = "#{level.to_s.upcase} - \#{message}" - end - EOM - end - attr_reader :line attr_reader :program_name @@ -23,6 +15,11 @@ def open(program_name) @program_name = program_name end + def log(priority, message) + priority_name = Logger::Syslog::LOGGER_MAP.to_a.find { |k,v| (v & priority) == v }[0] + @line = "#{priority_name.to_s.upcase} - #{message}" + end + def reset @line = '' end @@ -466,7 +463,6 @@ def initialize(line) @line = line return unless /\A(\w+) - (.*)\Z/ =~ @line severity, @msg = $1, $2 - severity = Logger::Syslog::LOGGER_MAP.invert[severity.downcase.intern] @severity = severity.to_s.upcase @severity = 'ANY' if @severity == 'UNKNOWN' end From ebc460e2cdc071dc76fa6313b78522f2ba1af0f8 Mon Sep 17 00:00:00 2001 From: Valentin Mihov Date: Wed, 2 May 2012 17:57:08 +0300 Subject: [PATCH 2/2] Adding a gemspec, so that the repo can be used as a source in a Gemfile --- .gitignore | 1 - syslog-logger.gemspec | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 syslog-logger.gemspec diff --git a/.gitignore b/.gitignore index 0fea3eb..5fff1d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ pkg -*.gemspec diff --git a/syslog-logger.gemspec b/syslog-logger.gemspec new file mode 100644 index 0000000..66f2adf --- /dev/null +++ b/syslog-logger.gemspec @@ -0,0 +1,41 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{syslog-logger} + s.version = "1.6.8" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Eric Hodel; Chris Powell; Matthew Boeh; Ian Lesperance; Dana Danger; Brian Smith; Ashley Martens"] + s.date = %q{2012-05-02} + s.description = %q{An improved Logger replacement that logs to syslog. It is almost drop-in with a few caveats.} + s.email = %q{teamplatform@ngmoco.com} + s.extra_rdoc_files = [ + "README.rdoc" + ] + s.files = [ + "README.rdoc", + "lib/syslog-formatter.rb", + "lib/syslog-logger.rb" + ] + s.homepage = %q{http://github.com/ngmoco/syslog_logger} + s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "SyslogLogger", "--main", "README.rdoc"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.4.2} + s.summary = %q{An improved Logger replacement that logs to syslog. It is almost drop-in with a few caveats.} + s.test_files = [ + "test/test_syslog_logger.rb" + ] + + if s.respond_to? :specification_version then + s.specification_version = 3 + + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + else + end + else + end +end +