简体   繁体   中英

Python: logging.basicConfig has no effect

My file:

#!/usr/bin/env python3
import logging
logging.basicConfig(level=logging.DEBUG)
print("START")
logging.debug("DEBUG")
logging.info("INFO")
logging.warning("WARNING")
logging.error("ERROR")
logging.critical("CRITICAL")
print("END")

The output:

START
WARNING:root:WARNING
ERROR:root:ERROR
CRITICAL:root:CRITICAL
END

I can change the level, specify a format or a filename, all has no effect. When I run this code with Python2, all works as expected. It also works on different systems. I think it used to work on my system too.

It seems like there is some log configuration, but I can't find it. The script is in an empty directory. There are no suspicious environment variables. Is there any other location for logging configuration or can there be any other reason for this behavior?

To be clear: The code above is the complete content of the file. I call it via /usr/bin/python3.8 test.py and get the mentioned output. The same, when I do it by REPL.

Try This out,

import logging

# Logger
logger = logging.getLogger('Logger')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler("Logger")
formatter = logging.Formatter('%(asctime)s | %(funcName)s | PID:%(process)d | %(levelname)s | %(message)s',
                              datefmt='%Y%m%d-%H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)

#Logger


print("START")
logger.debug("DEBUG")
logger.info("INFO")
logger.warning("WARNING")
logger.error("ERROR")
logger.critical("CRITICAL")
print("END")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM