-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
34 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,22 @@ | ||
# -*- encoding: utf-8 -*- | ||
# @Author: SWHL | ||
# @Contact: [email protected] | ||
import functools | ||
import sys | ||
from pathlib import Path | ||
import logging | ||
|
||
from loguru import logger | ||
|
||
def get_logger(): | ||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
|
||
@functools.lru_cache() | ||
def get_logger(save_dir: str = "."): | ||
loguru_format = ( | ||
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | " | ||
"<level>{level: <8}</level> | " | ||
"<cyan>{name}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>" | ||
) | ||
fmt = "%(asctime)s - %(levelname)s: %(message)s" | ||
format_str = logging.Formatter(fmt) | ||
|
||
logger.remove() | ||
logger.add( | ||
sys.stderr, | ||
format=loguru_format, | ||
level="INFO", | ||
enqueue=True, | ||
) | ||
|
||
file_name = "{time:YYYY-MM-DD-HH-mm-ss}.log" | ||
save_file = Path(save_dir) / file_name | ||
logger.add(save_file, rotation=None, retention="2 days") | ||
# 注意这里,想要写到不同log文件中,这里会存在写到同一个log文件中问题 | ||
if not logger.handlers: | ||
sh = logging.StreamHandler() | ||
logger.addHandler(sh) | ||
sh.setFormatter(format_str) | ||
return logger | ||
|
||
|
||
log_dir = Path(__file__).resolve().parent / "log" | ||
logger = get_logger(str(log_dir)) | ||
logger = get_logger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters