Created
December 2, 2020 12:34
-
-
Save Ja7ad/5df7efa532cc501c04a72cfcbab93c7e to your computer and use it in GitHub Desktop.
Singleton Logger in python ( info, warning, error, debug )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
class MetaSingleton(type): | |
_instance = {} | |
def __call__(cls, *args, **kwargs): | |
if cls not in cls._instance: | |
cls._instance[cls] = super().__call__(*args, **kwargs) | |
return cls._instance[cls] | |
class Logger(metaclass=MetaSingleton): | |
def _write_file(self, type_log, msg: str): | |
if not os.path.exists('log'): | |
os.mkdir('log') | |
log_write = f'[{type_log}] [{str(datetime.datetime.now())}]: {msg}\n' | |
with open(f'log/{datetime.date.today()}.log', 'a') as file_log: | |
file_log.write(log_write) | |
def info(self, msg): | |
self._write_file('Info', msg) | |
def error(self, msg): | |
self._write_file('Error', msg) | |
def warning(self, msg): | |
self._write_file('Warning', msg) | |
def debug(self, msg): | |
self._write_file('Debug', msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment