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