mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-20 00:10:03 +02:00
moves log_config out of setup_logging method to make it configurable, removes argument from function
This commit is contained in:
parent
60c671eb0d
commit
e31af9ae31
@ -5,13 +5,41 @@ import sys
|
||||
from copy import copy
|
||||
|
||||
import socketio # type: ignore[import-untyped]
|
||||
import uvicorn.config
|
||||
import uvicorn.logging
|
||||
from uvicorn.config import LOGGING_CONFIG
|
||||
|
||||
import pydase.config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
if pydase.config.OperationMode().environment == "development":
|
||||
LOG_LEVEL = logging.DEBUG
|
||||
else:
|
||||
LOG_LEVEL = logging.INFO
|
||||
|
||||
LOGGING_CONFIG = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"default": {
|
||||
"()": "pydase.utils.logging.DefaultFormatter",
|
||||
"fmt": "%(asctime)s.%(msecs)03d | %(levelprefix)s | "
|
||||
"%(name)s:%(funcName)s:%(lineno)d - %(message)s",
|
||||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||
},
|
||||
},
|
||||
"handlers": {
|
||||
"default": {
|
||||
"formatter": "default",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stderr",
|
||||
},
|
||||
},
|
||||
"loggers": {
|
||||
"pydase": {"handlers": ["default"], "level": LOG_LEVEL, "propagate": False},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class DefaultFormatter(uvicorn.logging.ColourizedFormatter):
|
||||
"""
|
||||
@ -67,87 +95,31 @@ class SocketIOHandler(logging.Handler):
|
||||
)
|
||||
|
||||
|
||||
def setup_logging(level: str | int | None = None) -> None:
|
||||
def setup_logging() -> None:
|
||||
"""
|
||||
Configures the logging settings for the application.
|
||||
|
||||
This function sets up logging with specific formatting and colorization of log
|
||||
messages. The log level is determined based on the application's operation mode,
|
||||
with an option to override the level. By default, in a development environment, the
|
||||
log level is set to DEBUG, whereas in other environments, it is set to INFO.
|
||||
|
||||
Args:
|
||||
level (Optional[str | int]):
|
||||
A specific log level to set for the application. If None, the log level is
|
||||
determined based on the application's operation mode. Accepts standard log
|
||||
level names ('DEBUG', 'INFO', etc.) and corresponding numerical values.
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> import logging
|
||||
>>> setup_logging(logging.DEBUG)
|
||||
>>> setup_logging("INFO")
|
||||
```
|
||||
messages. The log level is determined based on the application's operation mode. By
|
||||
default, in a development environment, the log level is set to DEBUG, whereas in
|
||||
other environments, it is set to INFO.
|
||||
"""
|
||||
|
||||
logger.debug("Configuring pydase logging.")
|
||||
|
||||
if pydase.config.OperationMode().environment == "development":
|
||||
log_level = logging.DEBUG
|
||||
else:
|
||||
log_level = logging.INFO
|
||||
|
||||
# If a level is specified, check whether it's a string or an integer.
|
||||
if level is not None:
|
||||
if isinstance(level, str):
|
||||
# Convert known log level strings directly to their corresponding logging
|
||||
# module constants.
|
||||
level_name = level.upper() # Ensure level names are uppercase
|
||||
if hasattr(logging, level_name):
|
||||
log_level = getattr(logging, level_name)
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Invalid log level: {level}. Must be one of 'DEBUG', 'INFO', "
|
||||
"'WARNING', 'ERROR', etc."
|
||||
)
|
||||
elif isinstance(level, int):
|
||||
log_level = level # Directly use integer levels
|
||||
else:
|
||||
raise ValueError("Log level must be a string or an integer.")
|
||||
|
||||
log_config = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"default": {
|
||||
"()": "pydase.utils.logging.DefaultFormatter",
|
||||
"fmt": "%(asctime)s.%(msecs)03d | %(levelprefix)s | "
|
||||
"%(name)s:%(funcName)s:%(lineno)d - %(message)s",
|
||||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||
},
|
||||
},
|
||||
"handlers": {
|
||||
"default": {
|
||||
"formatter": "default",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stderr",
|
||||
},
|
||||
},
|
||||
"loggers": {
|
||||
"pydase": {"handlers": ["default"], "level": log_level, "propagate": False},
|
||||
},
|
||||
}
|
||||
|
||||
logging.config.dictConfig(log_config)
|
||||
logging.config.dictConfig(LOGGING_CONFIG)
|
||||
|
||||
# configuring uvicorn logger
|
||||
LOGGING_CONFIG["formatters"]["default"]["fmt"] = (
|
||||
uvicorn.config.LOGGING_CONFIG["formatters"]["default"]["fmt"] = (
|
||||
"%(asctime)s.%(msecs)03d | %(levelprefix)s %(message)s"
|
||||
)
|
||||
LOGGING_CONFIG["formatters"]["default"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
|
||||
LOGGING_CONFIG["formatters"]["access"]["fmt"] = (
|
||||
uvicorn.config.LOGGING_CONFIG["formatters"]["default"]["datefmt"] = (
|
||||
"%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
uvicorn.config.LOGGING_CONFIG["formatters"]["access"]["fmt"] = (
|
||||
"%(asctime)s.%(msecs)03d | %(levelprefix)s %(client_addr)s "
|
||||
'- "%(request_line)s" %(status_code)s'
|
||||
)
|
||||
LOGGING_CONFIG["formatters"]["access"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
|
||||
uvicorn.config.LOGGING_CONFIG["formatters"]["access"]["datefmt"] = (
|
||||
"%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user