このチュートリアルでは、従来のlog4j 1.2.xを使用して、Javaアプリケーションでデバッグ・メッセージまたはエラー・メッセージを記録する方法を説明します。
1.プロジェクトディレクトリ
標準的なMavenスタイルのJavaプロジェクトである、最終的なプロジェクト構造を見直してください。
2. Log4jを入手する
次の依存関係を宣言します。
pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
非Mavenユーザの場合は、http://logging.apache.org/log4j/1.2/[log4j公式ページ]にアクセスし、jarファイルをダウンロードしてプロジェクトライブラリパスに手動で入力します。
3. log4j.properties
`log4j.properties`ファイルを作成し、それをresourcesフォルダに入れます。
上記の手順1を参照してください。
`project/classes`ディレクトリの下にあります
。 Java Webアプリケーションの場合、 `log4j.properties`ファイルが
`WEB-INF/classes`ディレクトリの下にあります
log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
-
Note ** + `ConversionPattern`のシンボルについては、http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html[log4j PatternLayout guide]を参照してください。
それを打ち破ろう:
-
%d \ {yyyy-MM-dd HH:mm:ss} =日付と時刻のフォーマット、参照先
SimpleDateFormat
JavaDoc。
-
%-5p = DEBUGやERRORのようなロギングの優先順位. -5はオプションですが、
かなりの印刷フォーマットのために。
-
%c \ {1} = getLogger()で設定したロギング名です.
log4j PatternLayout guide
をご覧ください。
-
%L =ロギング要求の行番号.
-
%m%n =ログと改行のメッセージ.
ログメッセージの例:
2014-07-02 20:52:39 DEBUG className:200 - This is debug message 2014-07-02 20:52:39 DEBUG className:201 - This is debug message2
4.デモ – メッセージを記録する方法?
メッセージを記録するには、まず最終的な静的ロガーを作成し、ロガーの名前を定義します。通常は、完全なパッケージクラス名を使用します。
final static Logger logger = Logger.getLogger(classname.class);
次に、さまざまな優先度を持つメッセージをログに記録します(例:debug、info、warn、error、fatal)。通常は、デバッグまたはエラーを使用するだけです。
//logs a debug message
if(logger.isDebugEnabled()){
logger.debug("This is debug");
}
//logs an error message with parameter
logger.error("This is error : " + parameter);
//logs an exception thrown from somewhere
logger.error("This is error", exception);
4.1例:
Loggerは
debug
priorityに設定されています。
log4j.properties
log4j.rootLogger=DEBUG, stdout #...
HelloExample.java
package com.mkyong;
import org.apache.log4j.Logger;
public class HelloExample{
final static Logger logger = Logger.getLogger(HelloExample.class);
public static void main(String[]args) {
HelloExample obj = new HelloExample();
obj.runMe("mkyong");
}
private void runMe(String parameter){
if(logger.isDebugEnabled()){
logger.debug("This is debug : " + parameter);
}
if(logger.isInfoEnabled()){
logger.info("This is info : " + parameter);
}
logger.warn("This is warn : " + parameter);
logger.error("This is error : " + parameter);
logger.fatal("This is fatal : " + parameter);
}
}
出力
2014-07-02 20:52:39 DEBUG HelloExample:19 - This is debug : mkyong 2014-07-02 20:52:39 INFO HelloExample:23 - This is info : mkyong 2014-07-02 20:52:39 WARN HelloExample:26 - This is warn : mkyong 2014-07-02 20:52:39 ERROR HelloExample:27 - This is error : mkyong 2014-07-02 20:52:39 FATAL HelloExample:28 - This is fatal : mkyong
4.2例-
Loggerは
error
priorityに設定されています。
log4j.properties
log4j.rootLogger=error, stdout #...
`HelloExample`をもう一度実行すると、以下の出力が得られます
2014-07-02 20:56:02 ERROR HelloExample:27 - This is error : mkyong 2014-07-02 20:56:02 FATAL HelloExample:28 - This is fatal : mkyong
log4jの `Priority`クラスを見直してください。
Priority.java
package org.apache.log4j;
public class Priority {
public final static int OFF__INT = Integer.MAX__VALUE;
public final static int FATAL__INT = 50000;
public final static int ERROR__INT = 40000;
public final static int WARN__INT = 30000;
public final static int INFO__INT = 20000;
public final static int DEBUG__INT = 10000;
//public final static int FINE__INT = DEBUG__INT;
public final static int ALL__INT = Integer.MIN__VALUE;
priorityが `log4j.properties`で定義されている場合、優先度が同じかそれ以上のメッセージだけが記録されます。
5.デモ – 例外をログに記録する方法
log4jを使用して例外を記録する方法を示す例です。
HelloExample2.java
package com.mkyong;
import org.apache.log4j.Logger;
public class HelloExample2{
final static Logger logger = Logger.getLogger(HelloExample2.class);
public static void main(String[]args) {
HelloExample2 obj = new HelloExample2();
try{
obj.divide();
}catch(ArithmeticException ex){
logger.error("Sorry, something wrong!", ex);
}
}
private void divide(){
int i = 10/0;
}
}
出力
2014-07-02 21:03:10 ERROR HelloExample2:16 - Sorry, something wrong!
java.lang.ArithmeticException:/by zero
at com.mkyong.HelloExample2.divide(HelloExample2.java:24)
at com.mkyong.HelloExample2.main(HelloExample2.java:14)
完了しました。
ソースコードをダウンロードする
ソースコードのダウンロード – リンク://wp-content/uploads/2014/07/log4j-hello-world.zip[log4j-hello-world-example.zip](8 KB)
参考文献
パターンレイアウト]。
ウィキペディア:log4j
-
リンク://spring-mvc/spring-mvc-log4j-integration-example/[Spring MVC
log4jの例]。リンク://logging/log4j-log4j-properties-examples/[log4j.properties
例]