本文介紹了SSM項目中配置LOG4J日志的方法,分享給大家,具體如下:
在pom文件中添加依賴 .
1
2
3
4
5
6
7
8
9
10
11
|
<!--Log4j2配置--> < dependency > < groupId >org.apache.logging.log4j</ groupId > < artifactId >log4j-core</ artifactId > < version >2.8.1</ version > </ dependency > < dependency > < groupId >org.apache.logging.log4j</ groupId > < artifactId >log4j-api</ artifactId > < version >2.8.1</ version > </ dependency > |
然后在aopu或者攔截器中創建一個靜態的logger對象
1
|
private static final Logger logger = LogManager.getLogger(TestAop. class ); |
2.8.1版本之后使用的就是LogManager
導入的兩個包的名字分別是
1
2
|
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; |
使用這個logger的info方法將信息打印到控制臺
logger.info("當前執行的類[" + lei + "] 當前運行的方法[" + method + "]");
(當然需要在配置文件中配置,請看下面)
在項目的resource中添加文件log4j.properties文件(文件中內容的配置請自行百度添加修改)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration status = "OFF" > < appenders > < Console name = "Console" target = "SYSTEM_OUT" > < PatternLayout pattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </ Console > </ appenders > < loggers > < root level = "info" > < appender-ref ref = "Console" /> </ root > </ loggers > </ configuration > |
maven項目添加后重新編譯項目,如果target編譯后的文件夾中沒有properties文件的話在pom文件中添加
1
2
3
4
5
6
7
8
9
|
< resources > < resource > < directory >src/main/resources</ directory > < includes > < include >**/*.properties</ include > </ includes > < filtering >true</ filtering > </ resource > </ resources > |
這樣就會編譯了。
現在就可以啟動項目查看控制臺,和輸出到本地的log日志了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/chenyidong521/article/details/58067503