001/*
002 * Copyright (c) 2017 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.fileexec;
017
018//      import java.nio.file.Paths;                                                             // 6.8.1.5 (2017/09/08)
019
020import java.util.logging.Logger;
021import java.util.logging.Level;
022import java.util.function.Supplier;                                             // 6.8.1.0 (2017/07/14)
023
024/**
025 * XLoggerは、Throwable を引数に取るwarningと、Level 600 の debug メソッドを
026 * 持つ、Logger の拡張クラスです。
027 * ここでは、継承するのではなく、委譲で、最低限のメソッドだけに対応しています。
028 * (LogManager とか、色々とややこしそうなので、調査する暇が無い)
029 *
030 * WARNING(900) → INFO(800) → CONFIG(700) → XXXX(600) → FINE(500) → ALL(Integer.MIN_VALUE)
031 * となっていますが、FINE では、多すぎ、INFO は、通常使用、間に、DEBUG的な、
032 * ロギングが欲しい場合に使用します。
033 * CONFIG を使いたいところですが、「CONFIGは静的な構成メッセージのメッセージ・レベルです。」と
034 * JavaDocにわざわざ、書かれていることから、使用を避けます。
035 *
036 * @og.rev 7.0.0.0 (2017/07/07) 新規作成
037 *
038 * @version  7.0
039 * @author   Kazuhiko Hasegawa
040 * @since    JDK1.8,
041 */
042public class XLogger {
043        /** 初期設定されているリソースバンドルのbaseName {@value} */
044        public static final String OMIT_NAME = "org.opengion.fukurou." ;                // fileexec は残す
045
046        /**
047         * デバッグレベルを新規に定義します。
048         *
049         * OFF:Integer.MAX_VALUE , SEVERE:1000 , WARNING:900 , INFO:800 , CONFIG:700 , FINE:500 , FINER:400 , FINEST:300 , ALL:Integer.MIN_VALUE
050         * LEVEL_DEBUG:600 で定義することで、INFO より細かいが、FINE より荒いログ出力が可能です。
051         * 他のアプリケーションで、INFO は許せても、FINE は許せない場合があるので、独自のログ出力が、可能です。
052         */
053        private static final class LEVEL_DEBUG extends Level {
054                private static final long serialVersionUID = 681020170714L ;            // 6.8.1.0 (2017/07/14)
055                /**
056                 * デバッグレベルのコンストラクター
057                 */
058                public LEVEL_DEBUG() { super( "DEBUG",600 ); }
059        };
060
061//      private static final Level DEBUG = new LEVEL_DEBUG();
062        public static final Level L_DEBUG = new LEVEL_DEBUG();                                  // 6.9.7.0 (2018/05/14) PMD Field DEBUG has the same name as a method
063
064        private final Logger LOGGER;
065
066        /**
067         * 名前を指定して、XLoggerオブジェクトを作成します。
068         *
069         * @og.rev 6.8.1.5 (2017/09/08) logフォルダの存在チェックと作成
070         *
071         * @param       name     ロガーの名前。通常は、クラス.class.getName() を渡せばよいです。
072         */
073        private XLogger( final String name ) {
074//              FileUtil.mkdirs( Paths.get( "log" ) );          // Logger の log フォルダが無ければ作成します。
075
076                LOGGER = Logger.getLogger( name );
077        }
078
079        /**
080         * 名前を指定して、XLoggerオブジェクトを作成します。
081         *
082         * @og.rev 7.2.1.0 (2020/03/13) ロガーの名前から、共通部分を削除します。
083         *
084         * @param       name     ロガーの名前。通常は、クラス.class.getName() を渡せばよいです。
085         * @return      XLoggerオブジェクト
086         */
087        public static XLogger getLogger( final String name ) {
088                String key = name ;
089                if( key.startsWith( OMIT_NAME ) ) {
090                        key = name.substring( OMIT_NAME.length() );
091                }
092
093//              return new XLogger( name );                             // 今は、個別に作成していますが、本来は、同じオブジェクトを返すようにすべき。
094                return new XLogger( key );                              // 今は、個別に作成していますが、本来は、同じオブジェクトを返すようにすべき。
095        }
096
097        /**
098         * INFO レベルのログをとります。
099         *
100         * @param       msgSupplier     呼び出されると、目的のログ・メッセージを生成する関数
101         * @see         Logger#info( Supplier )
102         */
103        public void info( final Supplier<String> msgSupplier ) {
104                LOGGER.info( msgSupplier );
105        }
106
107        /**
108         * WARNING レベルのログをとります。
109         *
110         * @param       msgSupplier     呼び出されると、目的のログ・メッセージを生成する関数
111         * @see         Logger#warning( Supplier )
112         */
113        public void warning( final Supplier<String> msgSupplier ) {
114                LOGGER.warning( msgSupplier );
115        }
116
117        /**
118         * WARNING レベルのログをとります。
119         *
120         * これは、Throwable を引数に取る拡張されたメソッドです。
121         *
122         * @param       thrown  ログ・メッセージに関連したThrowable。
123         * @param       msgSupplier     呼び出されると、目的のログ・メッセージを生成する関数
124         * @see         Logger#log( Level,Throwable,Supplier )
125         */
126        public void warning( final Throwable thrown , final Supplier<String> msgSupplier ) {
127                LOGGER.log( Level.WARNING , thrown , msgSupplier );
128        }
129
130        /**
131         * 600 レベルのログをとります。
132         *
133         * Supplierを引数に、Level = 600 のログをとります。
134         *
135         * @param       msgSupplier     呼び出されると、目的のログ・メッセージを生成する関数
136         * @see         Logger#log( Level,Supplier )
137         */
138        public void debug( final Supplier<String> msgSupplier ) {
139//              LOGGER.log( DEBUG , msgSupplier );
140                LOGGER.log( L_DEBUG , msgSupplier );
141        }
142}