Beautifully colored, quick and simple Python logging.
Project description
Python Quick Logging | QLogging
Beautifully colored, quick and simple Python logging. This logger is based on Python logging package
Screenshots:
Terminal/CMD
Notebooks:
Windows:
Features
- Color logging in Terminal and CMD
- Color logging in Jupyter Notebook and Jupyter Lab
- Color logging in Kaggle Notebook
- Color logging in Google Colab Notebook
- Know which function the logger was called from
- Know while line number the logger was called from
- Support logging to a file
- Simple and clean one-liner
- Customizable
Installation
$ pip install qlogging
Examples
Logging only to console/notebook:
import qlogging
logger = qlogging.get_logger(level='debug')
logger.debug("This is debug")
logger.info("This is info")
logger.warning("This is warning")
logger.error("This is an error")
logger.critical("This is a critical")
Output (output format: <time> <function_name>,<line#>| <log_message>
):
12:21:37 foo(),3| This is debug
12:21:37 foo(),4| This is info
12:21:37 foo(),5| This is warning
12:21:37 foo(),6| This is an error
12:21:37 foo(),7| This is a critical
Logging to console/terminal and a log file (append if log file exists):
import qlogging
logger = qlogging.get_logger(level='debug', logfile='my_log.log')
Logging to console/terminal and a log file (overwrite if log file exists):
import qlogging
logger = qlogging.get_logger(level='debug', logfile='my_log.log', logfilemode='w')
Logging with loggingmode='long'
(default is loggingmode='short'
):
import qlogging
logger = qlogging.get_logger(level='debug', loggingmode='long')
logger.debug("This is debug")
Output (output format: <date> <time> | <file_name> | <function_name>,<line#>| <log_message>
):
2021-05-18 12:38:22 | <main.py> | <foo()>,4 | This is debug
Easy Customization
Customize your logger based on the following get_logger()
function parameters
def get_logger(
level="info",
logfile=None,
logfilemode="a",
loggingmode="short",
format_str=None,
file_format_str=None,
format_date=None,
colors=None,
logger_config=None,
):
"""
returns Python logging based logger formatted with colors
:param level: (DEFAULT='info') str of logging level, each str option is mapped to Python logging levels, str options:
'info': logging.INFO,
'debug': logging.DEBUG,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL,
'notset': logging.NOTSET
:param logfile: (DEFAULT=None) str path where to save log file, example: '/tmp/my_log.log'
:param logfilemode: (DEFAULT='a') str of log file writing mode, same as the ones documented at Python logging package. options:
'a': appends to logfile
'w': overwrites logfile
:param loggingmode: (DEFAULT='short') str logging mode to be selected. options:
'short': will use short str format ('%(asctime)s %(funcName)s,%(lineno)s| %(message)s') and short date format ('%H:%M:%S')
'medium': will use long str format ('%(asctime)s | %(filename)s | %(funcName)s,%(lineno)s | %(message)s') and long date format ('%Y-%m-%d %H:%M:%S')
'manual': you need to set :param format_str: and :param format_date: yourself
:param format_str: (DEFAULT=None) str of format logging string for console, only set this if you selected :param loggingmode: as 'manual'. example (the style is always '%', see python logging module for more info):
'%(asctime)s | %(filename)s | %(funcName)s,%(lineno)s | %(message)s'
:param file_format_str: (DEFAULT=None) str of format logging string for logfile (if you keep it None, we will use what you passed in :param format_str:), only set this if you selected :param loggingmode: as 'manual'. example (the style is always '%', see python logging module for more info):
'%(asctime)s | %(filename)s | %(funcName)s,%(lineno)s | %(message)s'
:param date_str: (DEFAULT=None) str of date logging string, only set this if you selected :param loggingmode: as 'manual'. example:
'%Y-%m-%d %H:%M:%S'
:param colors: (DEFAULT=None) dict of color settings, only set this if you selected :param loggingmode: as 'manual'. example:
{
'DEBUG': Fore.CYAN + Style.BRIGHT,
'INFO': Fore.GREEN + Style.BRIGHT,
'WARNING': Fore.YELLOW + Style.BRIGHT,
'ERROR': Fore.RED + Style.BRIGHT,
'CRITICAL': Fore.RED + Back.WHITE + Style.BRIGHT,
}
:param logger_config: (DEFAULT=None) dict python logger config if you want to fully overwrite configs. example:
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"qlog": {
"()": "qlogging.qlogging.ColoredFormatter",
"colors": {
'DEBUG': Fore.CYAN + Style.BRIGHT,
'INFO': Fore.GREEN + Style.BRIGHT,
'WARNING': Fore.YELLOW + Style.BRIGHT,
'ERROR': Fore.RED + Style.BRIGHT,
'CRITICAL': Fore.RED + Back.WHITE + Style.BRIGHT,
},
"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
"datefmt":'%H:%M:%S'
},
},
"handlers": {
"console": {
"level": "DEBUG",
"formatter": "qlog",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"": {
"handlers": ["console"],
"level": "DEBUG",
"propagate": True,
},
},
}
:return: formated Python logging instance
"""
Alternatives
- coloredlogs 15.0: does not support coloring in notebooks.
- colorlog 5.0.1: does not support coloring in notebooks.
Credit:
License
MIT License Copyright (c) 2021 Github Account SinkingTitanic Owner
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file qlogging-1.3.1.tar.gz
.
File metadata
- Download URL: qlogging-1.3.1.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 351d0035e69863384497bdd62401af28a313c5b84a46f21b6eaceb8d5c52579c |
|
MD5 | 0fcdfc24c68090b4f634addf0d52a2e1 |
|
BLAKE2b-256 | a4a84a87a33304d86fa013f3ace7acd27731f5d7080a6c05aaa351813fba045d |