Python logging example (location and setting Log Level)


You can use the following code piece to log starting from DEBUG level;

”’
Created on Jul 24, 2013

@author: tuna
”’
#python logging example

import logging

#logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(filename=’C:\\test.log’,level=logging.DEBUG)
logging.debug(‘Debug log’)
logging.info(‘Info log’)
logging.warning(‘Warning log’)
logging.error(‘Error log’)
logging.critical(‘Critical log’)

DEBUG:root:Debug log
INFO:root:Info log
WARNING:root:Warning log
ERROR:root:Error log
CRITICAL:root:Critical log

or if you want to log only errors just do;

”’
Created on Jul 24, 2013

@author: tuna
”’
#python logging example

import logging

#logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(filename=’C:\\test.log’,level=logging.ERROR)
logging.debug(‘Debug log’)
logging.info(‘Info log’)
logging.warning(‘Warning log’)
logging.error(‘Error log’)
logging.critical(‘Critical log’)

ERROR:root:Error log
CRITICAL:root:Critical log

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s