Skip to content

Instantly share code, notes, and snippets.

@motebaya
Created August 8, 2024 05:47
Show Gist options
  • Save motebaya/2c47373072d45a4c3dffe8e37ecccc92 to your computer and use it in GitHub Desktop.
Save motebaya/2c47373072d45a4c3dffe8e37ecccc92 to your computer and use it in GitHub Desktop.
a customization python logger for console log using logging module
#!/usr/bin/python3
# @github.com/motebaya - 2023.06.3 08:42:53 AM
# file: __logger__
import logging
from colorama.ansi import Fore
from typing import Union, Type
logging.addLevelName(logging.WARNING, f"{Fore.YELLOW}warning{Fore.RESET}")
logging.addLevelName(logging.DEBUG, f"{Fore.GREEN}debug{Fore.RESET}")
logging.addLevelName(logging.INFO, f"{Fore.CYAN}info{Fore.RESET}")
class Logger:
levels = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING
}
@staticmethod
def getClassName(classname: Union[Type, object])-> str:
if isinstance(classname, type):
return classname.__name__
elif isinstance(classname, object):
return classname.__class__.__name__
@staticmethod
def getLogger(level: str | bool = None) -> logging.RootLogger:
logging.basicConfig(**{
"format": f" {Fore.CYAN}%(asctime)s{Fore.RESET} %(levelname)s{Fore.CYAN}::{Fore.RESET}%(message)s ",
"level": Logger.levels.get(level) or "info",
"datefmt": '%H:%M:%S'
})
return logging.getLogger(__name__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment