Created
November 18, 2019 20:08
-
-
Save juftin/3a5255b9423a05b5e3aec47df3a043fc to your computer and use it in GitHub Desktop.
Easy Logging Setup
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
#!/usr/bin/env python | |
import logging | |
from os import environ | |
def logger(level="info", timestamp=True, filename=None, | |
custom_format=None, inherit_env=True): | |
""" | |
Default Logging Method. This takes multiple | |
parameters/environmental variable to define logging output. | |
This function accepts the following environmental variables: | |
LOG_LEVEL, LOG_TIMESTAMP, LOG_FILENAME, LOG_FORMAT | |
Example: | |
log = logger(level="debug", timestamp=True) | |
log.debug("This is a debug message") | |
log.info("This is an info message.") | |
log.warning("This is a warning message.") | |
log.error("This is an error message.") | |
log.critical("This is a critical message.") | |
> 2019-11-16 15:37:56,805 [ DEBUG]: This is a debug message | |
> 2019-11-16 15:37:56,805 [ INFO]: This is an info message. | |
> 2019-11-16 15:37:56,805 [ WARNING]: This is a warning message. | |
> 2019-11-16 15:37:56,805 [ ERROR]: This is an error message. | |
> 2019-11-16 15:37:56,805 [CRITICAL]: This is a critical message. | |
:param str level: the level of depth you wish to log | |
:param bool timestamp: whether or not to include timestamp in message | |
:param str filename: enables export to file, provide file name/path | |
:param str custom_format: log formatting string, | |
defaults to "%(asctime)s [%(levelname)8s]: %(message)s" | |
:param bool inherit_env: environmental vairables will overwrite other parameters | |
:return: the logging object | |
""" | |
if inherit_env: | |
log_level = environ.get(key="LOG_LEVEL", default=level) | |
log_timestamp = str(environ.get( | |
key="LOG_TIMESTAMP", | |
default=timestamp)).lower() in ["true", "t", "yes", "y", "1"] | |
log_filename = environ.get(key="LOG_FILENAME", default=filename) | |
custom_format = environ.get(key="LOG_FORMAT", default=custom_format) | |
else: | |
log_level = level | |
log_timestamp = timestamp | |
log_filename = filename | |
custom_format = custom_format | |
if not custom_format: | |
format = "%(asctime)s [%(levelname)8s]: %(message)s" | |
else: | |
format = custom_format | |
if not log_timestamp and not custom_format: | |
format = format.replace("%(asctime)s ", "") | |
handlers = [logging.StreamHandler()] | |
if log_filename: | |
handlers.append(logging.FileHandler(log_filename)) | |
logger = logging | |
logger.basicConfig( | |
level=logger.getLevelName(log_level.upper()), | |
format=format, | |
handlers=handlers) | |
return logger | |
log = logger(level="debug", timestamp=True, filename="logs.log", | |
custom_format=None, inherit_env=True) | |
log.debug("This is a debug message") | |
log.info("This is an info message.") | |
log.warning("This is a warning message.") | |
log.error("This is an error message.") | |
log.critical("This is a critical message.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment