Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to log to multiple facilities with several logger instances #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pkg
*.gemspec
28 changes: 20 additions & 8 deletions lib/syslog-logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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=

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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'
Expand Down
41 changes: 41 additions & 0 deletions syslog-logger.gemspec
Original file line number Diff line number Diff line change
@@ -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{[email protected]}
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

14 changes: 5 additions & 9 deletions test/test_syslog_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ 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

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
Expand Down Expand Up @@ -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
Expand Down