Created
July 2, 2024 04:21
-
-
Save xcollantes/011f770044e497b4372cf5cf92ffa409 to your computer and use it in GitHub Desktop.
Snippet for Python logging to file and terminal.
This file contains hidden or 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
| import logging as log | |
| logging = log.getLogger(__name__) | |
| logging.setLevel(log.INFO) | |
| formatter = log.Formatter( | |
| "%(asctime)s[%(levelname)s][%(filename)s][ln:%(lineno)s] %(message)s", | |
| datefmt="[%a %Y-%m-%d %H:%M:%S %Z]", | |
| ) | |
| file_handler = log.FileHandler("logs/app.log", "a") | |
| file_handler.setFormatter(formatter) | |
| terminal_handler = log.StreamHandler() | |
| terminal_handler.setFormatter(formatter) | |
| logging.addHandler(file_handler) | |
| logging.addHandler(terminal_handler) | |
| logging.info("Example") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment