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.util; 017 018import java.io.BufferedWriter; 019import java.io.File; 020import java.io.FileWriter; 021import java.io.IOException; 022import java.io.PrintWriter; 023 024/** 025 * Logを書き込む為の PrintWriter を管理するクラスです。 026 * 027 * 実際の Log の書き込みには, LogSender を利用して下さい。 028 * 029 * @og.group エラー処理 030 * 031 * @version 4.0 032 * @author Kazuhiko Hasegawa 033 * @since JDK5.0, 034 */ 035public final class LogWriter { 036 private static PrintWriter writer = null; 037 038 private static String logFileUrl = null; // 4.1.0.1 (2008/01/23) 039 040 /** 041 * デフォルトコンストラクター 042 * private にして,コンストラクターの作成をさせない様にしています。 043 * 044 */ 045 private LogWriter() {} 046 047 /** 048 * Logファイルの出力先を設定します。 049 * 050 * @og.rev 4.1.0.1 (2008/01/23) 新規作成 051 * 052 * @param url 出力先 053 */ 054 public static synchronized void init( final String url ) { 055 logFileUrl = url; 056 } 057 058 /** 059 * Logを書き出します。 060 * 061 * @og.rev 4.1.0.1 (2008/01/23) 出力時間を出力する。 062 * @og.rev 5.5.7.2 (2012/10/09) HybsDateUtil を利用するように修正します。 063 * 064 * @param message メッセージ 065 */ 066 public static synchronized void log( final String message ) { 067 if( writer == null ) { 068 writer = getPrintWriter(); 069 } 070 writer.println( "[WriteTime= " + HybsDateUtil.getDate( "yyyy/MM/dd HH:mm:ss.SSS" ) + "] " + message ); // 5.5.7.2 (2012/10/09) HybsDateUtil を利用 071 writer.flush(); 072 } 073 074 /** 075 * 例外のスタックトレースをLogWriterのPrintWriterに書き出します。 076 * 077 * @og.rev 4.1.0.1 (2008/01/23) 新規作成 078 * @og.rev 4.3.4.5 (2009/01/08) nullチェック追加 079 * 080 * @param th スタックトレースの取得元Throwableオブジェクト 081 */ 082 public static synchronized void log( final Throwable th ) { 083 if( writer == null ) { 084 writer = getPrintWriter(); 085 } 086 th.printStackTrace( writer ); 087 } 088 089 /** 090 * PrintWriter を close() します。 091 * 092 */ 093 public static synchronized void close() { 094 if( writer != null ) { writer.close(); } 095 writer = null; 096 } 097 098 /** 099 * 追加モードで作成した,PrintWriter を取得します。 100 * PrintWriter は,シングルトーンとして,唯一存在させています。 101 * 102 * @og.rev 4.1.0.1 (2008/01/23) ログファイル出力先を外部から指定する。 103 * 104 * @return 追加モードで作成したPrintWriter 105 */ 106 private static synchronized PrintWriter getPrintWriter() { 107 108 if( logFileUrl == null || logFileUrl.length() == 0 ) { 109 return new PrintWriter( System.err ); 110 } 111 else { 112 // 日付フォームのファイル名を変換します。 113 DateSet dateSet = new DateSet(); 114 logFileUrl = dateSet.changeString(logFileUrl ); 115 116 try { 117 File logFile = new File( logFileUrl ); 118 return new PrintWriter( new BufferedWriter( new FileWriter( logFile, true ) ) ); 119 } 120 catch( IOException ex ) { 121 String errMsg = "ログライターが作成できません。[" + logFileUrl + "]"; 122 throw new RuntimeException( errMsg, ex ); 123 } 124 } 125 } 126}