diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2021-06-02 20:57:21 +0200 |
---|---|---|
committer | Matthias Hecker <36882671+mattzque@users.noreply.github.com> | 2021-06-05 18:06:45 +0200 |
commit | 03d0839fa48fd9424b7a968986e96b7e69e84d25 (patch) | |
tree | 86e042cf3baf2ec54be17599a748123eafdc6ec8 /lib/rbot | |
parent | bfa60f1846df4cc8d1713c0143477fea791da87c (diff) |
logger: fix STDERR recursion
Overriding (as we should do) the standard error write method when
daemonizing introduces a recursion in the logger, due to the
persistent presence of the console logger.
The solution is obviously to stop logging on console
after daemonization.
Diffstat (limited to 'lib/rbot')
-rw-r--r-- | lib/rbot/ircbot.rb | 5 | ||||
-rw-r--r-- | lib/rbot/logger.rb | 34 |
2 files changed, 26 insertions, 13 deletions
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index 91586c6a..817acd71 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -431,7 +431,10 @@ class Bot LoggerManager.instance.set_logfile(@logfile, @config['log.keep'], @config['log.max_size']) if $daemonize - log "Redirecting standard input/output/error" + log "Redirecting standard input/output/error, console logger disabled" + LoggerManager.instance.flush + LoggerManager.instance.disable_console_logger + [$stdin, $stdout, $stderr].each do |fd| begin fd.reopen "/dev/null" diff --git a/lib/rbot/logger.rb b/lib/rbot/logger.rb index fd6d485f..d4717aa6 100644 --- a/lib/rbot/logger.rb +++ b/lib/rbot/logger.rb @@ -14,12 +14,22 @@ class Bot class LoggerManager include Singleton + def enable_console_logger + @console_logger = Logger.new(STDERR) + @console_logger.datetime_format = @dateformat + @console_logger.level = Logger::Severity::DEBUG + end + + def disable_console_logger + @console_logger.close if @console_logger + @console_logger = nil + end + def initialize @dateformat = "%Y/%m/%d %H:%M:%S" - @logger = Logger.new(STDERR) - @logger.datetime_format = @dateformat - @logger.level = Logger::Severity::DEBUG + enable_console_logger + @file_logger = nil @queue = Queue.new @@ -27,26 +37,26 @@ class Bot end def set_logfile(filename, keep, max_size) + # close previous file logger if present + @file_logger.close if @file_logger + @file_logger = Logger.new(filename, keep, max_size*1024*1024) @file_logger.datetime_format = @dateformat - @file_logger.level = @logger.level + @file_logger.level = @console_logger.level + # make sure the thread is running, which might be false after a fork # (conveniently, we call set_logfile right after the fork) start_thread end def set_level(level) - @logger.level = level - if @file_logger - @file_logger.level = level - end + @console_logger.level = level if @console_logger + @file_logger.level = level if @file_logger end def sync_log(severity, message = nil, progname = nil) - @logger.add(severity, message, progname) - if @file_logger - @file_logger.add(severity, message, progname) - end + @console_logger.add(severity, message, progname) if @console_logger + @file_logger.add(severity, message, progname) if @file_logger end def async_log(severity, message=nil, who_pos=1) |