Skip to content

Instantly share code, notes, and snippets.

@ariel-co
Created April 8, 2025 22:40
Show Gist options
  • Select an option

  • Save ariel-co/129977b423ac5bdd73df51a219475c29 to your computer and use it in GitHub Desktop.

Select an option

Save ariel-co/129977b423ac5bdd73df51a219475c29 to your computer and use it in GitHub Desktop.
logging to both stdout and file
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