Python using basicConfig method to log to console and file Python using basicConfig method to log to console and file python python

Python using basicConfig method to log to console and file


Try this working fine(tested in python 2.7) for both console and file

# set up logging to filelogging.basicConfig(     filename='log_file_name.log',     level=logging.INFO,      format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',     datefmt='%H:%M:%S' )# set up logging to consoleconsole = logging.StreamHandler()console.setLevel(logging.DEBUG)# set a format which is simpler for console useformatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')console.setFormatter(formatter)# add the handler to the root loggerlogging.getLogger('').addHandler(console)logger = logging.getLogger(__name__)


I can't reproduce it on Python 3.3. The messages are written both to the screen and the 'example2.log'. On Python <3.3 it creates the file but it is empty.

The code:

from logging_tree import printout  # pip install logging_treeprintout()

shows that FileHandler() is not attached to the root logger on Python <3.3.

The docs for logging.basicConfig() say that handlers argument is added in Python 3.3. The handlers argument isn't mentioned in Python 3.2 documentation.


In the example below, you can specify the log destination based on its level. For example, the code below lets all logs over the INFO level go to the log file, and all above ERROR level goes to the console.

import logginglogging.root.handlers = []logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO , filename='ex.log')# set up logging to consoleconsole = logging.StreamHandler()console.setLevel(logging.ERROR)# set a format which is simpler for console useformatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s')console.setFormatter(formatter)logging.getLogger("").addHandler(console)logging.debug('debug')logging.info('info')logging.warning('warning')logging.error('error')logging.exception('exp')