Add stderr logging and log path

This commit is contained in:
Paul Nameless 2020-06-03 09:36:48 +08:00
parent ca81118f48
commit 3ad084f3db
2 changed files with 33 additions and 12 deletions

View file

@ -14,6 +14,7 @@ _linux = "Linux"
DEFAULT_CONFIG = os.path.expanduser("~/.config/tg/conf.py")
DEFAULT_FILES = os.path.expanduser("~/.cache/tg/")
LOG_LEVEL = "INFO"
LOG_PATH = os.path.expanduser("~/.local/share/tg/")
API_ID = "559815"
API_HASH = "fd121358f59d764c57c55871aa0807ca"

View file

@ -10,6 +10,7 @@ import re
import shlex
import struct
import subprocess
import sys
from datetime import datetime
from functools import wraps
from typing import Optional
@ -32,6 +33,37 @@ emoji_pattern = re.compile(
units = {"B": 1, "KB": 10 ** 3, "MB": 10 ** 6, "GB": 10 ** 9, "TB": 10 ** 12}
class LogWriter:
def __init__(self, level):
self.level = level
def write(self, message):
if message != "\n":
self.level.log(self.level, message)
def flush(self):
pass
def setup_log(level="DEBUG"):
handlers = []
for level, filename in zip(
(config.LOG_LEVEL, logging.ERROR), ("all.log", "error.log"),
):
handler = logging.FileHandler(filename)
handler.setLevel(level)
handlers.append(handler)
logging.basicConfig(
format="%(levelname)-8s [%(asctime)s] %(name)s %(message).1000s",
handlers=handlers,
)
logging.getLogger().setLevel(config.LOG_LEVEL)
sys.stderr = LogWriter(log.error)
logging.captureWarnings(True)
def get_file_handler(file_path, default=None):
mtype, _ = mimetypes.guess_type(file_path)
if not mtype:
@ -107,18 +139,6 @@ def get_waveform(file_path):
return base64.b64encode(packed).decode()
def setup_log(level="DEBUG"):
logging.basicConfig(
level=level,
format="%(asctime)s %(levelname)s %(message)s",
handlers=[
logging.handlers.RotatingFileHandler(
"./tg.log", backupCount=1, maxBytes=1024 * 1024 * 10 # 10 MB
),
],
)
def notify(
msg, subtitle="", title="tg", cmd=config.NOTIFY_CMD,
):