Skip to content

Commit

Permalink
Merge pull request #4 from bdurand/handle-non-strings
Browse files Browse the repository at this point in the history
Always cast values to strings before logging to syslog
  • Loading branch information
bdurand authored Nov 12, 2023
2 parents 2314b3a + 61de56c commit 33f1e4d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.0.0
## 1.1.1

### Changed

- Always cast values to strings before logging to syslog (thanks @eremeyev).

## 1.1.0

### Added

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.1.1
6 changes: 3 additions & 3 deletions lib/lumberjack_syslog_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def initialize(options = {})
end

def write(entry)
message = @template.call(entry).gsub(PERCENT, ESCAPED_PERCENT)
message = @template.call(entry).to_s.gsub(PERCENT, ESCAPED_PERCENT)
@@lock.synchronize do
syslog = open_syslog(entry.progname)
begin
Expand All @@ -93,13 +93,13 @@ def close
def open_syslog(progname) # :nodoc:
syslog_impl = syslog_implementation
if syslog_impl.opened?
if (progname.nil? || syslog_impl.ident == progname) && @syslog_facility == syslog_impl.facility && @syslog_options == syslog_impl.options
if (progname.nil? || syslog_impl.ident == progname.to_s) && @syslog_facility == syslog_impl.facility && @syslog_options == syslog_impl.options
return syslog_impl
else
syslog_impl.close
end
end
syslog = syslog_impl.open(progname, @syslog_options, @syslog_facility)
syslog = syslog_impl.open(progname.to_s, @syslog_options, @syslog_facility)
syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_DEBUG)
syslog
end
Expand Down
9 changes: 9 additions & 0 deletions spec/lumberjack_syslog_device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@
expect(syslog.output).to eq [[Syslog::LOG_WARNING, 'message 100%% [foo:"bar"]']]
end

it "should convert template output to strings" do
device = Lumberjack::SyslogDevice.new(template: lambda { |e| e.message })
allow(device).to receive(:syslog_implementation).and_return(syslog)
message = {foo: "bar"}
entry = Lumberjack::LogEntry.new(time, Lumberjack::Severity::WARN, message, "lumberjack_syslog_device_spec", 12345, {})
device.write(entry)
expect(syslog.output).to eq [[Syslog::LOG_WARNING, message.to_s]]
end

it "should convert lumberjack severities to syslog severities" do
syslog = MockSyslog.new
device = Lumberjack::SyslogDevice.new
Expand Down

0 comments on commit 33f1e4d

Please sign in to comment.