Skip to content

Instantly share code, notes, and snippets.

@daevski
Created November 11, 2022 17:47
Show Gist options
  • Save daevski/68cde5d5f08648270fccbd4e176c1804 to your computer and use it in GitHub Desktop.
Save daevski/68cde5d5f08648270fccbd4e176c1804 to your computer and use it in GitHub Desktop.
My favorite Python logging configuration
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