├── LICENSE ├── README.md ├── logging_formatting.py ├── logging_papertrail.py ├── logging_to_console.py └── logging_to_file.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 ArjanCodes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Logging: How to Write Logs Like a Pro! 2 | 3 | Don't make the mistake of underestimating the importance of logging! Logging can make all the difference in commercial software products, and it's essential to have a proper understanding of how to use it effectively. In this video, I dive into the details of Python logging, along with tips on how to make your logs easier to manage. 4 | 5 | Video: https://youtu.be/pxuXaaT1u3k. 6 | -------------------------------------------------------------------------------- /logging_formatting.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | 4 | def main() -> None: 5 | logging.basicConfig( 6 | level=logging.DEBUG, 7 | format="%(asctime)s %(levelname)s %(message)s", 8 | datefmt="%Y-%m-%d %H:%M:%S", 9 | ) 10 | 11 | logging.debug("This is a debug message.") 12 | logging.info("This is an info message.") 13 | logging.warning("This is a warning message.") 14 | logging.error("This is an error message.") 15 | logging.critical("This is a critical message.") 16 | 17 | 18 | if __name__ == "__main__": 19 | main() 20 | -------------------------------------------------------------------------------- /logging_papertrail.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from logging.handlers import SysLogHandler 3 | 4 | PAPERTRAIL_HOST = "logs.papertrailapp.com" 5 | PAPERTRAIL_PORT = 00000 # replace with your port 6 | 7 | 8 | def main() -> None: 9 | logger = logging.getLogger("arjancodes") 10 | logger.setLevel(logging.DEBUG) 11 | handler = SysLogHandler(address=(PAPERTRAIL_HOST, PAPERTRAIL_PORT)) 12 | logger.addHandler(handler) 13 | 14 | logger.debug("This is a debug message.") 15 | 16 | 17 | if __name__ == "__main__": 18 | main() 19 | -------------------------------------------------------------------------------- /logging_to_console.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | 4 | def main() -> None: 5 | logging.basicConfig(level=logging.WARNING) 6 | 7 | logging.debug("This is a debug message.") 8 | logging.info("This is an info message.") 9 | logging.warning("This is a warning message.") 10 | logging.error("This is an error message.") 11 | logging.critical("This is a critical message.") 12 | 13 | 14 | if __name__ == "__main__": 15 | main() 16 | -------------------------------------------------------------------------------- /logging_to_file.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | 4 | def main() -> None: 5 | logging.basicConfig( 6 | level=logging.DEBUG, 7 | format="%(asctime)s %(levelname)s %(message)s", 8 | datefmt="%Y-%m-%d %H:%M:%S", 9 | filename="basic.log", 10 | ) 11 | 12 | logging.debug("This is a debug message.") 13 | logging.info("This is an info message.") 14 | logging.warning("This is a warning message.") 15 | logging.error("This is an error message.") 16 | logging.critical("This is a critical message.") 17 | 18 | 19 | if __name__ == "__main__": 20 | main() 21 | --------------------------------------------------------------------------------