View Javadoc

    1   /*
    2    * "Peko" Visual Novel System
    3    *
    4    * All Rights Reserved.
    5    * Copyright (c) 1999-2003 Tsukuba Bunko.
    6    *
    7    * $Id: Logger.java,v 1.2 2005/07/11 14:07:31 ppoi Exp $
    8    */
    9   package tsukuba_bunko.peko;
    10
    11  import	java.io.File;
    12
    13  import	java.util.Properties;
    14
    15  import	javax.swing.JOptionPane;
    16
    17  import	org.apache.log4j.BasicConfigurator;
    18  import	org.apache.log4j.PropertyConfigurator;
    19  import	org.apache.log4j.LogManager;
    20  import	org.apache.log4j.Priority;
    21
    22  import	tsukuba_bunko.util.ResourceLocator;
    23
    24
    25  /***
    26   * "Peko" の各モジュールに対しロギングの機能を提供します。
    27   * @author	$Author: ppoi $
    28   * @version	$Revision: 1.2 $
    29   */
    30  public class Logger	{
    31
    32  	/***
    33  	 * ログ
    34  	 */
    35  	private static	org.apache.log4j.Logger	_logger = null;
    36
    37
    38  	/***
    39  	 * <code>Logger</code> のインスタンスを作成します。
    40  	 */
    41  	public Logger()
    42  	{
    43  		super();
    44  	}
    45
    46
    47  	/***
    48  	 * ログシステムを初期化します。このメソッドは、PekoSystem が初期化時に一度だけ呼び出します。
    49  	 * それ以外のクラス、状態でのメソッド呼び出しは禁止します。
    50  	 */
    51  	public static void prepare()
    52  	{
    53  		LogManager.resetConfiguration();
    54  		try	{
    55  			Properties	props = new Properties();
    56  			props.load( Logger.class.getResourceAsStream("log4j.properties") );
    57
    58  			ResourceLocator	locator = ResourceLocator.newInstance();
    59  			File	base = locator.findLocation( "tsukuba_bunko/peko/PekoSystem.class" );
    60  			if( base.isFile() )	{
    61  				base = base.getParentFile();
    62  			}
    63
    64  			props.setProperty( "log4j.appender.file.File", base.getAbsolutePath() + File.separator + "pvns.log" );
    65  			String	debug = System.getProperty( "pvns.debug", "off" ).toLowerCase();
    66  			if( "on".equals(debug) || "yes".equals(debug) || "true".equals(debug) )	{
    67  				props.setProperty( "log4j.rootLogger", "DEBUG, file" );
    68  			}
    69  			else	{
    70  				props.setProperty( "log4j.rootLogger", "INFO, file" );
    71  			}
    72
    73  			PropertyConfigurator.configure( props );
    74  		}
    75  		catch( Exception e )	{
    76  			BasicConfigurator.configure();
    77  		}
    78  		_logger = LogManager.getLogger( "peko" );
    79  		_logger.info( "[logging] start logging." );
    80  	}
    81
    82
    83  	/***
    84  	 */
    85  	public static final void debug( String message )
    86  	{
    87  		_logger.debug( message );
    88  	}
    89
    90  	/***
    91  	 */
    92  	public static final void debug( String message, Throwable e )
    93  	{
    94  		_logger.debug( message, e );
    95  	}
    96
    97  	/***
    98  	 */
    99  	public static final void info( String message )
    100 	{
    101 		_logger.info( message );
    102 	}
    103
    104 	public static final void info( String message, Throwable e )
    105 	{
    106 		_logger.info( message, e );
    107 	}
    108
    109 	public static final void warn( String message )
    110 	{
    111 		if( _logger.isEnabledFor(Priority.WARN) )	{
    112 			_logger.warn( message );
    113 			JOptionPane.showMessageDialog( null, message, "WARNING", JOptionPane.WARNING_MESSAGE );
    114 		}
    115 	}
    116
    117 	public static final void warn( String message, Throwable e )
    118 	{
    119 		if( _logger.isEnabledFor(Priority.WARN) )	{
    120 			_logger.warn( message, e );
    121 			JOptionPane.showMessageDialog( null, message, "WARNING", JOptionPane.WARNING_MESSAGE );
    122 		}
    123 	}
    124
    125 	public static final void warn( MessageID messageID )
    126 	{
    127 		String	message = messageID.getMessage();
    128 		if( _logger.isEnabledFor(Priority.WARN) )	{
    129 			_logger.warn( message );
    130 			JOptionPane.showMessageDialog( null, message, "WARNING", JOptionPane.WARNING_MESSAGE );
    131 		}
    132 	}
    133
    134 	public static final void warn( MessageID messageID, Object[] params )
    135 	{
    136 		String	message = messageID.getMessage( params );
    137 		if( _logger.isEnabledFor(Priority.WARN) )	{
    138 			_logger.warn( message );
    139 			JOptionPane.showMessageDialog( null, message, "WARNING", JOptionPane.WARNING_MESSAGE );
    140 		}
    141 	}
    142
    143 	public static final void warn( MessageID messageID, Throwable e )
    144 	{
    145 		String	message = messageID.getMessage();
    146 		if( _logger.isEnabledFor(Priority.WARN) )	{
    147 			_logger.warn( message, e );
    148 			JOptionPane.showMessageDialog( null, message, "WARNING", JOptionPane.WARNING_MESSAGE );
    149 		}
    150 	}
    151
    152 	public static final void warn( MessageID messageID, Object[] params, Throwable e )
    153 	{
    154 		String	message = messageID.getMessage( params );
    155 		if( _logger.isEnabledFor(Priority.WARN) )	{
    156 			_logger.warn( message, e );
    157 			JOptionPane.showMessageDialog( null, message, "WARNING", JOptionPane.WARNING_MESSAGE );
    158 		}
    159 	}
    160
    161 	public static final void error( String message )
    162 	{
    163 		if( _logger.isEnabledFor(Priority.ERROR) )	{
    164 			_logger.error( message );
    165 			JOptionPane.showMessageDialog( null, message, "ERROR", JOptionPane.ERROR_MESSAGE );
    166 		}
    167 	}
    168
    169 	public static final void error( String message, Throwable e )
    170 	{
    171 		if( _logger.isEnabledFor(Priority.ERROR) )	{
    172 			_logger.error( message, e );
    173 			JOptionPane.showMessageDialog( null, message, "ERROR", JOptionPane.ERROR_MESSAGE );
    174 		}
    175 	}
    176
    177 	public static final void error( MessageID messageID )
    178 	{
    179 		String	message = messageID.getMessage();
    180 		if( _logger.isEnabledFor(Priority.ERROR) )	{
    181 			_logger.error( message );
    182 			JOptionPane.showMessageDialog( null, message, "ERROR", JOptionPane.ERROR_MESSAGE );
    183 		}
    184 	}
    185
    186 	public static final void error( MessageID messageID, Object[] params )
    187 	{
    188 		String	message = messageID.getMessage( params );
    189 		if( _logger.isEnabledFor(Priority.ERROR) )	{
    190 			_logger.error( message );
    191 			JOptionPane.showMessageDialog( null, message, "ERROR", JOptionPane.ERROR_MESSAGE );
    192 		}
    193 	}
    194
    195 	public static final void error( MessageID messageID, Throwable e )
    196 	{
    197 		String	message = messageID.getMessage();
    198 		if( _logger.isEnabledFor(Priority.ERROR) )	{
    199 			_logger.error( message, e );
    200 			JOptionPane.showMessageDialog( null, message, "ERROR", JOptionPane.ERROR_MESSAGE );
    201 		}
    202 	}
    203
    204 	public static final void error( MessageID messageID, Object[] params, Throwable e )
    205 	{
    206 		String	message = messageID.getMessage( params );
    207 		if( _logger.isEnabledFor(Priority.ERROR) )	{
    208 			_logger.error( message, e );
    209 			JOptionPane.showMessageDialog( null, message, "ERROR", JOptionPane.ERROR_MESSAGE );
    210 		}
    211 	}
    212
    213 	public static final void fatal( String message )
    214 	{
    215 		_logger.fatal( message );
    216 	}
    217
    218 	public static final void fatal( String message, Throwable e )
    219 	{
    220 		_logger.fatal( message, e );
    221 	}
    222
    223 	public static final void fatal( MessageID messageID )
    224 	{
    225 		_logger.fatal( messageID.getMessage() );
    226 	}
    227
    228 	public static final void fatal( MessageID messageID, Object[] params )
    229 	{
    230 		_logger.fatal( messageID.getMessage(params) );
    231 	}
    232
    233 	public static final void fatal( MessageID messageID, Throwable e )
    234 	{
    235 		_logger.fatal( messageID.getMessage(), e );
    236 	}
    237
    238 	public static final void fatal( MessageID messageID, Object[] params, Throwable e )
    239 	{
    240 		_logger.fatal( messageID.getMessage(params), e );
    241 	}
    242 }