您好,登錄后才能下訂單哦!
log4j是apache的一個(gè)開源項(xiàng)目,表示log for java.是一個(gè)日志工具,可以將日志信息輸出到文件,控制臺等其他地方 ,還可以定義日志的輸出格式,另外還有日志級別的區(qū)分,可通過配置文件去配置輸出格式,輸出目的地等信息.
log4j2從名字就知道了,是log4j的升級版本,性能有了很大提升,增加了很多新特性(如自定義級別,lambda表達(dá)式)與新的API,直接與tomcat等服務(wù)器集成.
點(diǎn)擊這里下載
作者用的是win10,下載zip.
搜索安裝:Eclipse New Java Project
ctrl+shift+p調(diào)出命令面板,輸入new java project
項(xiàng)目名
JavaSE版本
創(chuàng)建完畢.
在項(xiàng)目根路徑下新建lib文件夾,把下載的
這兩個(gè)jar包放進(jìn)去.
下一步設(shè)置.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");
}
}
點(diǎn)擊main上的Run
結(jié)果:
輸出了fatel與error信息.
另外還提示了沒有找到log4j2配置文件,使用默認(rèn)的配置文件,默認(rèn)輸出到控制臺.
src下創(chuàng)建log4j2.xml配置文件.
log4j2的配置文件只能是.xml或.json或.jsn文件,默認(rèn)會在classpath下尋找(在這里classpath相當(dāng)于圖中的src).
默認(rèn)情況下系統(tǒng)選擇配置文件的優(yōu)先級如下:
這里使用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三個(gè)主要的組件為
<appenders>了輸出目的地,<Console>表示控制臺,<PatternLayout>定義了布局,里面的布局表示時(shí)間格式(%d,HH:mm:ss.SSS),接著是日志線程名(%t),%-5表示最小長度為5左對齊,level表示輸出日志級別.
%logger表示輸出logger名稱.
%m是%msg與%message的縮寫,表示輸出應(yīng)用提供的與記錄事件有關(guān)的信息.
%n表示換行.
<loggers>定義的logger與logger使用的appender.root為log默認(rèn)的輸出形式,如果一個(gè)類的沒有明確loggers中的輸出格式就會采用root的格式.
沒有之前的"沒有配置文件"的提示了.
使用插件maven for java創(chuàng)建,命令面板(ctrl+shift+p)輸入.
選擇quickstart.
選擇版本.
最后選擇項(xiàng)目路徑即可.
在控制臺中vscode會下載相關(guān)文件,然后會提示以交互模式創(chuàng)建:
這里等一會就好了.
然后會提示輸入一些值.
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>
修改默認(rèn)的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.
勾選從模板創(chuàng)建項(xiàng)目.
選好路徑與填好項(xiàng)目名即可.
選擇剛才的lib目錄,或者自己手動(dòng)改成兩個(gè)jar包(log4j-api-版本.jar,log4j-core-版本.jar).
項(xiàng)目結(jié)構(gòu)如下:
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>
直接點(diǎn)擊run按鈕.
這里作者偷懶就直接在上面項(xiàng)目的基礎(chǔ)上新建模塊不新建項(xiàng)目了.
都不用選直接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下創(chuàng)建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>
創(chuàng)建包與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.
參考:
1.log4j2(1)
2.log4j2(2)
3.log4j2(3)
4.log4j2(4)
5.log4j2(5)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。