-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconsole_logger.py
More file actions
62 lines (50 loc) · 2.06 KB
/
console_logger.py
File metadata and controls
62 lines (50 loc) · 2.06 KB
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
"""
Copyright © Krypton 2019-2023 - https://github.com/kkrypt0nn (https://krypton.ninja)
Description:
🐍 A simple template to start to code your own and personalized discord bot in Python
programming language.
Version: 6.1.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an " AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
"""
import logging
from src.bot import Config
class ConsoleLoggingFormatter(logging.Formatter):
# Colours
black = "\x1b[30m"
red = "\x1b[31m"
green = "\x1b[32m"
yellow = "\x1b[33m"
blue = "\x1b[34m"
gray = "\x1b[38m"
# Styles
reset = "\x1b[0m"
bold = "\x1b[1m"
COLOURS = {
logging.DEBUG: gray + bold,
logging.INFO: blue + bold,
logging.WARNING: yellow + bold,
logging.ERROR: red,
logging.CRITICAL: red + bold,
}
def format(self, record) -> str:
log_colour = self.COLOURS[record.levelno]
format_str = (
"(black){asctime}(reset) (levelcolour){levelname:<8}(reset) "
"(green){name}(reset) {message}"
)
format_str = format_str.replace("(black)", self.black + self.bold)
format_str = format_str.replace("(reset)", self.reset)
format_str = format_str.replace("(levelcolour)", log_colour)
format_str = format_str.replace("(green)", self.green + self.bold)
formatter = logging.Formatter(format_str, "%Y-%m-%d %H:%M:%S", style="{")
return formatter.format(record)
def add_console_handler(config: Config) -> None:
console_handler = logging.StreamHandler()
console_handler.setFormatter(ConsoleLoggingFormatter())
logging.getLogger().addHandler(console_handler)