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 018import 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 /** 044 * デバッグレベルを新規に定義します。 045 * 046 * OFF:Integer.MAX_VALUE , SEVERE:1000 , WARNING:900 , INFO:800 , CONFIG:700 , FINE:500 , FINER:400 , FINEST:300 , ALL:Integer.MIN_VALUE 047 * LEVEL_DEBUG:600 で定義することで、INFO より細かいが、FINE より荒いログ出力が可能です。 048 * 他のアプリケーションで、INFO は許せても、FINE は許せない場合があるので、独自のログ出力が、可能です。 049 */ 050 private static final class LEVEL_DEBUG extends Level { 051 private static final long serialVersionUID = 681020170714L ; // 6.8.1.0 (2017/07/14) 052 /** 053 * デバッグレベルのコンストラクター 054 */ 055 public LEVEL_DEBUG() { super( "600",600 ); } 056 }; 057 058// private static final Level DEBUG = new LEVEL_DEBUG(); 059 private 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 060 061 private final Logger LOGGER; 062 063 /** 064 * 名前を指定して、XLoggerオブジェクトを作成します。 065 * 066 * @og.rev 6.8.1.5 (2017/09/08) logフォルダの存在チェックと作成 067 * 068 * @param name ロガーの名前。通常は、クラス.class.getName() を渡せばよいです。 069 */ 070 private XLogger( final String name ) { 071 FileUtil.mkdirs( Paths.get( "log" ) ); // Logger の log フォルダが無ければ作成します。 072 073 LOGGER = Logger.getLogger( name ); 074 } 075 076 /** 077 * 名前を指定して、XLoggerオブジェクトを作成します。 078 * 079 * @param name ロガーの名前。通常は、クラス.class.getName() を渡せばよいです。 080 * @return XLoggerオブジェクト 081 */ 082 public static XLogger getLogger( final String name ) { 083 return new XLogger( name ); // 今は、個別に作成していますが、本来は、同じオブジェクトを返すようにすべき。 084 } 085 086 /** 087 * INFO レベルのログをとります。 088 * 089 * @param msgSupplier 呼び出されると、目的のログ・メッセージを生成する関数 090 * @see Logger#info( Supplier ) 091 */ 092 public void info( final Supplier<String> msgSupplier ) { 093 LOGGER.info( msgSupplier ); 094 } 095 096 /** 097 * WARNING レベルのログをとります。 098 * 099 * @param msgSupplier 呼び出されると、目的のログ・メッセージを生成する関数 100 * @see Logger#warning( Supplier ) 101 */ 102 public void warning( final Supplier<String> msgSupplier ) { 103 LOGGER.warning( msgSupplier ); 104 } 105 106 /** 107 * WARNING レベルのログをとります。 108 * 109 * これは、Throwable を引数に取る拡張されたメソッドです。 110 * 111 * @param thrown ログ・メッセージに関連したThrowable。 112 * @param msgSupplier 呼び出されると、目的のログ・メッセージを生成する関数 113 * @see Logger#log( Level,Throwable,Supplier ) 114 */ 115 public void warning( final Throwable thrown , final Supplier<String> msgSupplier ) { 116 LOGGER.log( Level.WARNING , thrown , msgSupplier ); 117 } 118 119 /** 120 * 600 レベルのログをとります。 121 * 122 * Supplierを引数に、Level = 600 のログをとります。 123 * 124 * @param msgSupplier 呼び出されると、目的のログ・メッセージを生成する関数 125 * @see Logger#log( Level,Supplier ) 126 */ 127 public void debug( final Supplier<String> msgSupplier ) { 128// LOGGER.log( DEBUG , msgSupplier ); 129 LOGGER.log( L_DEBUG , msgSupplier ); 130 } 131}