Skip to content

Instantly share code, notes, and snippets.

@nkthiebaut
Created June 26, 2020 17:25
Show Gist options
  • Save nkthiebaut/f6988d4d0621114be9f5ad9c845e0b97 to your computer and use it in GitHub Desktop.
Save nkthiebaut/f6988d4d0621114be9f5ad9c845e0b97 to your computer and use it in GitHub Desktop.
# Official doc: https://docs.python.org/3/howto/logging-cookbook.html
import os
import logging
from logging import StreamHandler
from logging import Formatter
LOGS_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../logs')
if not os.path.exists(LOGS_DIR):
os.mkdir(LOGS_DIR)
LOG_FORMAT = (
"%(asctime)s [%(levelname)s]: %(message)s in %(pathname)s:%(lineno)d")
STREAM_HANDLER = StreamHandler()
STREAM_HANDLER.setFormatter(Formatter(LOG_FORMAT))
FILE_HANDLER = logging.FileHandler(os.path.join(LOGS_DIR, 'my_package.log'))
FILE_HANDLER.setFormatter(Formatter(LOG_FORMAT))
package_logger = logging.getLogger('my_package')
package_logger.addHandler(STREAM_HANDLER)
package_logger.addHandler(FILE_HANDLER)
package_logger.setLevel(logging.DEBUG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment