简体   繁体   English

在jar文件中配置SLF4J

[英]Config SLF4J in jar file

I have my own jar file which is used as a library for my main application. 我有自己的jar文件,该文件用作我的主应用程序的库。 I need to set SLF4J as my logger and configure it in my jar. 我需要将SLF4J设置为记录器,并在jar中对其进行配置。

  • Write logs into a file 将日志写入文件
  • Set other configurations in a property file 在属性文件中设置其他配置

When it is googled, there are samples for web apps. 谷歌搜索时,会有一些针对网络应用的示例。 Any one let me know how to do above functionalities or mention any useful resource. 任何人都让我知道如何完成上述功能或提及任何有用的资源。

For a library, you should only include slf4j-api.jar . 对于库,您应仅包括slf4j-api.jar This means that in your code, you should only use classes within SLF4J's API, ie LoggerFactory and Logger . 这意味着在您的代码中,仅应使用SLF4J API中的类,即LoggerFactoryLogger

Your library should not define anything else regarding logging. 您的库不应定义任何其他有关日志记录的内容。 It's the responsibility of the application that uses your library to define the underlying logging implementation (logback, log4j, jcl, etc) and include the necessary bindings , as well as the underlying logging platform's configuration, such as a logback.xml file. 应用程序负责使用您的库来定义基础的日志记录实现(logback,log4j,jcl等),并包括必要的绑定以及基础日志记录平台的配置,例如logback.xml文件。

Please refer to the SLF4J manual for further reference. 请参考SLF4J手册以获取更多参考。

To log something using slf4j, you must include the slf4j-api jar in your application. 要使用slf4j记录内容,必须在应用程序中包含slf4j-api jar。 This is the bare minimum that is nessesary for slf4j logging. 这是slf4j日志记录所必需的最低要求。 If using maven, it can be included in the pom as shown below 如果使用maven,则可以将其包含在pom中,如下所示

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.10</version>
</dependency>

However the default logging behavior in slf4j is very simple. 但是,slf4j中的默认日志记录行为非常简单。 In nearly all circumstances you will want to send slf4j logs to a logging framework, which can perform more complex logging. 在几乎所有情况下,您都希望将slf4j日志发送到日志记录框架,该框架可以执行更复杂的日志记录。 There are several jars that can be included to accomplish this. 可以包括几个罐子来完成此操作。 I use log4j, so the examples below are for that framework. 我使用log4j,因此以下示例适用于该框架。 In order to log over log4j, slf4j-log4j12 jar is needed. 为了登录log4j,需要slf4j-log4j12 jar。

Pom dependancy Pom依赖

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.10</version>
</dependency>

From there all slf4j logs will be redirected to log4j, which will work normally. 从那里,所有slf4j日志都将重定向到log4j,它将正常工作。 Configure it through a log4j.properties file. 通过log4j.properties文件对其进行配置。 The below example log4j.properties will log things both to the console, and to the /tmp/logfile.log file. 下面的示例log4j.properties会将内容同时记录到控制台和/tmp/logfile.log文件中。 remove the console appender to only log to the file. 删除控制台附加程序以仅登录到该文件。

# Root logger option
log4j.rootLogger=INFO, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/logfile.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

If you want to log with a different framework, include the appropriate jar, and configure the underlying framework as you otherwise would. 如果要使用其他框架登录,请包括适当的jar,然后按其他方式配置基础框架。

SLF4J is not a logger, therefore you cannot configure anything. SLF4J不是记录器,因此您无法配置任何东西。 Use Logback. 使用Logback。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM