aboutsummaryrefslogtreecommitdiffstats
path: root/lib/logger
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2019-06-04 18:07:26 -0400
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2019-07-15 14:43:08 +0900
commit58065b87018a9d1ed972b8c856004bf75728da02 (patch)
treef3326bd6aab14ee2204f3fec842515821778cc1a /lib/logger
parentf4064a0a0c24734b1ec98e6e2dbdf5e38e856c41 (diff)
downloadruby-58065b87018a9d1ed972b8c856004bf75728da02.tar.gz
[ruby/logger] Add option to set the binary mode of the log device
Without binmode strings with incompatible encoding can't be written in the file. This is very common in applications that log user provided parameters. We need to allow changing the binnary mode because right now it is impossible to use the built-in log rotation feature when you provide a File object to the LogDevice, and if you provide a filename you can't have binmode. https://github.com/ruby/logger/commit/9114b3ac7e
Diffstat (limited to 'lib/logger')
-rw-r--r--lib/logger/log_device.rb5
1 files changed, 4 insertions, 1 deletions
diff --git a/lib/logger/log_device.rb b/lib/logger/log_device.rb
index c6dc43c11a..5661f5ce14 100644
--- a/lib/logger/log_device.rb
+++ b/lib/logger/log_device.rb
@@ -9,8 +9,9 @@ class Logger
attr_reader :filename
include MonitorMixin
- def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil)
+ def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: false)
@dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
+ @binmode = binmode
mon_initialize
set_dev(log)
if @filename
@@ -82,6 +83,7 @@ class Logger
else
@dev = open_logfile(log)
@dev.sync = true
+ @dev.binmode if @binmode
@filename = log
end
end
@@ -99,6 +101,7 @@ class Logger
logdev = File.open(filename, (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
logdev.flock(File::LOCK_EX)
logdev.sync = true
+ logdev.binmode if @binmode
add_log_header(logdev)
logdev.flock(File::LOCK_UN)
rescue Errno::EEXIST