-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_logging.py
82 lines (69 loc) · 2.39 KB
/
config_logging.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
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File : config_logging.py
Author : GraphLinq Chain
Email : info@graphlinq.io
Website : https://graphlinq.io
Repository : https://github.com/jrbgit/GraphLinq.TelegramBot
Date : 2023-06-20
Version : 1.2
Description : Logging Config - Telegram bot for GraphLinq
"""
import logging
loggers = {}
telegram_bot_logger = logging.getLogger('telegram')
file_handler = logging.FileHandler('logs/telegram_bot_debug.log')
formatter = logging.Formatter('[%(levelname)s] [%(asctime)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
file_handler.setFormatter(formatter)
telegram_bot_logger.addHandler(file_handler)
log_formats = {
logging.DEBUG: {
'filename': 'logs/debug.log',
'encoding': 'utf-8',
'format': '[DEBUG] [%(asctime)s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
logging.INFO: {
'filename': 'logs/info.log',
'encoding': 'utf-8',
'format': '[INFO] [%(asctime)s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
logging.WARNING: {
'filename': 'logs/warning.log',
'encoding': 'utf-8',
'format': '[WARNING] [%(asctime)s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
logging.ERROR: {
'filename': 'logs/error.log',
'encoding': 'utf-8',
'format': '[ERROR] [%(asctime)s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
logging.CRITICAL: {
'filename': 'logs/critical.log',
'encoding': 'utf-8',
'format': '[CRITICAL] [%(asctime)s] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
}
}
for level, settings in log_formats.items():
logger = logging.getLogger(str(level))
logger.setLevel(level)
file_handler = logging.FileHandler(filename=settings['filename'], encoding=settings['encoding'])
formatter = logging.Formatter(settings['format'], datefmt=settings['datefmt'])
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
loggers[level] = logger
def log_debug(message):
loggers[logging.DEBUG].debug(message)
def log_info(message):
loggers[logging.INFO].info(message)
def log_warning(message):
loggers[logging.WARNING].warning(message)
def log_error(message):
loggers[logging.ERROR].error(message)
def log_critical(message):
loggers[logging.CRITICAL].critical(message)