如何在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.examplewill apply to all loggers under thecom.examplepackage 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
相关产品推荐
相关产品推荐

