ShipmentNotifier/ShipmentNotifierLogger.py

34 lines
926 B
Python

import logging
import os
from datetime import datetime
def log(log_message, level):
logger = logging.getLogger('sn-logger')
log_message_types = {
'debug': logger.debug,
'info': logger.info,
'warning': logger.warning,
'error': logger.error,
'critical': logger.critical
}
if not logger.handlers:
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(levelname)s] %(message)s')
file_handler = logging.FileHandler('logs/' + 'log_' + datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + '.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
log_message_types[level](log_message)
def cleanLogs():
for filename in sorted(os.listdir('logs/'))[:-5]:
filenameWithPath = os.path.join('logs/', filename)
os.remove(filenameWithPath)