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

Timestamp #4

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
14 changes: 3 additions & 11 deletions lib/syslog-formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# [Time.mSec] [SeverityLabel]: message

class Logger::SyslogFormatter < Logger::Formatter
Format = "[%s] [%5s]: %s\n"
Format = "[%5s] %s\n"

attr_accessor :datetime_format

Expand All @@ -12,19 +12,11 @@ def initialize
end

def call(severity, time, progname, msg)
Format % [format_datetime(time), severity, msg2str(msg)]
Format % [severity, msg2str(msg)]
end

protected

def format_datetime(time)
if @datetime_format.nil?
time.strftime("%H:%M:%S.") << "%06d " % time.usec
else
time.strftime(@datetime_format)
end
end

def msg2str(msg)
case msg
when ::String
Expand All @@ -37,4 +29,4 @@ def msg2str(msg)
end
end

end
end
9 changes: 3 additions & 6 deletions test/test_syslog_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class << MockSyslog
Logger::Syslog::LOGGER_MAP.values.uniq.each do |level|
eval <<-EOM
def #{level}(message)
@line = "#{level.to_s.upcase} - \#{message}"
@line = message
end
EOM
end
Expand Down Expand Up @@ -464,11 +464,8 @@ class Log
attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
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'
return unless /\A\[\s*([^\]]+)\] ([\x0-\xff]*)/ =~ @line
@severity, @msg = $1, $2
end
end

Expand Down