Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging tips #23

Draft
wants to merge 7 commits into
base: feature/git-tips
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Current content:

- [Git tips](git_tips)
- [Logging tips](logging_tips)
7 changes: 7 additions & 0 deletions sa/logging_tips/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Logging in Python 👌

During coding, making sure that you have a logging system in place is very important. It will allow you to debug errors more easily and will enable you to better analyse the performance 🕵️‍♀️ of your application.

---

In the notebook included in this folder you will not only find general information about the logging module, but also multiple tips and tricks that will level up your logging game! Make sure to run every codeblock only once, as reruns might break some things with the logging components.
Binary file added sa/logging_tips/img/logging_components.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions sa/logging_tips/logging.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
Empty file.
33 changes: 33 additions & 0 deletions sa/logging_tips/logging_template/helpers/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
This file contains the logging setup.
"""

import logging
import sys

APP_LOGGER_NAME = 'MyLoggerApp'

def setup_applevel_logger(logger_name=APP_LOGGER_NAME, file_name=None):
"""..."""
logger = logging.getLogger(logger_name)
syslog = logging.StreamHandler()

format = '%(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(format)
syslog.setFormatter(formatter)

logger.setLevel(logging.DEBUG)
logger.handlers.clear()
logger.addHandler(sh)

if file_name:
fh = logging.FileHandler(file_name)
fh.setFormatter(formatter)
logger.addHandler(fh)

return logger


def get_logger(module_name):
"""..."""
return logging.getLogger(APP_LOGGER_NAME).getChild(module_name)
13 changes: 13 additions & 0 deletions sa/logging_tips/logging_template/helpers/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Custom module
"""

import logger

log = logger.get_logger(__name__)


def multiply(num1, num2): # multiply two numbers
"""..."""
log.debug('Executing multiply.')
return num1 * num2
19 changes: 19 additions & 0 deletions sa/logging_tips/logging_template/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
File containing the entrypoint of the script.
"""

from helpers import logger
log = logger.setup_applevel_logger(file_name = 'app_debug.log')

from helpers import module


def run():
"""..."""
log.debug('Calling module function.')
module.multiply(5, 2)
log.info('Finished.')


if __name__ == '__main__':
run()
Loading