feat: logging_time decorator

This commit is contained in:
superobk
2024-05-23 15:41:53 +08:00
parent d1be3b2aa7
commit d149521b11
5 changed files with 31 additions and 8 deletions

View File

@ -34,6 +34,11 @@ python main.py
## Configuration
Create ".env.yaml" at the root of jarvis-models, and copy the following yaml configuration
```yaml
env:
version: 0.0.1
host: 0.0.0.0
port: 8000
log:
level: debug
time_format: "%Y-%m-%d %H:%M:%S"

View File

@ -7,6 +7,8 @@ from fastapi.responses import JSONResponse
from injector import inject
from injector import singleton
from ..log.logging_time import logging_time
from ..configuration import MeloConf
from .blackbox import Blackbox
@ -14,7 +16,7 @@ import soundfile
from melo.api import TTS
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@singleton
class MeloTTS(Blackbox):
@ -50,6 +52,7 @@ class MeloTTS(Blackbox):
text = args[0]
return isinstance(text, str)
@logging_time(logger=logger)
def processing(self, *args, **kwargs) -> io.BytesIO | bytes:
text = args[0]
current_time = time.time()

View File

@ -81,7 +81,7 @@ class LogConf():
time_format = "%Y-%m-%d %H:%M:%S"
filename: str | None
@inject
def __init__(self,config: Configuration) -> None:
def __init__(self, config: Configuration) -> None:
self.level = config.get("log.level")
c = config.get("log.level", default=DEFAULT_LEVEL).upper()
level=logging._nameToLevel.get(c)
@ -95,7 +95,10 @@ class LogConf():
@singleton
class EnvConf():
version: str
host: str
port: str
@inject
def __init__(self) -> None:
def __init__(self, config: Configuration) -> None:
self.version = "0.0.1"
self.host = config.get("env.host", default="0.0.0.0")
self.port = config.get("env.port", default="8080")

15
src/log/logging_time.py Normal file
View File

@ -0,0 +1,15 @@
import time
import logging
def logging_time(logger: logging.Logger=None):
if logger is None:
logger = logging.getLogger("unknown_logger")
def dec(cb):
def inner(self, *args, **kwargs):
start = time.time()
result = cb(self, *args, **kwargs)
end = time.time()
logger.info(self.__class__.__name__+"."+cb.__name__ + " Time taken: " + str(end - start) + " seconds")
return result
return inner
return dec

View File

@ -10,10 +10,7 @@ from scipy.io.wavfile import read
import torch
MATPLOTLIB_FLAG = False
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging
logger = logging.getLogger(__name__)
def load_checkpoint(checkpoint_path, model, optimizer=None):
assert os.path.isfile(checkpoint_path)