Reputation: 21
Have this part of code:
import logging
logFormatter = logging.Formatter(u'#%(levelname)-8s [%(asctime)s] [LINE:%(lineno)d] %(filename)-8s %(message)s',)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
How I can handle logging.error calling, for example to save log in database or do something else
Upvotes: 0
Views: 118
Reputation: 40912
Probably the simplest answer is to use multiple handlers, setup with different logging levels. From the logging
docs, here is a perfect example of what you are trying to achieve:
import logging
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
The example provides both the DEBUG
and ERROR
level handler formatting for these log level types.
Perhaps another solution is to switch to using dictConfig
for handling the python logging
module configuration. That would handle the structure in a dictionary
type way, and you may be able to provide multiple values to the level
attribute.
Upvotes: 1