English | 中文
The framework log backend is fixed as loguru. plugins.log configures the output list of a logger. The extension point is PluginType.LOG_WRITER. The built-in outputs are console and file.
import trpc_sdk.log completes registration: package initialization enters _loguru.py, that module then imports _writer.py, and the console and file decorators run at that moment. Your own writer module must also be imported from the service entry, before log initialization, or the decorator never runs.
PluginType.LOG has two built-in plugins. The runtime factory always takes the one named loguru. To extend output, register PluginType.LOG_WRITER, not PluginType.LOG.
DefaultLoggerInitPlugin, registered name"default".init()callsget_log_config()and runs only once.LoguruFactoryPlugin, registered name"loguru"(DEFAULT_LOG_BACKEND_NAME). Itscreate_loggerbuilds a logger from the output list.
get_log_config() reads plugins.log. The shape is "logger name → output list". "default" is the default logger name (DEFAULT_LOG_NAME). It shares a name with the init plugin above: one is the logger in yaml, and the other is the plugin that reads the configuration.
Each output corresponds to OutputConfig. An unknown key raises TypeError during construction.
writer: the registered name of aLOG_WRITER, such asconsoleorfile.level:trace/debug/info/warning/error/fatal. Bothtraceanddebugland on logging'sDEBUG. The default isdebug.formatter: an empty string or"json"uses the text layout."noop"and"full_json"use the formatter classes in_format.py. An unknown name falls back to"json".format_config:FormatConfig. What a writer actually uses isfmtandtime_fmt. The only percent fields that can be compiled into a native loguru template areasctime,levelname,filename,lineno,process,message, and%(msecs)03d. Other fields or modifiers fall back to the formatter callback.writer_config:WriterConfig. The built-inFileWriterreads onlyfilename,log_path,max_size,max_backups, andcompress:- The path is
log_path + "/" + filename. Whenlog_pathis empty, onlyfilenameis used. max_sizeis in megabytes. It is multiplied by1000000and passed to loguru'srotation. A value of0does not rotate.max_backupsis passed to loguru'sretention. A value of0does not limit how many files are kept.compress: trueenablesgzcompression.max_age,roll_type, andtime_unitonWriterConfigare not read byFileWriter.
- The path is
caller_skipis onOutputConfigand defaults to2. Current built-in writers do not read it.capture_third_partymust be the YAML booleantrueorfalse. A quoted"false"is a non-empty string and is treated as true.- When
third_party_levelis omitted, it followslevelof this output.
When a log binding is created, _LoguruSinkManager.create_binding looks up the registered PluginType.LOG_WRITER plugin by output_config.writer:
- If no plugin with that name is found, the code prints
cannot find writer pluginand skips this output. - If the instance subclasses
LoggerWriterPlugin, it callscreate_log_sink(output_config). create_log_sinkcalls the subclass's_get_sink_kwargs(output_config). A return value ofNoneskips this output as well.
A custom writer subclasses LoggerWriterPlugin and implements _get_sink_kwargs. This class is not exported from the trpc_sdk.log package. Import it from trpc_sdk.log._writer.
The built-in ConsoleWriter is the template. _common_sink_kwargs(output_config) already puts in level, format, and enqueue=True. The subclass adds sink and the other arguments loguru.logger.add needs:
import sys
from trpc_sdk.abc import PluginType
from trpc_sdk.log._writer import LoggerWriterPlugin
from trpc_sdk.plugin import register_plugin
@register_plugin(PluginType.LOG_WRITER, "console")
class ConsoleWriter(LoggerWriterPlugin):
def _get_sink_kwargs(self, output_config):
sink_kwargs = self._common_sink_kwargs(output_config)
sink_kwargs["sink"] = sys.stderr
sink_kwargs["colorize"] = False
return sink_kwargsTo write your own writer, change the registered name in the decorator, for example to "stderr", and import that module from the service entry.
Then set writer to that registered name. One logger can write to several outputs, so the value under default is a list:
plugins:
log:
default:
- writer: console
level: infoAfter the configuration is loaded, log calls from trpc_sdk.log such as info(...) or get_logger("default") write to the configured output plugins.