[Python] How to set different levels for different python log handlers

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página