Java slf4j 日志文件保存在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21881846/
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
Where does the slf4j log file get saved?
提问by user2763361
I have the followed imports:
我有以下进口:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
and the following instantiation:
以及以下实例化:
private static Logger logger = LoggerFactory.getLogger(Test.class);
and the following in my Main method:
以及我的 Main 方法中的以下内容:
logger.info("SOME MESSAGE: ");
However, I'm not able to find the output anywhere. All I see is that in my console there is:
但是,我无法在任何地方找到输出。我所看到的只是在我的控制台中有:
21:21:24.235 [main] INFO some_folder.Test - SOME MESSAGE:
How do I locate the log file?
如何找到日志文件?
Note that the following are on my build path:
请注意,以下是我的构建路径:
slf4j-api-1.7.5.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.6.4.jar
slf4j-log4j12-1.6.4.jar
I read the answer to similar questions but nobody actually says how to fix the problem.
我阅读了类似问题的答案,但实际上没有人说如何解决问题。
回答by jmkgreen
It does not write to a file by default. You would need to configure something like the RollingFileAppender
and have the root logger write to it (possibly in addition to the default ConsoleAppender
).
默认情况下它不会写入文件。您需要配置类似的东西RollingFileAppender
并让根记录器写入它(可能除了默认的ConsoleAppender
)。
回答by Arnaud Denoyelle
slf4j is only an API. You should have a concrete implementation (for example log4j). This concrete implementation has a config file which tells you where to store the logs.
slf4j 只是一个 API。您应该有一个具体的实现(例如 log4j)。这个具体的实现有一个配置文件,它告诉你在哪里存储日志。
When slf4j catches a log messages with a logger, it is given to an appender which decides what to do with the message. By default, the ConsoleAppender displays the message in the console.
当 slf4j 使用记录器捕获日志消息时,会将其提供给决定如何处理消息的附加程序。默认情况下,ConsoleAppender 在控制台中显示消息。
The default configuration file is :
默认配置文件是:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<!-- By default => console -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
If you put a configuration file available in the classpath, then your concrete implementation (in your case, log4j) will find and use it. See Log4J documentation.
如果您在类路径中放置了一个可用的配置文件,那么您的具体实现(在您的情况下为 log4j)将找到并使用它。请参阅Log4J 文档。
Example of file appender :
文件附加程序示例:
<Appenders>
<File name="File" fileName="${filename}">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
...
</Appenders>
Complete example with a file appender :
带有文件附加程序的完整示例:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="File" fileName="${filename}">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
回答by Jay
As already mentioned its just a facade and it helps to switch between different logger implementation easily. For example if you want to use log4j implementation.
正如已经提到的,它只是一个外观,它有助于轻松地在不同的记录器实现之间切换。例如,如果您想使用 log4j 实现。
A sample code would looks like below.
示例代码如下所示。
If you use maven get the dependencies
如果您使用 maven 获取依赖项
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
Have the below in log4j.properties in location src/main/resources/log4j.properties
在位置 src/main/resources/log4j.properties 的 log4j.properties 中有以下内容
log4j.rootLogger=DEBUG, STDOUT, file
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylogs.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
Hello world code below would prints in console and to a log file as per above configuration.
下面的 Hello world 代码将按照上述配置在控制台和日志文件中打印。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}
回答by Rahul Vanimisetty
The log file is not visible because the slf4j configuration file location needs to passed to the java run command using the following arguments .(e.g.)
日志文件不可见,因为需要使用以下参数将 slf4j 配置文件位置传递给 java run 命令。(例如)
-Dlogging.config={file_location}\log4j2.xml
or this:
或这个:
-Dlog4j.configurationFile={file_location}\log4j2.xml