Skip to content

Instantly share code, notes, and snippets.

@JV-conseil
Last active October 16, 2023 17:23
Show Gist options
  • Save JV-conseil/a3530f669bde8b6c53e0405c77b58184 to your computer and use it in GitHub Desktop.
Save JV-conseil/a3530f669bde8b6c53e0405c77b58184 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
# ====================================================
import logging
def configure_logger_file(name: str = "default") -> object:
"""Freely adapted from
https://simpletutorials.com/c/python/zf02eyin/python-3-logging-using-dictconfig
"""
output = None
try:
logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"default": {
"format": "{asctime}\t{levelname}\t{message}\t{value}\t{data}",
"style": "{",
},
},
"handlers": {
"file": {
"class": "logging.handlers.RotatingFileHandler",
"filename": f"./logs/{name}.log",
"formatter": "default",
"level": "DEBUG",
"maxBytes": 1024,
"backupCount": 3,
},
},
"loggers": {
name: {
"handlers": ["file"],
"propagate": False,
}
},
}
)
output = logging.getLogger(name)
except Exception as e:
print(e)
return output
# Create a custom logger
logger_file = configure_logger_file("default")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment