Skip to content

Instantly share code, notes, and snippets.

@jamesWalker55
Created March 1, 2025 15:25
Show Gist options
  • Save jamesWalker55/07b124f488ebc6668b343fc49b74bb77 to your computer and use it in GitHub Desktop.
Save jamesWalker55/07b124f488ebc6668b343fc49b74bb77 to your computer and use it in GitHub Desktop.
Opinionated setup for Python's logging module.
# Get a logger, put this in the beginning of every file:
import logging
log = logging.getLogger(__name__)
del logging
# Function to set up logging
def setup_logging():
import datetime
import logging
log.setLevel(logging.DEBUG)
# Custom log output format
msg_fmt = "[%(asctime)s %(levelname)s %(name)s.%(funcName)s] %(message)s"
fmt = logging.Formatter(fmt=msg_fmt)
# Log to a file
time_string = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = f"hydrus-tool.{time_string}.log"
h = logging.FileHandler(log_filename, "w", encoding="utf8")
h.setLevel(logging.DEBUG)
h.setFormatter(fmt)
log.addHandler(h)
# Log to stdout
h = logging.StreamHandler()
h.setLevel(logging.WARNING)
h.setFormatter(fmt)
log.addHandler(h)
# Your own code here:
setup_logging()
log.info("get file sources for file %s", file.file_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment