001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.fukurou.system; // 6.4.2.0 (2016/01/29) package変更 fukurou.util → fukurou.system 017 018import java.io.BufferedWriter; 019import java.io.File; 020import java.io.FileWriter; 021import java.io.IOException; 022import java.io.PrintWriter; 023 024import java.io.OutputStreamWriter; // 7.1.0.1 (2020/02/07) 025import java.io.FileOutputStream; // 7.1.0.1 (2020/02/07) 026 027 028/** 029 * Logを書き込む為の PrintWriter を管理するクラスです。 030 * 031 * 実際の Log の書き込みには, LogSender を利用して下さい。 032 * 033 * @og.rev 6.4.2.0 (2016/01/29) package変更 fukurou.util → fukurou.system 034 * 035 * @og.group エラー処理 036 * 037 * @version 4.0 038 * @author Kazuhiko Hasegawa 039 * @since JDK5.0, 040 */ 041public final class LogWriter { 042 private static PrintWriter writer ; 043 private static String encode ; // 7.1.0.1 (2020/02/07) "UTF-8" など 044 private static final Object LOCK = new Object(); // 6.3.9.0 (2015/11/06) synchronizedブロック 045 046 private static String logFileUrl ; // 4.1.0.1 (2008/01/23) 047 048 /** 049 * デフォルトコンストラクター 050 * private にして,コンストラクターの作成をさせない様にしています。 051 * 052 */ 053 private LogWriter() {} 054 055 /** 056 * Logファイルの出力先を設定します。 057 * 058 * このファイル名は、日付フォーマット変数を含むことができます。 059 * 060 * @og.rev 4.1.0.1 (2008/01/23) 新規作成 061 * @og.rev 6.3.9.0 (2015/11/06) Use block level rather than method level synchronization.(PMD) 062 * @og.rev 6.4.2.0 (2016/01/29) 出力先変更時は、既存の Writer をclose() してからにする。 063 * @og.rev 7.1.0.1 (2020/02/07) ログファイルのエンコードを指定します。 064 * 065 * @param url 出力先 066 * @param enc エンコード 067 * @see org.opengion.fukurou.system.DateSet#changeString(String) 068 */ 069// public static void init( final String url ) { 070 public static void init( final String url,final String enc ) { 071 synchronized( LOCK ) { 072 close(); 073 logFileUrl = url; 074 encode = enc; // 7.1.0.1 (2020/02/07) 075 } 076 } 077 078 /** 079 * Logを書き出します。 080 * 081 * @og.rev 4.1.0.1 (2008/01/23) 出力時間を出力する。 082 * @og.rev 5.5.7.2 (2012/10/09) HybsDateUtil を利用するように修正します。 083 * @og.rev 6.3.9.0 (2015/11/06) Use block level rather than method level synchronization.(PMD) 084 * 085 * @param message メッセージ 086 */ 087 public static void log( final String message ) { 088 synchronized( LOCK ) { 089 if( writer == null ) { writer = getPrintWriter(); } 090 } 091 writer.println( "[WriteTime= " + DateSet.getDate( "yyyy/MM/dd HH:mm:ss.SSS" ) + "] " + message ); // 5.5.7.2 (2012/10/09) HybsDateUtil を利用 092 writer.flush(); 093 } 094 095 /** 096 * 例外のスタックトレースをLogWriterのPrintWriterに書き出します。 097 * 098 * @og.rev 4.1.0.1 (2008/01/23) 新規作成 099 * @og.rev 4.3.4.5 (2009/01/08) nullチェック追加 100 * @og.rev 6.3.9.0 (2015/11/06) Use block level rather than method level synchronization.(PMD) 101 * @og.rev 6.4.2.0 (2016/01/29) ex.printStackTrace() を、ThrowUtil#ogStackTrace(Throwable) に置き換え。 102 * 103 * @param th スタックトレースの取得元Throwableオブジェクト 104 */ 105 public static void log( final Throwable th ) { 106 synchronized( LOCK ) { 107 if( writer == null ) { writer = getPrintWriter(); } 108 } 109 writer.println( ThrowUtil.ogStackTrace( th ) ); // 6.4.2.0 (2016/01/29) 110 } 111 112 /** 113 * PrintWriter を close() します。 114 * 115 * @og.rev 6.3.9.0 (2015/11/06) Use block level rather than method level synchronization.(PMD) 116 */ 117 public static void close() { 118 synchronized( LOCK ) { 119 if( writer != null ) { writer.close(); } 120 writer = null; 121 } 122 } 123 124 /** 125 * 追加モードで作成した,PrintWriter を取得します。 126 * PrintWriter は,シングルトーンとして,唯一存在させています。 127 * 128 * @og.rev 4.1.0.1 (2008/01/23) ログファイル出力先を外部から指定する。 129 * @og.rev 6.3.9.0 (2015/11/06) Use block level rather than method level synchronization.(PMD) 130 * @og.rev 7.1.0.1 (2020/02/07) ログファイルのエンコードを指定します。 131 * 132 * @return 追加モードで作成したPrintWriter 133 * @og.rtnNotNull 134 */ 135 private static PrintWriter getPrintWriter() { 136 if( logFileUrl == null || logFileUrl.isEmpty() ) { 137 return new PrintWriter( System.err ); 138 } 139 else { 140 // 日付フォームのファイル名を変換します。 141 final DateSet dateSet = new DateSet(); 142 final String timeFileUrl = dateSet.changeString( logFileUrl ); // 6.3.9.0 (2015/11/06) 日付フォーマットは、作成都度行う事とする。 143 144 try { 145 final File logFile = new File( timeFileUrl ); 146 // 7.1.0.1 (2020/02/07) ログファイルのエンコードを指定します。 147 // return new PrintWriter( new BufferedWriter( new FileWriter( logFile, true ) ) ); 148 if( encode == null || encode.isEmpty() ) { 149 return new PrintWriter( new BufferedWriter( new FileWriter( logFile, true ) ) ); 150 } 151 else { 152 return new PrintWriter( new BufferedWriter( new OutputStreamWriter( new FileOutputStream( logFile, true ), encode ) ) ); // 7.1.0.1 (2020/02/07) 153 } 154 } 155 catch( final IOException ex ) { 156 final String errMsg = "ログライターが作成できません。[" + timeFileUrl + "]"; 157 throw new OgRuntimeException( errMsg, ex ); 158 } 159 } 160 } 161}