トッカンソフトウェア

Log4j2でログ出力(Web)

今回はLog4j2を使用してWebアプリでログを出力してみます。Log4j2の基本については前回を参照下さい。
log4j2.xmlは前回使用したものをそのまま使用します。

Webアプリの作成はこちらを参照下さい。

環境構築

前回に使用したJarに加えて、log4j-web-2.9.1.jar を使用します。
log4j-web-2.9.1.jar は前回ダウンロードした apache-log4j-2.9.1-bin.zip を解凍した中に入っています。

今回使用するJarは以下になります。

これらのJarファイルを WEB-INF\lib の中に入れます。
log4j2.xmlはWEB-INFに入れます。

ファイル構成は以下になります。


※log4j-web-2.9.1.jarがない場合、log4j2.xmlをWEB-INFに入れても読み込まれません。
またTomcatやJavaのバージョンなどが古い場合、log4j-web-2.9.1.jarがあっても読み込まれません。
log4j2.xmlが読み込まれない場合、前回と同じようにlog4j2.xmlをsrc直下に置いて下さい。


log4j2.xmlの作成

log4j2.xmlは前回に作成したものをそのまま使用します。

Javaファイル(Servlet)にログ出力処理の埋め込み

				
package test;

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.LogManager;
import org.apache.logging.log4j.Logger;

@WebServlet("/TestGet")
public class TestG extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Logger logger = LogManager.getLogger(TestG.class);

	public TestG() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		logger.trace("_trace");
		logger.debug("_debug");
		logger.info("_info");
		logger.warn("_warn");
		logger.error("_error");
		logger.fatal("_fatal");
		
		request.setCharacterEncoding("UTF8");
		response.setContentType("text/html; charset=UTF8");
		response.getWriter().append("get1:").append(request.getParameter("aaa"));
		response.getWriter().append("<BR>");
		response.getWriter().append("get2:").append(request.getParameter("bbb"));

		System.out.println("作業フォルダ:" + System.getProperty("user.dir"));

	}
}

			

処理結果

Fileにログを出力する場合、普通にEclipseで実行すれば、Eclipseのフォルダに出力されていると思います。
もしログが存在しない場合は、System.out.printlnで出力した作業フォルダを確認して下さい。

10 02, 2017 7:40:55 午前 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:webTest' did not find a matching property.
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server version:        Apache Tomcat/8.0.24
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server built:          Jul 1 2015 20:19:55 UTC
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server number:         8.0.24.0
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: OS Name:               Windows 10
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: OS Version:            10.0
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Architecture:          amd64
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Java Home:             C:\Program Files\Java\jre1.8.0_73
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: JVM Version:           1.8.0_73-b02
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: JVM Vendor:            Oracle Corporation
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: CATALINA_BASE:         C:\Users\akira\workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: CATALINA_HOME:         C:\apache-tomcat-8.0.24
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dfile.encoding=UTF-8
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dsun.jnu.encoding=UTF-8
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dcatalina.base=C:\Users\akira\workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dcatalina.home=C:\apache-tomcat-8.0.24
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dwtp.deploy=C:\Users\akira\workspace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-8.0.24\endorsed
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dfile.encoding=UTF-8
10 02, 2017 7:40:55 午前 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
情報: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_73\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_73/bin/server;C:/Program Files/Java/jre1.8.0_73/bin;C:/Program Files/Java/jre1.8.0_73/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\EgisTec MyWinLocker\x64;C:\Program Files (x86)\EgisTec MyWinLocker\;C:\Program Files\Microsoft Windows Performance Toolkit\;c:\Android\android-sdk\tools;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\TortoiseHg\;C:\Java\apache-maven-3.2.5\bin;%USERPROFILE%\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Nodist\bin;C:\ProgramData\Miniconda3;C:\ProgramData\Miniconda3\Scripts;C:\ProgramData\Miniconda3\Library\bin;C:\Program Files\VisualSVN Server\bin;C:\Program Files (x86)\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files (x86)\Microsoft Visual Studio\Common\Tools;C:\Program Files (x86)\Microsoft Visual Studio\VC98\bin;C:\Users\akira\AppData\Local\atom\bin;C:\Program Files (x86)\Microsoft VS Code\bin;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;C:\eclipseEEMars;;.
10 02, 2017 7:40:55 午前 org.apache.coyote.AbstractProtocol init
情報: Initializing ProtocolHandler ["http-nio-8080"]
10 02, 2017 7:40:55 午前 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
情報: Using a shared selector for servlet write/read
10 02, 2017 7:40:55 午前 org.apache.coyote.AbstractProtocol init
情報: Initializing ProtocolHandler ["ajp-nio-8009"]
10 02, 2017 7:40:55 午前 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
情報: Using a shared selector for servlet write/read
10 02, 2017 7:40:55 午前 org.apache.catalina.startup.Catalina load
情報: Initialization processed in 1032 ms
10 02, 2017 7:40:55 午前 org.apache.catalina.core.StandardService startInternal
情報: サービス Catalina を起動します
10 02, 2017 7:40:55 午前 org.apache.catalina.core.StandardEngine startInternal
情報: Starting Servlet Engine: Apache Tomcat/8.0.24
10 02, 2017 7:40:56 午前 org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
警告: Name = postgres Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
10 02, 2017 7:40:56 午前 org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
警告: Name = postgres Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
10 02, 2017 7:40:56 午前 org.apache.jasper.servlet.TldScanner scanJars
情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
10 02, 2017 7:40:57 午前 org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
警告: Name = postgres Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
10 02, 2017 7:40:57 午前 org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
警告: Name = postgres Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
10 02, 2017 7:40:57 午前 org.apache.jasper.servlet.TldScanner scanJars
情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
10 02, 2017 7:40:58 午前 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler ["http-nio-8080"]
10 02, 2017 7:40:58 午前 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler ["ajp-nio-8009"]
10 02, 2017 7:40:58 午前 org.apache.catalina.startup.Catalina start
情報: Server startup in 2933 ms
2017-10-02 07:41:01.510 p=TRACE c=test.TestG t=http-nio-8080-exec-2 C=test.TestG F=TestG.java M=doGet L=25 m=_trace 
2017-10-02 07:41:01.526 p=DEBUG c=test.TestG t=http-nio-8080-exec-2 C=test.TestG F=TestG.java M=doGet L=26 m=_debug 
2017-10-02 07:41:01.526 p=INFO  c=test.TestG t=http-nio-8080-exec-2 C=test.TestG F=TestG.java M=doGet L=27 m=_info 
2017-10-02 07:41:01.526 p=WARN  c=test.TestG t=http-nio-8080-exec-2 C=test.TestG F=TestG.java M=doGet L=28 m=_warn 
2017-10-02 07:41:01.527 p=ERROR c=test.TestG t=http-nio-8080-exec-2 C=test.TestG F=TestG.java M=doGet L=29 m=_error 
2017-10-02 07:41:01.527 p=FATAL c=test.TestG t=http-nio-8080-exec-2 C=test.TestG F=TestG.java M=doGet L=30 m=_fatal 
作業フォルダ:C:\eclipseEEMars


ページのトップへ戻る