Skip to content

Instantly share code, notes, and snippets.

@nclv
Created January 23, 2020 21:05
Show Gist options
  • Save nclv/626256825a7c2a9346e4710cc07615bb to your computer and use it in GitHub Desktop.
Save nclv/626256825a7c2a9346e4710cc07615bb to your computer and use it in GitHub Desktop.
import logging
def setup_custom_logger(name, here):
"""Création d'un logger.
On veut logger dans le terminal (ERROR) et dans un fichier de log (DEBUG).
Debug (logger.debug): Provide very detailed output. Used for diagnosing
problems.
Info (logger.info): Provides information on successful execution. Confirms if
things are working as expected.
Warning (logger.warn or logger.warning): Issue a warning regarding a problem
that might occur in the future or a recoverable fault.
Error (logger.error): Indicates a problem in the software as it is not
executing as expected.
Critical (logger.critical): Indicates a serious error that might stop the
program from running.
"""
# create file handler which logs even debug messages
filehandler = logging.FileHandler(
filename=f"{here}/game.log", mode="w", encoding="utf-8"
)
filehandler.setLevel(logging.DEBUG)
# create console handler with a higher log level
consolehandler = logging.StreamHandler()
consolehandler.setLevel(logging.WARNING)
# create formatter and add it to the handlers
logformat = "%(asctime)s - %(name)-40s %(levelname)-8s %(message)s"
fileformatter = logging.Formatter(fmt=logformat, datefmt="%d-%b-%y %H:%M:%S")
consoleformatter = logging.Formatter(fmt="%(levelname)-8s %(message)s")
filehandler.setFormatter(fileformatter)
consolehandler.setFormatter(consoleformatter)
# create logger
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# add the handlers to the logger
logger.addHandler(filehandler)
logger.addHandler(consolehandler)
return logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment