您好,登錄后才能下訂單哦!
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.
參考:
1.log4j2(1)
2.log4j2(2)
3.log4j2(3)
4.log4j2(4)
5.log4j2(5)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。