Created
November 11, 2022 17:47
-
-
Save daevski/68cde5d5f08648270fccbd4e176c1804 to your computer and use it in GitHub Desktop.
My favorite Python logging configuration
This file contains 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
from sys import stdout as sys_stdout | |
import time | |
import logging | |
# Outputs to CONSOLE and FILE; see "handlers" portion of config. | |
# Filename: 2022-11-11_12-37.log | |
# Contents: [2022-11-11] [12:32] INFO: set completion-ignore-case On | |
timestamp = time.strftime("%Y-%m-%d_%H-%M") | |
# path comes from a yaml config file | |
logging_file = Path(f"{config['provisioner_log_location']}") / f"{timestamp}.log" | |
logging.basicConfig( | |
level=logging.DEBUG, | |
datefmt="[%Y-%m-%d] [%H:%M]", | |
format="%(asctime)s %(levelname)s: %(message)s [PID: %(process)d]", | |
handlers=[ | |
logging.FileHandler(logging_file), | |
logging.StreamHandler(sys_stdout), | |
], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment