forked from KenyonY/openai-forward
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 changed file
with
34 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from loguru import logger | ||
from sparrow import relp | ||
import sys | ||
import logging | ||
|
||
|
||
class InterceptHandler(logging.Handler): | ||
def emit(self, record): | ||
# Get corresponding Loguru level if it exists | ||
try: | ||
level = logger.level(record.levelname).name | ||
except ValueError: | ||
level = record.levelno | ||
|
||
# Find caller from where originated the logged message | ||
frame, depth = logging.currentframe(), 2 | ||
while frame.f_code.co_filename == logging.__file__: | ||
frame = frame.f_back | ||
depth += 1 | ||
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage()) | ||
|
||
|
||
def setting_log(log_name, multi_process=True): | ||
logging.root.handlers = [InterceptHandler()] | ||
for name in logging.root.manager.loggerDict.keys(): | ||
logging.getLogger(name).handlers = [] | ||
logging.getLogger(name).propagate = True | ||
logger_config = { | ||
"handlers": [ | ||
{"sink": sys.stdout}, | ||
{"sink": relp(f"../Log/{log_name}"), "enqueue": multi_process, "rotation": "100 MB"}, | ||
], | ||
} | ||
logger.configure(**logger_config) |