Created
April 8, 2025 22:40
-
-
Save ariel-co/129977b423ac5bdd73df51a219475c29 to your computer and use it in GitHub Desktop.
logging to both stdout and file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging, logging.config | |
| logging.config.dictConfig({ | |
| "version": 1, | |
| "formatters": { | |
| "common": { | |
| "format": (f"%(asctime)s%(msecs)03d %(process)7d " | |
| f"[{JOB_ID or 0:4d}] %(levelname)-7s %(message)s"), | |
| "datefmt": "%Y-%m-%dT%H:%M:%S" }}, | |
| "handlers": { | |
| "stdout": { | |
| "class": "logging.StreamHandler", | |
| "formatter": "common", | |
| "stream": "ext://sys.stdout" }, | |
| "file": { | |
| "class": "logging.FileHandler", | |
| "formatter": "common", | |
| "filename": os.path.join( | |
| SCRIPT_DIR, "log", | |
| ( f"{SCRIPT_START_TIME:%Y-%m-%d_%H_%M}-" | |
| f"{SCRIPT_BASENAME}-{JOB_ID or 'main'}.log")) } }, | |
| "loggers": { | |
| '': { | |
| "handlers": ["stdout", "file"], | |
| "level": logging.DEBUG if args.debug else logging.INFO } } }) | |
| # logging=logging.getLogger(__name__) | |
| logger=logging.getLogger() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment