Log4j-htmllayout
提供:Dev Guides
log4j-HTMLLayout
ロギング情報をHTML形式のファイルで生成する場合は、org.apache.log4j.HTMLLayoutを使用してロギング情報をフォーマットできます。
HTMLLayoutクラスは、抽象org.apache.log4j.Layoutクラスを拡張し、その基本クラスからformat()メソッドをオーバーライドして、HTMLスタイルのフォーマットを提供します。
表示される次の情報を提供します。
- 特定のロギングイベントが生成される前に、アプリケーションの開始から経過した時間。
- ロギング要求を呼び出したスレッドの名前。
- このロギング要求に関連付けられたレベル。
- ロガーとロギングメッセージの名前。 *プログラムファイルのオプションの場所情報と、このログが呼び出された行番号。
HTMLLayoutは、次のメソッドを提供する非常にシンプルなレイアウトオブジェクトです。
Sr.No. | Method & Description |
---|---|
1 |
text/html HTMLコンテンツのコンテンツタイプを設定します。 デフォルトはtext/htmlです。 |
2 |
setLocationInfo(String) ロギングイベントの場所情報を設定します。 デフォルトはfalseです。 |
3 |
setTitle(String) HTMLファイルのタイトルを設定します。 デフォルトはlog4j Log Messagesです。 |
HTMLLayoutの例
HTMLLayoutの簡単な設定ファイルは次のとおりです。
# Define the root logger with appender file
log =/usr/home/log4j
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/htmlLayoutl
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.Title=HTML Layout Example
log4j.appender.FILE.layout.LocationInfo=true
次に、ログ情報を生成する次のJavaの例を考えてみましょう。
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/*Get actual class name to be printed on*/
static Logger log = Logger.getLogger(log4jExample.class.getName());
public static void main(String[] args)throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}
上記のプログラムをコンパイルして実行します。 次のログ情報を持つ/usr/home/log4jディレクトリにhtmlLayoutlファイルを作成します。
ログセッションの開始時間Mon Mar 22 13:30:24 AST 2010
Time | Thread | Level | Category | File:Line | Message |
---|---|---|---|---|---|
0 | main | DEBUG | log4jExample | log4jExample.java:15 | Hello this is an debug message |
6 | main | INFO | log4jExample | log4jExample.java:16 | Hello this is an info message |
Webブラウザを使用してhtmlLayoutlファイルを開きます。 </html>および</body>タグのフッターが完全に欠落していることに注意することも重要です。
HTML形式のログファイルを持つことの大きな利点の1つは、リモート表示用のWebページとして公開できることです。