Log4j-logging-files

提供:Dev Guides
移動先:案内検索

log4j-ファイルのログイン

ログ情報をファイルに書き込むには、 org.apache.log4j.FileAppender を使用する必要があります。

FileAppenderの構成

FileAppenderには、次の構成可能なパラメーターがあります。

Property Description
immediateFlush This flag is by default set to true, which means the output stream to the file being flushed with each append operation.
encoding It is possible to use any character-encoding. By default, it is the platform-specific encoding scheme.
threshold The threshold level for this appender.
Filename The name of the log file.
fileAppend This is by default set to true, which means the logging information being appended to the end of the same file.
bufferedIO This flag indicates whether we need buffered writing enabled. By default, it is set to false.
bufferSize If buffered I/O is enabled, it indicates the buffer size. By default, it is set to 8kb.

以下は、FileAppenderのサンプル構成ファイル log4j.properties です-

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender

# Set the name of the file
log4j.appender.FILE.File=${log}/log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, overwrite
log4j.appender.FILE.Append=false

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

上記の log4j.properties ファイルと同等のXML構成ファイルが必要な場合は、次の内容があります。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>

<appender name="FILE" class="org.apache.log4j.FileAppender">

   <param name="file" value="${log}/log.out"/>
   <param name="immediateFlush" value="true"/>
   <param name="threshold" value="debug"/>
   <param name="append" value="false"/>

   <layout class="org.apache.log4j.PatternLayout">
      <param name="conversionPattern" value="%m%n"/>
   </layout>
</appender>

<logger name="log4j.rootLogger" additivity="false">
   <level value="DEBUG"/>
   <appender-ref ref="FILE"/>
</logger>

</log4j:configuration>

上記の設定でhttp://www.finddevguides.com/log4j/log4j_sample_program[log4j-Sample Program]を試すことができます。

複数のファイルにログインする

たとえば、ファイルサイズが特定のしきい値に達した場合など、特定の理由でログメッセージを複数のファイルに書き込むことができます。

ログ情報を複数のファイルに書き込むには、 FileAppender クラスを拡張し、すべてのプロパティを継承する org.apache.log4j.RollingFileAppender クラスを使用する必要があります。

FileAppenderについて上記で説明したものに加えて、次の構成可能なパラメーターがあります-

Property Description
maxFileSize This is the critical size of the file above which the file will be rolled. Default value is 10 MB.
maxBackupIndex This property denotes the number of backup files to be created. Default value is 1.

以下は、RollingFileAppenderのサンプル構成ファイル log4j.properties です。

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender

# Set the name of the file
log4j.appender.FILE.File=${log}/log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true

# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=5MB

# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

XML構成ファイルが必要な場合は、最初のセクションで説明したものと同じものを生成し、 RollingFileAppender に関連する追加パラメーターのみを追加できます。

この構成例は、各ログファイルの最大許容サイズが5 MBであることを示しています。 最大サイズを超えると、新しいログファイルが作成されます。 maxBackupIndex は2として定義されているため、2番目のログファイルが最大サイズに達すると、最初のログファイルが消去され、その後すべてのログ情報が最初のログファイルにロールバックされます。

上記の設定でhttp://www.finddevguides.com/log4j/log4j_sample_program[log4j-Sample Program]を試すことができます。

毎日のログファイル生成

ロギング情報のクリーンな記録を保持するために、ログファイルを毎日生成する必要がある場合があります。

ロギング情報を毎日ファイルに書き込むには、 FileAppender クラスを拡張し、そのすべてのプロパティを継承する org.apache.log4j.DailyRollingFileAppender クラスを使用する必要があります。

FileAppenderについて前述したものに加えて、重要な構成可能なパラメーターは1つだけです。

Property Description
DatePattern This indicates when to roll over the file and the naming convention to be followed. By default, roll over is performed at midnight each day.

DatePatternは、次のパターンのいずれかを使用してロールオーバースケジュールを制御します。

DatePattern Description
'.' yyyy-MM Roll over at the end of each month and at the beginning of the next month.
'.' yyyy-MM-dd Roll over at midnight each day. This is the default value.
'.' yyyy-MM-dd-a Roll over at midday and midnight of each day.
'.' yyyy-MM-dd-HH Roll over at the top of every hour.
'.' yyyy-MM-dd-HH-mm Roll over every minute.
'.' yyyy-ww Roll over on the first day of each week depending upon the locale.

以下は、毎日正午と真夜中にロールオーバーするログファイルを生成するサンプル構成ファイル log4j.properties です。

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender

# Set the name of the file
log4j.appender.FILE.File=${log}/log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true

# Set the DatePattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-a

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

XML構成ファイルが必要な場合は、最初のセクションで説明したものと同じものを生成し、 DailyRollingFileAppender に関連する追加パラメーターのみを追加できます。

上記の設定でhttp://www.finddevguides.com/log4j/log4j_sample_program[log4j-Sample Program]を試すことができます。