This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogger.py
70 lines (47 loc) · 1.61 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations
import os
import sys
from helpers.time import formatted_date
class Ansi:
BLACK = 40
RED = 41
GREEN = 42
YELLOW = 43
BLUE = 44
MAGENTA = 45
CYAN = 46
WHITE = 47
# Received permission directly from him to use it. Just figured it looks cool.
# Made some optimisations to it.
__name__ = "LoggerModule"
__author__ = "lenforiee"
DEBUG = "debug" in sys.argv
# Windows support
# activated only when os name is nt (windows)
if os.name == "nt":
from ctypes import windll
windll.kernel32.SetConsoleMode(windll.kernel32.GetStdHandle(-11), 7)
def log_message(content: str, l_type: str, bg_col: str):
"""Creates the final string and writes it to console.
Args:
content (str): The main text to be logged to console.
l_type (str): The type of the log that will be displayed to the user.
bl_col (str): The background colour for the `l_type`.
"""
# Print to console. Use this as faster ig.
sys.stdout.write(
f"\033[37m{bg_col}[{l_type}]\033[49m - "
f"[{formatted_date()}] {content}\033[39m\n",
)
def custom_log(message: str, header: str, colour: Ansi):
"""Prints custom log with custom header and colour"""
return log_message(message, header, f"\033[{colour}m")
def debug(message: str):
if DEBUG:
return log_message(message, "DEBUG", "\033[43m")
def info(message: str):
return log_message(message, "INFO", "\033[42m")
def error(message: str):
return log_message(message, "ERROR", "\033[41m")
def warning(message: str):
return log_message(message, "WARNING", "\033[44m")