41 lines
990 B
Python
41 lines
990 B
Python
from enum import Enum
|
|
import os
|
|
import datetime
|
|
|
|
|
|
class LoggerStage(Enum):
|
|
MESSAGE = 1
|
|
WARNING = 2
|
|
ERROR = 3
|
|
|
|
|
|
class Logger:
|
|
|
|
path = None
|
|
|
|
@classmethod
|
|
def set_log_file(cls, path):
|
|
cls.path = path
|
|
|
|
@classmethod
|
|
def _write(cls, entry, msg, kind):
|
|
if not cls.path:
|
|
return
|
|
if not os.path.exists(path=cls.path):
|
|
os.makedirs(os.path.dirname(cls.path), exist_ok=True)
|
|
with open(cls.path, 'a') as file:
|
|
message = datetime.datetime.now().strftime('%Y.%m.%d %H:%M:%S') + ' ' + entry + ': ' + str(kind) + ' ' + msg + '\n'
|
|
file.write(message)
|
|
|
|
@classmethod
|
|
def write_message(cls, entry, message):
|
|
cls._write(entry, message, LoggerStage.MESSAGE)
|
|
|
|
@classmethod
|
|
def write_warning(cls, entry, warning):
|
|
cls._write(entry, warning, LoggerStage.WARNING)
|
|
|
|
@classmethod
|
|
def write_error(cls, entry, error):
|
|
cls._write(entry, error, LoggerStage.ERROR)
|