Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions astrbot/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from astrbot.core.agent.tool import FunctionTool, ToolSet
from astrbot.core.agent.tool_executor import BaseFunctionToolExecutor
from astrbot.core.config.astrbot_config import AstrBotConfig
from astrbot.core.log import get_loguru_logger
from astrbot.core.star.register import register_agent as agent
from astrbot.core.star.register import register_llm_tool as llm_tool

loguru_logger = get_loguru_logger()

__all__ = [
"AstrBotConfig",
"BaseFunctionToolExecutor",
Expand All @@ -15,5 +18,6 @@
"html_renderer",
"llm_tool",
"logger",
"loguru_logger",
"sp",
]
22 changes: 21 additions & 1 deletion astrbot/core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ def _is_plugin_path(pathname: str | None) -> bool:
return ("data/plugins" in norm_path) or ("astrbot/builtin_stars/" in norm_path)


def _get_plugin_tag(pathname: str | None) -> str:
if not pathname:
return "[Core]"
norm_path = os.path.normpath(pathname)
for prefix in (
"data" + os.sep + "plugins" + os.sep,
"astrbot" + os.sep + "builtin_stars" + os.sep,
):
if prefix in norm_path:
idx = norm_path.index(prefix) + len(prefix)
plugin_name = norm_path[idx:].split(os.sep)[0]
return f"[{plugin_name}]"
return "[Core]"


def _get_short_level_name(level_name: str) -> str:
level_map = {
"DEBUG": "DBUG",
Expand All @@ -81,7 +96,7 @@ def _build_source_file(pathname: str | None) -> str:

def _patch_record(record: "Record") -> None:
extra = record["extra"]
extra.setdefault("plugin_tag", "[Core]")
extra.setdefault("plugin_tag", _get_plugin_tag(record["file"].path))
extra.setdefault("short_levelname", _get_short_level_name(record["level"].name))
level_no = record["level"].no
extra.setdefault("astrbot_version_tag", f" [v{VERSION}]" if level_no >= 30 else "")
Expand Down Expand Up @@ -415,3 +430,8 @@ def configure_trace_logger(cls, config: dict | None) -> None:
backup_count=3,
trace=True,
)


def get_loguru_logger():
"""Returns the patched loguru logger for plugin use."""
return _loguru
Comment on lines +435 to +437
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The get_loguru_logger function currently returns the base patched logger, which defaults the plugin_tag to [Core] in the _patch_record logic (line 84). Since this function is explicitly intended for plugin use, it should return a logger instance bound with the [Plug] tag. This ensures that logs generated by plugins using this API are correctly categorized in the output, while still allowing plugins to override the tag if necessary using .bind(). As this introduces new behavior for plugin logging, please include a corresponding unit test.

Suggested change
def get_loguru_logger():
"""Returns the patched loguru logger for plugin use."""
return _loguru
def get_loguru_logger():
"""Returns the patched loguru logger for plugin use."""
return _loguru.bind(plugin_tag="[Plug]")
References
  1. New functionality should be accompanied by corresponding unit tests.

Loading