每個Level
有對應的intLevel
整數值,數值越小等級越高,數值越大等級越低。
Level | intValue | 用途 |
---|---|---|
OFF | 0 | 不輸出任何日誌 |
FETAL | 100 | 造成應用程式停止的日誌 |
ERROR | 200 | 造成應用程式錯誤的日誌 |
WARN | 300 | 可能導致錯誤的日誌 |
INFO | 400 | 一般資訊的日誌 |
DEBUG | 500 | 除錯資訊的日誌 |
TRACE | 600 | 更細的除錯資訊,通常用來追蹤程式流程的日誌 |
ALL | Integer.MAX_VALUE | 輸出所有日誌 |
Logger輸出的等級大於等於Logger設定的等級時才會輸出訊息。
高---------------------------------------低
FETAL > ERROR > WARN > INFO > DEBUG > TRACE
要使用Log4j2需四個項目:(tomcat須為含7以上)
一.下載 apache Log4j2
https://logging.apache.org/log4j/2.x/download.html
二.設定使用log4j-api-2.xxx.jar及log4j-core-2.xxx.ja進classpath
如在Eclipse IDE中須將上述的兩個jar放入
(a).webapps--> builderPath-->Libraries-->classpath
可消除編輯程式碼的錯誤
(b).DynamicWebProject下的WEB-INF/lib目錄
可消除執行程式碼的錯誤
三.建立Log4j2的參數檔,檔名為log4j2開頭,副檔名及格式可為".properties","xml',"json"
以下是log4j2.xml範例
==================================================================
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
==================================================================
Log4j2的參數檔,放置位置有src,classpath及WEB-INF三個地方,JSP DynamicWebProject
我建議 WEB-INF目錄
四.Log4j2的servlet及jsp範例程式如下:
==================================================================
package com.lccnet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
/**
* Servlet implementation class Servlet1
*/
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
Logger log = LogManager.getLogger(this.getClass());
log.info("Show INFO message");
log.warn("Show WARN message");
log.error("Show ERROR message");
log.fatal("Show FATAL message");
/*
* 高---------------------------------------低
* FETAL > ERROR > WARN > INFO > DEBUG > TRACE
* */
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
==================================================================
==================================================================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.apache.logging.log4j.Logger,org.apache.logging.log4j.LogManager"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello World!
<%
Logger log = LogManager.getLogger(this.getClass());
log.info("Show INFO message");
log.warn("Show WARN message");
log.error("Show ERROR message");
log.fatal("Show FATAL message");
%>
</body>
</html>
==================================================================
沒有留言:
張貼留言