log4j是apache的一个开源项目,表示log for java.是一个日志工具,可以将日志信息输出到文件,控制台等其他地方 ,还可以定义日志的输出格式,另外还有日志级别的区分,可通过配置文件去配置输出格式,输出目的地等信息.
log4j2从名字就知道了,是log4j的升级版本,性能有了很大提升,增加了很多新特性(如自定义级别,lambda表达式)与新的API,直接与tomcat等服务器集成.
点击这里下载
作者用的是win10,下载zip.
搜索安装:Eclipse New Java Project
ctrl+shift+p调出命令面板,输入new java project
项目名
JavaSE版本
创建完毕.
在项目根路径下新建lib文件夹,把下载的
这两个jar包放进去.
下一步设置.classpath.
添加
<classpathentry kind="lib" path="lib/log4j-api-2.12.1.jar" /> <classpathentry kind="lib" path="lib/log4j-core-2.12.1.jar" />
src下直接建立test.java
test.java:
import org.apache.logging.log4j.*; public class test { private final static Logger logger = LogManager.getLogger(test.class.getName()); public static void main(String[] args) { logger.info("info"); logger.error("error"); logger.debug("debug"); logger.trace("trace"); logger.fatal("fatel"); } }
点击main上的Run
结果:
输出了fatel与error信息.
另外还提示了没有找到log4j2配置文件,使用默认的配置文件,默认输出到控制台.
src下创建log4j2.xml配置文件.
log4j2的配置文件只能是.xml或.json或.jsn文件,默认会在classpath下寻找(在这里classpath相当于图中的src).
默认情况下系统选择配置文件的优先级如下:
这里使用log4j2.
xml.log4j2.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </appenders> <loggers> <logger name="test" level="info" additivity="false"> <appender-ref ref="Console"/> </logger> <root level="info"> <appender-ref ref="Console"/> </root> </loggers> </configuration>
Logger三个主要的组件为
<appenders>了输出目的地,<Console>表示控制台,<PatternLayout>定义了布局,里面的布局表示时间格式(%d,HH:mm:ss.SSS),接着是日志线程名(%t),%-5表示最小长度为5左对齐,level表示输出日志级别.
%logger表示输出logger名称.
%m是%msg与%message的缩写,表示输出应用提供的与记录事件有关的信息.
%n表示换行.
<loggers>定义的logger与logger使用的appender.root为log默认的输出形式,如果一个类的没有明确loggers中的输出格式就会采用root的格式.
没有之前的"没有配置文件"的提示了.
使用插件maven for java创建,命令面板(ctrl+shift+p)输入.
选择quickstart.
选择版本.
最后选择项目路径即可.
在控制台中vscode会下载相关文件,然后会提示以交互模式创建:
这里等一会就好了.
然后会提示输入一些值.
ok.
在<dependencies>中添加如下代码:
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.12.1</version> </dependency>
以下是完整的pom.xml:
<?xml version="1.0" encoding="UTF-8" ?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <name>test</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.12.1</version> </dependency> </dependencies> <build> <pluginManagement> <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
右键选择Update project configuration.
在java目录下新建log4j2.xml.
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </appenders> <loggers> <logger name="com.example.App" level="info" additivity="false"> <appender-ref ref="Console"/> </logger> <root level="info"> <appender-ref ref="Console"/> </root> </loggers> </configuration>
修改默认的App.java.
package com.example; import org.apache.logging.log4j.*; public class App { private static final Logger logger = LogManager.getLogger(App.class.getName()); public static void main( String[] args ) { logger.info("info"); logger.error("error"); logger.debug("debug"); logger.trace("trace"); logger.fatal("fatel"); } }
run即可.
什么也不用选,直接next.
勾选从模板创建项目.
选好路径与填好项目名即可.
选择刚才的lib目录,或者自己手动改成两个jar包(log4j-api-版本.jar,log4j-core-版本.jar).
项目结构如下:
Main.java
package com.test; import org.apache.logging.log4j.*; public class Main { private static final Logger logger = LogManager.getLogger(Main.class.getName()); public static void main(String[] args) { logger.info("info"); logger.error("error"); logger.debug("debug"); logger.trace("trace"); logger.fatal("fatel"); } }
在src下新建log4j2.xml.
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </appenders> <loggers> <logger name="com.test.Main" level="info" additivity="false"> <appender-ref ref="Console"/> </logger> <root level="info"> <appender-ref ref="Console"/> </root> </loggers> </configuration>
直接点击run按钮.
这里作者偷懒就直接在上面项目的基础上新建模块不新建项目了.
都不用选直接next.
填好GroupId与ArtifactId之后next.
最后选好位置.
pom.xml中加入:
<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.12.1</version> </dependency> </dependencies>
以下是完整的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.12.1</version> </dependency> </dependencies> </project>
右键选择同步pom.xml.
选择右下角弹出的提示中的Import Changes.
在target下的classes下创建log4j2.xml.
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </appenders> <loggers> <logger name="com.example.test" level="info" additivity="false"> <appender-ref ref="Console"/> </logger> <root level="info"> <appender-ref ref="Console"/> </root> </loggers> </configuration>
创建包与test.java.
test.java
package com.example; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class test { private static final Logger logger = LogManager.getLogger(test.class.getName()); public static void main(String[] args) { logger.info("info"); logger.error("error"); logger.debug("debug"); logger.trace("trace"); logger.fatal("fatel"); } }
新建配置Application.
输入Main class与名字.
run.
这篇文章主要写了log4j2的入门级别的使用.以下是作者的公众号,欢迎关注.
参考:
1. log4j2(1)
2. log4j2(2)
3. log4j2(3)
4. log4j2(4)
5. log4j2(5)