General logger
GeneralLogger
¶
Bases: Logger
General logger class, create a logger with config from config file for separate module.
Source code in agentuniverse/base/util/logging/general_logger.py
class GeneralLogger(Logger):
"""General logger class, create a logger with config from config file for
separate module."""
def __init__(self,
module_name: str,
log_path: str,
log_format: str,
log_rotation: str,
log_retention: str,
log_level: LOG_LEVEL = "INFO",
add_handler: bool = True):
"""Create a new logger instance used by a specific module.
Args:
module_name (`str`):
Name of the module, also used in log file name.
log_path (`str`):
Path of the log file.
log_format (`str`):
Format of recorded logs.
log_rotation (`str`):
Specifies the log rotation policy, controlling when a new log
file is created. It can be a time period (e.g., "1 week",
"10 days"), a file size (e.g., "100 MB"), or a function
returning True when rotation should occur.
log_retention (`str`):
Specifies the duration to keep old log files. It can be a time
span (e.g., "30 days") or a function to filter the files to be
retained. Files outside this policy are purged.
log_level (`LOG_LEVEL`, defaults to `"INFO"`):
Log level, values should be in LOG_LEVEL.
add_handler (`bool`, defaults to `True`):
Whether to add a new loguru handler only used to handle log
recorded by this instance.
"""
self.module_name = module_name
self.log_path = log_path
self.log_format = log_format
self.log_rotation = log_rotation
self.log_retention = log_retention
if add_handler:
self._add_handler(log_level)
def update_properties(self, **kwargs):
"""Update logger properties."""
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
else:
raise AttributeError(f"{self.__class__.__name__} "
f"has no attribute '{key}'")
def warn(self, msg, *args, **kwargs):
self._logger.opt(depth=1).bind(
source=self.module_name,
context_prefix=_get_context_prefix()
).warning(msg, *args, **kwargs)
def info(self, msg, *args, **kwargs):
self._logger.opt(depth=1).bind(
source=self.module_name,
context_prefix=_get_context_prefix()
).info(msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
self._logger.opt(depth=1).bind(
source=self.module_name,
context_prefix=_get_context_prefix()
).error(msg, *args, **kwargs)
def critical(self, msg, *args, **kwargs):
self._logger.opt(depth=1).bind(
source=self.module_name,
context_prefix=_get_context_prefix()
).critical(msg, *args, **kwargs)
def trace(self, msg, *args, **kwargs):
self._logger.opt(depth=1).bind(
source=self.module_name,
context_prefix=_get_context_prefix()
).trace(msg, *args, **kwargs)
def debug(self, msg, *args, **kwargs):
self._logger.opt(depth=1).bind(
source=self.module_name,
context_prefix=_get_context_prefix()
).debug(msg, *args, **kwargs)
def _add_handler(self, log_level: LOG_LEVEL = "INFO"):
"""Add a new loguru log handler, use instance module name to filter out
message recorded by this instance.
Args:
log_level (`str`, defaults to `"INFO"`):
Log level, values should be in LOG_LEVEL.
"""
self._logger.add(
sink=self.log_path,
level=log_level,
format=self.log_format,
rotation=self.log_rotation,
retention=self.log_retention,
compression='zip',
encoding="utf-8",
enqueue=True,
filter=_get_source_filter(self.module_name)
)
__init__(module_name, log_path, log_format, log_rotation, log_retention, log_level='INFO', add_handler=True)
¶
Create a new logger instance used by a specific module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_name |
`str`
|
Name of the module, also used in log file name. |
required |
log_path |
`str`
|
Path of the log file. |
required |
log_format |
`str`
|
Format of recorded logs. |
required |
log_rotation |
`str`
|
Specifies the log rotation policy, controlling when a new log file is created. It can be a time period (e.g., "1 week", "10 days"), a file size (e.g., "100 MB"), or a function returning True when rotation should occur. |
required |
log_retention |
`str`
|
Specifies the duration to keep old log files. It can be a time span (e.g., "30 days") or a function to filter the files to be retained. Files outside this policy are purged. |
required |
log_level |
`LOG_LEVEL`, defaults to `"INFO"`
|
Log level, values should be in LOG_LEVEL. |
'INFO'
|
add_handler |
`bool`, defaults to `True`
|
Whether to add a new loguru handler only used to handle log recorded by this instance. |
True
|
Source code in agentuniverse/base/util/logging/general_logger.py
def __init__(self,
module_name: str,
log_path: str,
log_format: str,
log_rotation: str,
log_retention: str,
log_level: LOG_LEVEL = "INFO",
add_handler: bool = True):
"""Create a new logger instance used by a specific module.
Args:
module_name (`str`):
Name of the module, also used in log file name.
log_path (`str`):
Path of the log file.
log_format (`str`):
Format of recorded logs.
log_rotation (`str`):
Specifies the log rotation policy, controlling when a new log
file is created. It can be a time period (e.g., "1 week",
"10 days"), a file size (e.g., "100 MB"), or a function
returning True when rotation should occur.
log_retention (`str`):
Specifies the duration to keep old log files. It can be a time
span (e.g., "30 days") or a function to filter the files to be
retained. Files outside this policy are purged.
log_level (`LOG_LEVEL`, defaults to `"INFO"`):
Log level, values should be in LOG_LEVEL.
add_handler (`bool`, defaults to `True`):
Whether to add a new loguru handler only used to handle log
recorded by this instance.
"""
self.module_name = module_name
self.log_path = log_path
self.log_format = log_format
self.log_rotation = log_rotation
self.log_retention = log_retention
if add_handler:
self._add_handler(log_level)
update_properties(**kwargs)
¶
Update logger properties.
Source code in agentuniverse/base/util/logging/general_logger.py
Logger
¶
Bases: ABC
The basic class of all logger, define all level log functions.
Source code in agentuniverse/base/util/logging/general_logger.py
class Logger(ABC):
"""The basic class of all logger, define all level log functions."""
@property
def _logger(self):
"""Logger field"""
return loguru.logger
@abstractmethod
def warn(self, msg, *args, **kwargs):
"""Log warn level message."""
raise NotImplementedError
@abstractmethod
def info(self, msg, *args, **kwargs):
"""Log info level message."""
raise NotImplementedError
@abstractmethod
def error(self, msg, *args, **kwargs):
"""Log error level message."""
raise NotImplementedError
@abstractmethod
def critical(self, msg, *args, **kwargs):
"""Log critical level message."""
raise NotImplementedError
@abstractmethod
def trace(self, msg, *args, **kwargs):
"""Log trace level message."""
raise NotImplementedError
@abstractmethod
def debug(self, msg, *args, **kwargs):
"""Log debug level message."""
raise NotImplementedError