1. Overview
1.概述
In this tutorial, we’ll explore how to log messages to both file and console using the Apache Log4j2 library.
在本教程中,我们将探讨如何使用Apache Log4j2库将消息记录到文件和控制台。
This is very useful in a non-prod environment where we may want to see debug messages in the console, and we may want to persist the higher level logs to a file for later analysis.
这在非开发环境中非常有用,我们可能想在控制台中看到调试信息,我们可能想把高层日志持久化到一个文件中,以便以后分析。
2. Project Setup
2.项目设置
Let’s start by creating a Java project. We’ll add the log4j2 dependencies and look at how to configure and use the logger.
让我们从创建一个Java项目开始。我们将添加log4j2的依赖项,并看看如何配置和使用记录器。
2.1. Log4j2 Dependency
2.1. Log4j2的依赖性
Let’s add the log4j2 dependencies to our project. We’ll need the Apache Log4J Core and Apache Log4J API dependencies:
让我们把log4j2的依赖项添加到我们的项目中。我们将需要Apache Log4J Core和Apache Log4J API依赖项。
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.18.0</version>
</dependency>
</dependencies>
2.2. Application Class
2.2.应用类
Now let’s use the log4j2 library to add some logging to our application:
现在让我们使用log4j2库来为我们的应用程序添加一些日志。
public class Log4j2ConsoleAndFile {
private static final Logger logger = LogManager.getLogger(Log4j2ConsoleAndFile.class);
public static void main(String[] args) {
logger.info("Hello World!");
logger.debug("Hello World!");
}
}
3. Log4j2 Configuration
3.Log4j2配置
To autoconfigure the logger, we need to have a configuration file on the classpath. It can be in JSON, XML, YAML, or properties format. The file should be named log4j2. For our example, let’s use a configuration file named log4j2.properties.
为了自动配置记录器,我们需要在classpath上有一个配置文件。它可以是 JSON、XML、YAML 或属性格式。该文件应命名为log4j2。对于我们的例子,让我们使用一个名为log4j2.properties的配置文件。
3.1. Logging to the Console
3.1.登录到控制台
To log to any destination, we first need to define an appender that logs to the console. Let’s look at the configuration to do this:
要想将日志记录到任何目的地,我们首先需要定义一个appender,将日志记录到控制台。让我们看一下这样做的配置。
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
Let’s understand each of the components of the configuration:
让我们了解一下配置的每一个组成部分。
- appender.console.type – Here, we specify the type of the appender that will be used to log. The type Console specifies that the appender will only write to the console. We should note that the word console in the key name is only a convention and not mandatory.
- appender.console.name – We can give any unique name that we can use to refer to this appender later.
- appender.console.layout.type – This decides the name of the layout class used to format the log messages.
- appender.console.layout.pattern – This is the pattern that will be used to format the log messages.
To enable the console logger, we need to add the console appender to the root logger. We can do this using the name specified above:
为了启用控制台记录器,我们需要在根记录器中添加控制台附加器。我们可以使用上面指定的名称来完成:。
rootLogger=debug, STDOUT
Using this configuration, we’ll log all debug and above messages to the console. For a console running in a local environment, debug level logging is common.
使用这种配置,我们将把所有debug及以上的信息记录到控制台。对于在本地环境中运行的控制台来说,debug级别的日志记录是很常见的。
3.2. Logging to a File
3.2.记录到一个文件
Similarly, we can configure the logger to log to a file. This is often useful for persisting logs. Let’s define a file appender:
同样地,我们可以配置日志器来记录到一个文件。这对于持久化日志来说通常很有用。让我们来定义一个文件appender。
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=logs/log4j.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.filter.threshold.type = ThresholdFilter
appender.file.filter.threshold.level = info
In the case of a file appender, it’s mandatory to also specify the file name.
在文件应用者的情况下,必须同时指定文件名。
In addition to this, let’s also set the threshold level. Since we’re logging to a file, we don’t want to log all messages because it can take up a lot of persistent storage. We only want to log messages with a level of info or above. We can do this using the filter ThresholdFilter and setting its level info.
除此以外,我们也来设置一下阈值水平。由于我们要记录到一个文件,我们不想记录所有的消息,因为这会占用大量的持久性存储。我们只想记录级别为info或以上的消息。我们可以使用过滤器ThresholdFilter 并设置其级别info。
To enable the file logger, we need to add the file appender to the root logger. We need to change the rootLogger configuration to include the file appender:
为了启用文件记录器,我们需要将文件应用器添加到根记录器中。我们需要改变根记录器配置,以包括文件应用器:
rootLogger=debug, STDOUT, LOGFILE
Even if we have used the level debug at the root level, the file logger will only log info and above messages.
即使我们在根层使用了debug级别,文件记录器也只能记录info及以上的信息。
4. Testing
4.测试
Now let’s run the application and check the output in the console:
现在,让我们运行该应用程序,并检查控制台的输出。
12:43:47,891 INFO Application:8 - Hello World!
12:43:47,892 DEBUG Application:9 - Hello World!
As expected, we can see both the log messages in the console. If we check the log file at the path logs/log4j.log, we can see only the info level log message:
正如预期的那样,我们可以在控制台中看到这两条日志信息。如果我们检查路径为logs/log4j.log的日志文件,我们只能看到info级别的日志消息。
12:43:47,891 INFO Application:8 - Hello World!
5. Conclusion
5.总结
In this article, we learned how to log messages to both a console and a file. We created a Java project, configured Log4j2 using a properties file, and tested that messages are printed to both the console and file.
在这篇文章中,我们学习了如何将消息记录到控制台和文件中。我们创建了一个Java项目,使用属性文件配置了Log4j2,并测试了消息是否被打印到控制台和文件中。
As always, the full source code is available over on GitHub.
一如既往,完整的源代码可在GitHub上获得,。