I'm having a problem with python 3, I'm trying to log the stdout and the stderr to a log file. I was able to figure it out using http://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
The point of it was to have both the log file and the console output as normal, while using only the print statement. (I know that's not how you're supposed to do it, I'm trying to log someone else's code)
I came up with this:
import logging
import sys
import traceback
class StreamToLogger(object):
def __init__(self, logger, log_level, std):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
self.std = std
def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
self.std.write(line+"\n")
self.std.flush()
def flush(self):
return
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
filename="history.log",
filemode='a'
)
stdout_logger = logging.getLogger('STDOUT')
sl = StreamToLogger(stdout_logger, logging.INFO, sys.__stdout__)
sys.stdout = sl
stderr_logger = logging.getLogger('STDERR')
sl = StreamToLogger(stderr_logger, logging.ERROR, sys.__stderr__)
sys.stderr = sl
try:
import bot
#program that I am logging
except Exception as e:
traceback.print_exc()
sys.exit(1)
This worked great, until the log file got to huge to handle.
So my idea is to use the rotating log file thing. Unfortunately my implementation of the rotating log file what I came up with causes a stack overflow immediately after the first time the program prints anything.
This is the new code:
import logging
import sys
import traceback
import logging.handlers
class StreamToLogger(object):
def __init__(self, logger, log_level, std, handler):
self.handler = handler
self.logger = logger
self.log_level = log_level
self.linebuf = ''
self.std = std
def write(self, buf):
for line in buf.rstrip().splitlines():
self.handler.emit(line)
#^^STACK OVERFLOW^^
self.logger.log(self.log_level, line.rstrip())
self.std.write(line+"\n")
self.std.flush()
def flush(self):
return
hand = logging.handlers.TimedRotatingFileHandler("bot.log", when="S", interval=20)
#my attempt at handling
stdout_logger = logging.getLogger('STDOUT')
sl = StreamToLogger(stdout_logger, logging.INFO, sys.__stdout__, hand)
sys.stdout = sl
stderr_logger = logging.getLogger('STDERR')
sl = StreamToLogger(stderr_logger, logging.ERROR, sys.__stderr__, hand)
sys.stderr = sl
try:
import bot
except Exception as e:
traceback.print_exc()
sys.exit(1)
Anyone have any ideas that could help?
sys.stdout = sl
the piping that u did and then tried so the log file was created but a new error showed up something related to message format. – GIRISH RAMNANI Mar 8 at 5:20emit
method requires aLogRecord
but you are passing a string – GIRISH RAMNANI Mar 8 at 5:41