Log4j-logging-files
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 です-
上記の log4j.properties ファイルと同等のXML構成ファイルが必要な場合は、次の内容があります。
上記の設定で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 です。
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 です。
XML構成ファイルが必要な場合は、最初のセクションで説明したものと同じものを生成し、 DailyRollingFileAppender に関連する追加パラメーターのみを追加できます。
上記の設定でhttp://www.finddevguides.com/log4j/log4j_sample_program[log4j-Sample Program]を試すことができます。