172 lines
3.6 KiB
Python
172 lines
3.6 KiB
Python
'''Simple logging module'''
|
|
|
|
import sys
|
|
|
|
from os import environ as env
|
|
from datetime import datetime
|
|
|
|
|
|
stdout = sys.stdout
|
|
|
|
|
|
class Log():
|
|
def __init__(self, config=dict()):
|
|
'''setup the logger'''
|
|
if not isinstance(config, dict):
|
|
raise TypeError(f'config is not a dict')
|
|
|
|
self.levels = {
|
|
'CRIT': 60,
|
|
'ERROR': 50,
|
|
'WARN': 40,
|
|
'INFO': 30,
|
|
'VERB': 20,
|
|
'DEBUG': 10,
|
|
'MERP': 0
|
|
}
|
|
|
|
self.config = dict()
|
|
self.setConfig(self._parseConfig(config))
|
|
|
|
|
|
def _lvlCheck(self, level):
|
|
'''make sure the minimum logging level is an int'''
|
|
try:
|
|
value = int(level)
|
|
|
|
except ValueError:
|
|
value = self.levels.get(level.upper())
|
|
|
|
if value not in self.levels.values():
|
|
raise error.InvalidLevel(f'Invalid logging level: {level}')
|
|
|
|
return value
|
|
|
|
|
|
def _parseConfig(self, config):
|
|
'''parse the new config and update the old values'''
|
|
date = config.get('date', self.config.get('date',True))
|
|
systemd = config.get('systemd', self.config.get('systemd,', True))
|
|
|
|
if not isinstance(date, bool):
|
|
raise TypeError(f'value for "date" is not a boolean: {date}')
|
|
|
|
if not isinstance(systemd, bool):
|
|
raise TypeError(f'value for "systemd" is not a boolean: {date}')
|
|
|
|
return {
|
|
'level': self._lvlCheck(config.get('level', self.config.get('level', 'INFO'))),
|
|
'datefmt': config.get('datefmt', self.config.get('datefmt', '%Y-%m-%d %H:%M:%S')),
|
|
'date': date,
|
|
'systemd': systemd
|
|
}
|
|
|
|
|
|
def setConfig(self, config):
|
|
'''set the config'''
|
|
self.config = self._parseConfig(config)
|
|
|
|
|
|
def getConfig(self):
|
|
'''return the current config'''
|
|
return self.config
|
|
|
|
|
|
def printConfig(self):
|
|
for k,v in self.config.items():
|
|
stdout.write(f'{k}: {v}\n')
|
|
|
|
stdout.flush()
|
|
|
|
|
|
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.config['level']:
|
|
return
|
|
|
|
message = ' '.join([str(message) for message in msg])
|
|
output = f'{level}: {message}\n'
|
|
|
|
if self.config['date'] and (self.config['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.config['datefmt'])
|
|
output = f'{date} {output}'
|
|
|
|
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, config=None):
|
|
'''get a logging instance and create one if it doesn't exist'''
|
|
Logger = logger.get(loginst)
|
|
|
|
if not Logger:
|
|
if config:
|
|
logger[loginst] = Log(config)
|
|
|
|
else:
|
|
raise error.InvalidLogger(f'logger "{loginst}" doesn\'t exist')
|
|
|
|
return logger[loginst]
|
|
|
|
class error:
|
|
'''base class for all errors'''
|
|
|
|
class InvalidLevel(Exception):
|
|
'''Raise when an invalid logging level was specified'''
|
|
|
|
class InvalidLogger(Exception):
|
|
'''Raise when the specified logger doesn't exist'''
|
|
|
|
|
|
'''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'''
|
|
setConfig = DefaultLog.setConfig
|
|
getConfig = DefaultLog.getConfig
|
|
printConfig = DefaultLog.printConfig
|