0

I'm using Python's logging module to log some debug strings to a file that give me good log I use this script attached but it don't give me any output to screen how can I show all print in the screen and print it to the log with the formatt ? any help?

class StreamToLogger(object):
   """
   Fake file-like stream object that redirects writes to a logger instance.
   """
   def __init__(self, logger, log_level=logging.INFO):
      self.logger = logger
      self.log_level = log_level
      self.linebuf = ''

   def write(self, buf):
      for line in buf.rstrip().splitlines():
         self.logger.log(self.log_level, line.rstrip())

logging.basicConfig(
   level=logging.DEBUG,
   format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
   filename="out.log",
   filemode='a'
)

stdout_logger = logging.getLogger('STDOUT')
sl = StreamToLogger(stdout_logger, logging.INFO)
sys.stdout = sl

#stderr_logger = logging.getLogger('STDERR')
#sl = StreamToLogger(stderr_logger, logging.ERROR)
#sys.stderr = sl
  • Why not just configure additional parent logger as described in the [documentation](https://docs.python.org/2/howto/logging.html)? – BartoszKP May 16 '14 at 14:43
  • Maybe your question was answered here: http://stackoverflow.com/questions/13733552/logger-configuration-to-log-to-file-and-print-to-stdout – Fabzi Sep 19 '14 at 13:05

0 Answers0