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