java Dropwizard 日志记录:为特定记录器添加新的 appender
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31060481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dropwizard logging: add new appender for a particular logger
提问by nightcrawler
I'm new to dropwizard and am trying to figure out ways to configure logging better.
我是 dropwizard 的新手,正在尝试找出更好地配置日志记录的方法。
I have registered a new logger in a bundle like so:
我已经在一个包中注册了一个新的记录器,如下所示:
Logger log = LoggerFactory.getLogger("mylogger");
log.info("this is a log from mylogger");
Now I'm using this bundle in a bunch of services. By default any log that comes through this logger would be written to the application log file.
现在我在一堆服务中使用这个包。默认情况下,通过此记录器的任何日志都将写入应用程序日志文件。
The problem I'm trying to solve is this: I want all logs written by mylogger(only) to go to a new file. It is fairly starightforward to add a new appender to the service yml file like:
我试图解决的问题是:我希望 mylogger(only) 写入的所有日志都转到一个新文件。在服务 yml 文件中添加一个新的 appender 是相当直接的,例如:
logging:
loggers:
appenders:
- type: file.
currentLogFilename: ./logs/example.log
archivedLogFilenamePattern: ./logs/example-%d.log.gz
archivedFileCount: 5
But this would mean that all of application logs would now be written to example.log. I do not know of a way to specify a logger specifically for this appender which does not affect/alter already existing logging.
但这意味着所有应用程序日志现在都将写入 example.log。我不知道有什么方法可以专门为这个 appender 指定一个记录器,它不会影响/改变已经存在的日志记录。
Can someone tell me if there's a way to do this in dropwizard? Thanks!
有人可以告诉我是否有办法在 dropwizard 中做到这一点?谢谢!
回答by Denny Abraham Cheriyan
In Dropwizard 0.9.0 (released on Oct 28th 2015), they have added support for individual logger appenders and disabling logger additivity.
在 Dropwizard 0.9.0(2015 年 10 月 28 日发布)中,他们增加了对单个记录器附加程序的支持并禁用记录器可加性。
To achieve what you have described, you can specify the following in your yaml configuration file -
要实现您所描述的内容,您可以在 yaml 配置文件中指定以下内容 -
logging:
level: INFO
loggers:
"mylogger":
level: DEBUG
additive: false
appenders:
- type: file
currentLogFilename: /var/log/mylogger.log
archivedLogFilenamePattern: /var/log/mylogger-%d.log.gz
archivedFileCount: 5
appenders:
- type: console
Setting additive to false will prevent the logger (or anything under it) from writing to appenders which are hierarchically above it, including the root logger.
将additive 设置为false 将阻止记录器(或其下的任何内容)写入层次结构在其之上的附加程序,包括根记录器。
References -
参考 -
回答by cbradsh1
If you are wanting your custom logger to be the only logs in the log file, as well as going into the default appenders, you could create your own appender based on the Dropwizard logging spec.
如果您希望您的自定义记录器成为日志文件中唯一的日志,并进入默认的附加程序,您可以根据 Dropwizard 日志规范创建您自己的附加程序。
Requirements:
要求:
Custom Appender factory
自定义 Appender 工厂
that extends io.dropwizard.logging.AbstractAppenderFactory
(referred to as MyCustomFactory)
扩展io.dropwizard.logging.AbstractAppenderFactory
(称为 MyCustomFactory)
Custom configurations can be injected here from your .yml file by creating JsonProperties here, using Hibernate Annotations for any validations you need on the configuration file:
可以通过在此处创建 JsonProperties 来从 .yml 文件中注入自定义配置,使用 Hibernate Annotations 进行配置文件上所需的任何验证:
@NotNull
@JsonProperty
private String url;
@JsonProperty
private int maxBufferSize = 100;
@JsonProperty
private int sendDelayInSeconds = 10;
Give the class a @JsonTypeName("YourAppenderName")
. This is how you reference the appender in your configuration.
给班级一个@JsonTypeName("YourAppenderName")
. 这就是您在配置中引用 appender 的方式。
This class needs access to any configuration you want to pass to your appender, since the function of this class is to create your appenders that Dropwizard will use.
此类需要访问您想要传递给附加程序的任何配置,因为此类的功能是创建 Dropwizard 将使用的附加程序。
Custom Appender
自定义 Appender
that extends ch.qos.logback.core.AppenderBase
(Referred to as MyCustomAppender). You can write your own, or use an existing from from services like Loggly.
扩展ch.qos.logback.core.AppenderBase
(称为 MyCustomAppender)。您可以自己编写,也可以使用来自 Loggly 等服务的现有文件。
Inside the appender, check where the log is coming from and filter what you want to write to a file.
在 appender 中,检查日志的来源并过滤要写入文件的内容。
To tell Dropwizard about your new custom factory...
告诉 Dropwizard 你的新定制工厂......
You need a file called io.dropwizard.logging.AppenderFactory
placed into your src/main/resources/META-INF/services/
directory.
您需要一个名为的文件io.dropwizard.logging.AppenderFactory
放入您的src/main/resources/META-INF/services/
目录中。
The contents of this file is the full name (include packages) for your custom Factory. (Example: com.myCompany.appender.MyCustomFactory)
此文件的内容是您的自定义工厂的全名(包括包)。(例如:com.myCompany.appender.MyCustomFactory)
Using the custom appender
使用自定义 appender
In your yml file add the new appender by the name you specified:
在您的 yml 文件中,按照您指定的名称添加新的 appender:
appenders:
# Log warnings and errors to stderr
- type: console
threshold: INFO
target: stderr
# Custom Logger
- type: YourAppenderName
threshold: INFO
url: https://sendYourLogsHere/logs
回答by Chathurika Sandarenu
You can refer to the code in dropwizard-logging library for samples.
Check the FileAppenderFactory
. It is a good reference point.
示例可以参考 dropwizard-logging 库中的代码。检查FileAppenderFactory
. 这是一个很好的参考点。