001 002package gudusoft.gsqlparser.pp.logger; 003 004import java.text.SimpleDateFormat; 005import java.util.Date; 006 007public class PPLogger 008{ 009 010 public static final int OFF = 0; 011 012 public static final int INFO = 300; 013 014 public static final int ERROR = 100; 015 016 public static int currLogLevel = OFF; 017 018 private static PPLogger logger = new PPLogger( ); 019 020 public static void error( Throwable e ) 021 { 022 logger.doLog( new PPLoggerRecord( ERROR, null, e ) ); 023 } 024 025 public static void info( Throwable e ) 026 { 027 info( null, e ); 028 } 029 030 public static void info( String msg ) 031 { 032 info( msg, null ); 033 } 034 035 public static void info( String msg, Throwable e ) 036 { 037 info( 0, msg, null ); 038 } 039 040 public static void info( int stackLevel, String msg, Throwable e ) 041 { 042 logger.doLog( new PPLoggerRecord( INFO, stackLevel, msg, e ) ); 043 } 044 045 private void doLog( PPLoggerRecord record ) 046 { 047 if ( currLogLevel == OFF ) 048 { 049 return; 050 } 051 052 SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ); 053 if ( currLogLevel >= record.getLogLevel( ) ) 054 { 055 StringBuilder buf = new StringBuilder( ); 056 buf.append( fmt.format( new Date( ) ) ); 057 switch ( record.getLogLevel( ) ) 058 { 059 case ERROR : 060 buf.append( " ERROR: " ); 061 break; 062 case INFO : 063 buf.append( " INFO: " ); 064 default : 065 break; 066 } 067 buf.append( record.getClassName( ) ) 068 .append( "." ) 069 .append( record.getMethodName( ) ) 070 .append( "(" ) 071 .append( record.getFileName( ) ) 072 .append( ":" ) 073 .append( record.getLineNum( ) ) 074 .append( ")" ); 075 076 if ( record.getMsg( ) != null ) 077 { 078 buf.append( "\n" ).append( record.getMsg( ) ); 079 } 080 081 System.err.println( buf ); 082 083 if ( record.getE( ) != null ) 084 { 085 record.getE( ).printStackTrace( ); 086 } 087 088 System.err.print( "\n" ); 089 } 090 } 091 092}