Python logging manages different levels of log printing and storing instances
- 2020-07-21 08:47:39
- OfStack
Python's built-in module, logging, manages different levels of log printing and storage. It is very convenient
import logging
logging.basicConfig(level = logging.DEBUG,
format = '%(asctime)s %(filename)s[line:%(lineno)d]%(levelname)s %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = './logcheck.log',
filemode = 'w')
###############################################################################
#define one StreamHandler, set the log mode
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
###############################################################################
filePath = r'C:\ddms.bat'
logging.error('Open file failed!')
logging.warn('sort mode disabled')
logging.debug('%s' % filePath)
logging.info('xml file generated successfully!')
Operation results:
root : ERROR Open file failed!
root : WARNING sort mode disabled
root : INFO xml file generated successfully!
conclusion
That's all this article has to say about Python logging managing different levels of log printing and storing instances, hoping to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!