I've read a few posts on this but I'm still confused. I have this logging setup:
import logging class MongoHandler(logging.Handler): def __init__(self): logging.Handler.__init__(self) from pymongo import Connection self.db = Connection('db_server').db_name def emit(self, record): try: self.db.Logging.save(record.__dict__) except: print 'Logging Error: Unable to save log entry to db' mh = MongoHandler() sh = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(threadName)s - %(levelname)s - %(message)s') sh.setFormatter(formatter) log = logging.getLogger('DeviceMonitor_%s' % hostname) log.addHandler(mh) log.addHandler(sh) log.setLevel(logging.INFO)
I want to be able to set a different level for the StreamHandler and the MongoHandler. Is that possible or do I need to have a second Logger obj?
Python logger with multiple handlers (stream, file, etc.), so you can write log to multiple targets at once · GitHub.
There are six log levels in Python; each level is associated with an integer that indicates the log severity: NOTSET=0, DEBUG=10, INFO=20, WARN=30, ERROR=40, and CRITICAL=50. All the levels are rather straightforward (DEBUG < INFO < WARN ) except NOTSET, whose particularity will be addressed next.
You can set a different logging level for each logging handler but it seems you will have to set the logger's level to the "lowest". In the example below I set the logger to DEBUG, the stream handler to INFO and the TimedRotatingFileHandler to DEBUG. So the file has DEBUG entries and the stream outputs only INFO. You can't direct only DEBUG to one and only INFO to another handler. For that you'll need another logger.
logger = logging.getLogger("mylog") formatter = logging.Formatter( '%(asctime)s | %(name)s | %(levelname)s: %(message)s') logger.setLevel(logging.DEBUG) stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.INFO) stream_handler.setFormatter(formatter) logFilePath = "my.log" file_handler = logging.handlers.TimedRotatingFileHandler( filename=logFilePath, when='midnight', backupCount=30) file_handler.setFormatter(formatter) file_handler.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(stream_handler) logger.info("Started"); try: x = 14 y = 0 z = x / y except Exception as ex: logger.error("Operation failed.") logger.debug( "Encountered {0} when trying to perform calculation.".format(ex)) logger.info("Ended");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With