Managing loggers with Python logging
Asked Answered
C

2

3

I'm writing a server app which should be able to log at different levels both on the console and a log file.

The problem is, if logging.basicConfig() is set it will log to the console but it must be set in the main thread.

It can also be set with logging.basicConfig(filename='logger.log') to write to a file.

Setting a handle either for console logging (logging.StreamHandler()) or file logging (logging.FileHandler()) complements the logging.baseconfig() option set.

The problem is, that the settings are not independent. What I mean is, the loglevel of logging.baseConfig() must include the Handler level, or it wont be logged.

So if I set the baseConfig to log to file, and a StreamHandler to log to console, the file loglevel must be lower than the console level. (Also, the basicConfig option logs all other logs.)

I tried to create two Handles, one for the console and one for the log file, they work, but whatever log type is specified by basicConfig() will still be displayed duplicating messages.

Is there a way to disable the output of basicConfig()? Or any other way to implement these options?

Coloquintida answered 9/11, 2010 at 16:39 Comment(0)
C
4

You don't say in your question exactly what levels you want on your console and file logging. However, you don't need to call basicConfig(), as it's only a convenience function. You can do e.g. (code just typed in, not tested):

import logging

logger = logging.getLogger(__name__)
configured = False

def configure_logging():
    global configured
    if not configured:
        logger.setLevel(logging.DEBUG) # or whatever
        console = logging.StreamHandler()
        file = logging.FileHandler('/path/to/file')
        #set a level on the handlers if you want;
        #if you do, they will only output events that are >= that level
        logger.addHandler(console)
        logger.addHandler(file)
        configured = True

Events are passed to the logger first, and if an event is to be processed (due to comparing the level of the logger and the event) then the event is passed to each handler of the logger and all its ancestors' handlers as well. If a level is set on a handler the event may be dropped by that handler, otherwise it will output the event.

Clot answered 9/11, 2010 at 22:42 Comment(0)
B
0

Find the below example code to handle the logging with all kind of exceptions

import mysql.connector
import logging
logging.basicConfig(filename=r'C:\Users\root\Desktop\logs.txt',level=logging.DEBUG,format='%(asctime)s,%(levelname)s:%(message)s',datefmt='%d-%m-%Y %H:%M:%S')

while True:
    try:
        mydb=mysql.connector.connect(host='localhost',user='root',passwd='password123', database='shiva')

        mycursor=mydb.cursor()
        logging.info("Connected mysql db successfully...\n")
        mycursor.execute("show databases")
        mycursor.execute("Create table employee(name varchar(20), salary float(20))")
        mydb.commit()
    except Exception as e:
        logging.info("Trying to Connect MysqlDB...")
        logging.critical("Error Occured While Connecting...\n\n" "CAUSEDBY: "+str(e))
        logging.warning("Check Login Credentials.")
        
Boughton answered 19/11, 2020 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.