1

I'm trying to figure out how to capture messages generated by Python/NumPy intractive shell when running my script. I would like to log all generated by console messages (errors, warnings) to same file as defined in my code log messages with time stamps:

def LogToFile():
 global logger
 logger = logging.getLogger('MyApp')
 logger.setLevel(logging.DEBUG)
 file_log_handler = RotatingFileHandler('logfile.log', maxBytes=1024, backupCount=5)
 logger.addHandler(file_log_handler)
 stderr_log_handler = logging.StreamHandler()
 logger.addHandler(stderr_log_handler)
 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 file_log_handler.setFormatter(formatter)
 stderr_log_handler.setFormatter(formatter)
 return logger
  • Does [this](http://stackoverflow.com/a/4675744/7207392) help in any way? And re errors, [this](http://stackoverflow.com/a/22434262/7207392)? – Paul Panzer Feb 19 '17 at 19:33
  • Thanks Paul, but not really. Your method will just redirect console output to separate file. I'm looking for a way to use standard Python logging facilities in order to have single rotating log file collecting all info with time stamps, etc. – Yuri Belenky Feb 20 '17 at 08:29

1 Answers1

0

Afaik, you'd have to specify this in basicConfig, not in your logger:

logging.basicConfig(filename=LOG_FILE,
                    level=logging.DEBUG)

before you do

logger = logging.getLogger('MyApp')

logger.setLevel(logging.DEBUG)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93