izzylib/IzzyLib/logging.py

151 lines
3.2 KiB
Python

'''
Simple logging module
I only created this because working with Sanic's logging is a fuck tbh
'''
import sys
from os import environ as env
from datetime import datetime
class Log():
'''basic logging class'''
def __init__(self, minimum='INFO', datefmt='%Y-%m-%d %H:%M:%S', date=True, systemd=True):
self.levels = {
'CRIT': 60,
'ERROR': 50,
'WARN': 40,
'INFO': 30,
'VERB': 20,
'DEBUG': 10,
'MERP': 0
}
self.systemd = True
self.date = date
self.datefmt = datefmt
self.minimum = self._lvlCheck(minimum)
def _lvlCheck(self, level):
'''make sure the minimum logging level is an int'''
try:
value = int(level)
except ValueError:
value = self.levels.get(level)
if value not in self.levels.values():
raise error.InvalidLevel(f'Invalid logging level: {level}')
return value
def setLevel(self, level):
'''change the minimum logging level'''
self.minimum = self._lvlCheck(level)
def setDateEnable(self, boolean):
'''enable or disable printing the date'''
if type(boolean) != bool:
raise TypeError(f'{boolean} is not a boolean')
self.date = boolean
def setDate(self, datefmt):
'''change the date format'''
'''insert regex to make sure it is the correct format here'''
self.datefmt = datefmt
def setSystemd(self, boolean):
'''enable or disable checking if the script is running in systemd'''
if type(boolean) != bool:
raise TypeError(f'{boolean} is not a boolean')
self.systemd = boolean
def log(self, level, msg):
'''log to the console'''
levelNum = self._lvlCheck(level)
if type(level) == int:
for k,v in self.levels.items():
if v == levelNum:
level = k
if levelNum < self.minimum:
return
output = f'{level}: {msg}\n'
if self.date and (self.systemd and not env.get('INVOCATION_ID')):
'''only show date when not running in systemd and date var is True'''
date = datetime.now().strftime(self.datefmt)
output = f'{date} {output}'
stdout = sys.stdout
stdout.write(output)
stdout.flush()
def critical(self, msg):
self.log('CRIT', msg)
def error(self, msg):
self.log('ERROR', msg)
def warning(self, msg):
self.log('WARN', msg)
def info(self, msg):
self.log('INFO', msg)
def verbose(self, msg):
self.log('VERB', msg)
def debug(self, msg):
self.log('DEBUG', msg)
def merp(self, msg):
self.log('MERP', msg)
def getLogger(loginst):
'''get a logging instance'''
Logger = logger.get(loginst)
if not Logger:
raise
'''create a default logger'''
logger = {
'default': Log()
}
DefaultLog = logger['default']
'''aliases for default logger's log output functions'''
critical = DefaultLog.critical
error = DefaultLog.error
warning = DefaultLog.warning
info = DefaultLog.info
verbose = DefaultLog.verbose
debug = DefaultLog.debug
merp = DefaultLog.merp
'''aliases for the default logger's config functions'''
'''will probably change this to setConfig to simplify things'''
setLevel = DefaultLog.setLevel
setDateEnable = DefaultLog.setDateEnable
setDate = DefaultLog.setDate
setSystemd = DefaultLog.setSystemd
class error:
class InvalidLevel(Exception):
'''Raise when an invalid logging level was specified'''
class InvalidLogger(Exception):
'''Raise when the specified logger doesn't exist'''