You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在tinylog中配置并行双控制台写入器及差异化日志格式?

Does tinylog support custom formats for specific loggers?

Absolutely! tinylog 2.x fully supports defining unique output formats for specific loggers—this is a supported feature that’s easy to implement with your existing setup.

How to configure it

You can leverage tinylog’s logger-specific configuration keys to override the global format for targeted loggers. Here’s a quick example that builds on your existing code:

// Your existing global console writer configuration
Configuration.set("writer", "console");
Configuration.set("writer.format", "{level}: {class-name}.{method}(): {message}");

// Set a custom format for a specific logger (e.g., "com.example.MyService")
Configuration.set("writer.format.com.example.MyService", "[SERVICE LOG] {level} | {message}");

// Alternatively, apply a custom format to all loggers in a package
Configuration.set("writer.format.com.example.utils", "[UTILS] {level}: {class-name} - {message}");

Using the configured loggers

When you retrieve loggers by name or class, tinylog will automatically apply the appropriate format:

// Uses the custom "[SERVICE LOG]" format
Logger serviceLogger = Logger.getLogger("com.example.MyService");
serviceLogger.info("Processing user request");

// Uses the custom "[UTILS]" format
Logger utilsLogger = Logger.getLogger(com.example.utils.FileHandler.class);
utilsLogger.debug("Loaded configuration file");

// Uses the global format
Logger regularLogger = Logger.getLogger(com.example.App.class);
regularLogger.warn("Low disk space warning");

Key notes

  • Logger names are hierarchical: configuring writer.format.com.example will apply to all loggers under the com.example package and its subpackages.
  • You can also configure entirely separate writers (e.g., file writers) for specific loggers if needed, but the above example focuses on overriding the format for the existing console writer.

Hope this helps you get the targeted logging formatting you need!

内容的提问来源于stack exchange,提问作者Christof Nasahl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 19:08:15