Распилен кусок с логгированием, выкинута библиотека для подкрашивания логов
This commit is contained in:
parent
6849a5c801
commit
ebb36ac844
@ -1,3 +1,2 @@
|
|||||||
twitchAPI==2.5.7.1
|
twitchAPI==2.5.7.1
|
||||||
termcolor
|
|
||||||
pyTelegramBotAPI==4.8.0
|
pyTelegramBotAPI==4.8.0
|
51
src/log_msg.py
Normal file
51
src/log_msg.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import logging
|
||||||
|
from logging.handlers import TimedRotatingFileHandler
|
||||||
|
import sys
|
||||||
|
log_format = logging.Formatter('%(asctime)s %(levelname)s:%(message)s')
|
||||||
|
log_file = 'output.log'
|
||||||
|
|
||||||
|
class CustomFormatter(logging.Formatter):
|
||||||
|
|
||||||
|
grey = "\x1b[38;20m"
|
||||||
|
yellow = "\x1b[33;20m"
|
||||||
|
red = "\x1b[31;20m"
|
||||||
|
bold_red = "\x1b[31;1m"
|
||||||
|
reset = "\x1b[0m"
|
||||||
|
format = "%(asctime)s %(levelname)s - %(message)s"
|
||||||
|
|
||||||
|
FORMATS = {
|
||||||
|
logging.DEBUG: grey + format + reset,
|
||||||
|
logging.INFO: grey + format + reset,
|
||||||
|
logging.WARNING: yellow + format + reset,
|
||||||
|
logging.ERROR: red + format + reset,
|
||||||
|
logging.CRITICAL: bold_red + format + reset
|
||||||
|
}
|
||||||
|
|
||||||
|
def format(self, record):
|
||||||
|
log_fmt = self.FORMATS.get(record.levelno)
|
||||||
|
formatter = logging.Formatter(log_fmt)
|
||||||
|
return formatter.format(record)
|
||||||
|
|
||||||
|
class MyLog:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.logger = logging.getLogger("main")
|
||||||
|
self.logger.setLevel(logging.DEBUG)
|
||||||
|
# Console logging
|
||||||
|
self.console = self.get_console_handler()
|
||||||
|
self.console.setFormatter(CustomFormatter())
|
||||||
|
self.logger.addHandler(self.console)
|
||||||
|
|
||||||
|
self.logger.addHandler(self.get_file_handler())
|
||||||
|
self.logger.propagate = False
|
||||||
|
|
||||||
|
def get_console_handler(self):
|
||||||
|
console_handler = logging.StreamHandler(sys.stdout)
|
||||||
|
console_handler.setFormatter(log_format)
|
||||||
|
return console_handler
|
||||||
|
|
||||||
|
def get_file_handler(self):
|
||||||
|
file_handler = TimedRotatingFileHandler(log_file, when='midnight')
|
||||||
|
file_handler.setFormatter(log_format)
|
||||||
|
return file_handler
|
||||||
|
|
@ -2,11 +2,8 @@
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import logging
|
|
||||||
|
|
||||||
from twitchAPI.twitch import Twitch
|
from twitchAPI.twitch import Twitch
|
||||||
from logging.handlers import TimedRotatingFileHandler
|
|
||||||
from termcolor import colored
|
|
||||||
import telebot
|
import telebot
|
||||||
from telebot.types import InputFile
|
from telebot.types import InputFile
|
||||||
from telebot import types
|
from telebot import types
|
||||||
@ -15,9 +12,7 @@ import requests
|
|||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from parse_config import Config, load_config
|
from parse_config import Config, load_config
|
||||||
|
from log_msg import MyLog
|
||||||
log_format = logging.Formatter('%(asctime)s %(levelname)s:%(message)s')
|
|
||||||
log_file = 'output.log'
|
|
||||||
|
|
||||||
|
|
||||||
def stream_message_worker(data: dict):
|
def stream_message_worker(data: dict):
|
||||||
@ -118,12 +113,7 @@ def check_alive():
|
|||||||
# Получаем инфо о стримере, если не получается, выходим с ошибкой
|
# Получаем инфо о стримере, если не получается, выходим с ошибкой
|
||||||
resolved_id = twitch_client.get_users(logins=[config_python.streamer])
|
resolved_id = twitch_client.get_users(logins=[config_python.streamer])
|
||||||
if not resolved_id['data']:
|
if not resolved_id['data']:
|
||||||
log.error(
|
log.error("Аккаунт " + config_python.streamer + " не найден")
|
||||||
colored(
|
|
||||||
"Аккаунт " + config_python.streamer + " не найден",
|
|
||||||
'red',
|
|
||||||
)
|
|
||||||
)
|
|
||||||
# Достаем ID стримера из инфо
|
# Достаем ID стримера из инфо
|
||||||
user_id = resolved_id['data'][0]['id']
|
user_id = resolved_id['data'][0]['id']
|
||||||
# Получаем стримы
|
# Получаем стримы
|
||||||
@ -136,29 +126,8 @@ def check_alive():
|
|||||||
no_stream_msg_worker()
|
no_stream_msg_worker()
|
||||||
|
|
||||||
|
|
||||||
def get_console_handler():
|
|
||||||
console_handler = logging.StreamHandler(sys.stdout)
|
|
||||||
console_handler.setFormatter(log_format)
|
|
||||||
return console_handler
|
|
||||||
|
|
||||||
|
|
||||||
def get_file_handler():
|
|
||||||
file_handler = TimedRotatingFileHandler(log_file, when='midnight')
|
|
||||||
file_handler.setFormatter(log_format)
|
|
||||||
return file_handler
|
|
||||||
|
|
||||||
|
|
||||||
def get_logger(logger_name):
|
|
||||||
logger = logging.getLogger(logger_name)
|
|
||||||
logger.setLevel(logging.DEBUG)
|
|
||||||
logger.addHandler(get_console_handler())
|
|
||||||
logger.addHandler(get_file_handler())
|
|
||||||
logger.propagate = False
|
|
||||||
return logger
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
log = get_logger("main")
|
log = MyLog().logger
|
||||||
log.info("Запущен")
|
log.info("Запущен")
|
||||||
config_python = load_config()
|
config_python = load_config()
|
||||||
twitch_client = Twitch(config_python.app_id, config_python.app_secret)
|
twitch_client = Twitch(config_python.app_id, config_python.app_secret)
|
||||||
|
Loading…
Reference in New Issue
Block a user