2005-06-09 11:25:08 +02:00
// serverLog.java
// -------------------------------------
2008-07-20 19:14:51 +02:00
// (C) by Michael Peter Christen; mc@yacy.net
2005-06-09 11:25:08 +02:00
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
2005-09-05 10:01:54 +02:00
//last major change: $LastChangedDate$ by $LastChangedBy$
//Revision: $LastChangedRevision$
2005-06-09 11:25:08 +02:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.server.logging ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.util.logging.Level ;
import java.util.logging.LogManager ;
import java.util.logging.Logger ;
public final class serverLog {
2005-08-31 00:24:38 +02:00
// log-level categories
2005-06-09 11:25:08 +02:00
public static final int LOGLEVEL_ZERO = Level . OFF . intValue ( ) ; // no output at all
2005-08-31 00:24:38 +02:00
public static final int LOGLEVEL_SEVERE = Level . SEVERE . intValue ( ) ; // system-level error, internal cause, critical and not fixeable (i.e. inconsistency)
2005-06-09 11:25:08 +02:00
public static final int LOGLEVEL_WARNING = Level . WARNING . intValue ( ) ; // uncritical service failure, may require user activity (i.e. input required, wrong authorization)
2005-08-30 23:10:39 +02:00
public static final int LOGLEVEL_CONFIG = Level . CONFIG . intValue ( ) ; // regular system status information (i.e. start-up messages)
2005-06-09 11:25:08 +02:00
public static final int LOGLEVEL_INFO = Level . INFO . intValue ( ) ; // regular action information (i.e. any httpd request URL)
2005-08-31 00:24:38 +02:00
public static final int LOGLEVEL_FINE = Level . FINE . intValue ( ) ; // in-function status debug output
2005-08-31 00:37:48 +02:00
public static final int LOGLEVEL_FINER = Level . FINER . intValue ( ) ; // in-function status debug output
public static final int LOGLEVEL_FINEST = Level . FINEST . intValue ( ) ; // in-function status debug output
2005-08-31 00:24:38 +02:00
// these categories are also present as character tokens
2005-06-09 11:25:08 +02:00
public static final char LOGTOKEN_ZERO = 'Z' ;
2005-08-31 00:24:38 +02:00
public static final char LOGTOKEN_SEVERE = 'E' ;
2005-06-09 11:25:08 +02:00
public static final char LOGTOKEN_WARNING = 'W' ;
2005-08-30 23:10:39 +02:00
public static final char LOGTOKEN_CONFIG = 'S' ;
2005-06-09 11:25:08 +02:00
public static final char LOGTOKEN_INFO = 'I' ;
2005-08-30 23:10:39 +02:00
public static final char LOGTOKEN_FINE = 'D' ;
2005-08-31 00:37:48 +02:00
public static final char LOGTOKEN_FINER = 'D' ;
public static final char LOGTOKEN_FINEST = 'D' ;
2005-06-09 11:25:08 +02:00
private final Logger theLogger ;
2005-08-31 00:24:38 +02:00
2008-08-02 14:12:04 +02:00
public serverLog ( final String appName ) {
2005-06-15 03:22:07 +02:00
this . theLogger = Logger . getLogger ( appName ) ;
2005-06-16 20:31:28 +02:00
//this.theLogger.setLevel(Level.FINEST); // set a default level
2005-06-09 11:25:08 +02:00
}
2005-08-31 00:24:38 +02:00
2008-08-02 14:12:04 +02:00
public void setLevel ( final Level newLevel ) {
2005-06-10 11:19:24 +02:00
this . theLogger . setLevel ( newLevel ) ;
2005-06-09 11:25:08 +02:00
}
2005-11-29 08:27:58 +01:00
2008-08-02 14:12:04 +02:00
public void logSevere ( final String message ) { this . theLogger . severe ( message ) ; }
public void logSevere ( final String message , final Throwable thrown ) { this . theLogger . log ( Level . SEVERE , message , thrown ) ; }
2005-11-29 08:27:58 +01:00
public boolean isSevere ( ) { return this . theLogger . isLoggable ( Level . SEVERE ) ; }
2005-08-31 00:24:38 +02:00
2008-08-02 14:12:04 +02:00
public void logWarning ( final String message ) { this . theLogger . warning ( message ) ; }
public void logWarning ( final String message , final Throwable thrown ) { this . theLogger . log ( Level . WARNING , message , thrown ) ; }
2005-11-29 08:27:58 +01:00
public boolean isWarning ( ) { return this . theLogger . isLoggable ( Level . WARNING ) ; }
2008-08-02 14:12:04 +02:00
public void logConfig ( final String message ) { this . theLogger . config ( message ) ; }
public void logConfig ( final String message , final Throwable thrown ) { this . theLogger . log ( Level . CONFIG , message , thrown ) ; }
2005-11-29 08:27:58 +01:00
public boolean isConfig ( ) { return this . theLogger . isLoggable ( Level . CONFIG ) ; }
2005-08-31 00:24:38 +02:00
2008-08-02 14:12:04 +02:00
public void logInfo ( final String message ) { this . theLogger . info ( message ) ; }
public void logInfo ( final String message , final Throwable thrown ) { this . theLogger . log ( Level . INFO , message , thrown ) ; }
2005-11-29 08:27:58 +01:00
public boolean isInfo ( ) { return this . theLogger . isLoggable ( Level . INFO ) ; }
2005-08-31 00:24:38 +02:00
2008-08-02 14:12:04 +02:00
public void logFine ( final String message ) { this . theLogger . fine ( message ) ; }
public void logFine ( final String message , final Throwable thrown ) { this . theLogger . log ( Level . FINE , message , thrown ) ; }
2005-11-29 08:27:58 +01:00
public boolean isFine ( ) { return this . theLogger . isLoggable ( Level . FINE ) ; }
2005-08-31 00:24:38 +02:00
2008-08-02 14:12:04 +02:00
public void logFiner ( final String message ) { this . theLogger . finer ( message ) ; }
public void logFiner ( final String message , final Throwable thrown ) { this . theLogger . log ( Level . FINER , message , thrown ) ; }
2005-11-29 08:27:58 +01:00
public boolean isFiner ( ) { return this . theLogger . isLoggable ( Level . FINER ) ; }
2005-08-31 00:37:48 +02:00
2008-08-02 14:12:04 +02:00
public void logFinest ( final String message ) { this . theLogger . finest ( message ) ; }
public void logFinest ( final String message , final Throwable thrown ) { this . theLogger . log ( Level . FINEST , message , thrown ) ; }
2005-11-29 08:27:58 +01:00
public boolean isFinest ( ) { return this . theLogger . isLoggable ( Level . FINEST ) ; }
2008-08-02 14:12:04 +02:00
public boolean isLoggable ( final Level level ) {
2005-09-09 13:03:06 +02:00
return this . theLogger . isLoggable ( level ) ;
}
2005-11-29 08:27:58 +01:00
2005-06-09 11:25:08 +02:00
// static log messages: log everything
2008-08-02 14:12:04 +02:00
public static void logSevere ( final String appName , final String message ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . severe ( message ) ;
}
2008-08-02 14:12:04 +02:00
public static void logSevere ( final String appName , final String message , final Throwable thrown ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . log ( Level . SEVERE , message , thrown ) ;
}
2008-09-03 01:49:48 +02:00
public static boolean isSevere ( final String appName ) {
return Logger . getLogger ( appName ) . isLoggable ( Level . SEVERE ) ;
2005-11-29 08:27:58 +01:00
}
2008-08-02 14:12:04 +02:00
public static void logWarning ( final String appName , final String message ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . warning ( message ) ;
}
2008-08-02 14:12:04 +02:00
public static void logWarning ( final String appName , final String message , final Throwable thrown ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . log ( Level . WARNING , message , thrown ) ;
}
2008-09-03 01:49:48 +02:00
public static boolean isWarning ( final String appName ) {
return Logger . getLogger ( appName ) . isLoggable ( Level . WARNING ) ;
2005-11-29 08:27:58 +01:00
}
2008-08-02 14:12:04 +02:00
public static void logConfig ( final String appName , final String message ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . config ( message ) ;
}
2008-08-02 14:12:04 +02:00
public static void logConfig ( final String appName , final String message , final Throwable thrown ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . log ( Level . CONFIG , message , thrown ) ;
}
2008-09-03 01:49:48 +02:00
public static boolean isConfig ( final String appName ) {
return Logger . getLogger ( appName ) . isLoggable ( Level . CONFIG ) ;
2005-11-29 08:27:58 +01:00
}
2008-08-02 14:12:04 +02:00
public static void logInfo ( final String appName , final String message ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . info ( message ) ;
}
2008-08-02 14:12:04 +02:00
public static void logInfo ( final String appName , final String message , final Throwable thrown ) {
2005-06-10 11:19:24 +02:00
Logger . getLogger ( appName ) . log ( Level . INFO , message , thrown ) ;
}
2008-09-03 01:49:48 +02:00
public static boolean isInfo ( final String appName ) {
return Logger . getLogger ( appName ) . isLoggable ( Level . INFO ) ;
2005-11-29 08:27:58 +01:00
}
2008-08-02 14:12:04 +02:00
public static void logFine ( final String appName , final String message ) {
2005-08-31 00:37:48 +02:00
Logger . getLogger ( appName ) . fine ( message ) ;
2005-06-10 11:19:24 +02:00
}
2008-08-02 14:12:04 +02:00
public static void logFine ( final String appName , final String message , final Throwable thrown ) {
2005-11-07 11:57:54 +01:00
Logger . getLogger ( appName ) . log ( Level . FINE , message , thrown ) ;
2005-08-31 00:37:48 +02:00
}
2008-09-03 01:49:48 +02:00
public static boolean isFine ( final String appName ) {
return Logger . getLogger ( appName ) . isLoggable ( Level . FINE ) ;
2005-11-29 08:27:58 +01:00
}
2005-08-31 00:37:48 +02:00
2008-08-02 14:12:04 +02:00
public static void logFiner ( final String appName , final String message ) {
2005-08-31 00:37:48 +02:00
Logger . getLogger ( appName ) . finer ( message ) ;
}
2008-08-02 14:12:04 +02:00
public static void logFiner ( final String appName , final String message , final Throwable thrown ) {
2005-08-31 00:37:48 +02:00
Logger . getLogger ( appName ) . log ( Level . FINER , message , thrown ) ;
2005-08-31 00:24:38 +02:00
}
2008-09-03 01:49:48 +02:00
public static boolean isFiner ( final String appName ) {
return Logger . getLogger ( appName ) . isLoggable ( Level . FINER ) ;
2005-11-29 08:27:58 +01:00
}
2005-08-31 00:37:48 +02:00
2008-08-02 14:12:04 +02:00
public static void logFinest ( final String appName , final String message ) {
2005-08-31 00:37:48 +02:00
Logger . getLogger ( appName ) . finest ( message ) ;
}
2008-08-02 14:12:04 +02:00
public static void logFinest ( final String appName , final String message , final Throwable thrown ) {
2005-08-31 00:37:48 +02:00
Logger . getLogger ( appName ) . log ( Level . FINEST , message , thrown ) ;
}
2008-09-03 01:49:48 +02:00
public static boolean isFinest ( final String appName ) {
return Logger . getLogger ( appName ) . isLoggable ( Level . FINEST ) ;
2005-11-29 08:27:58 +01:00
}
2005-08-31 00:37:48 +02:00
2008-08-02 14:12:04 +02:00
public static final void configureLogging ( final File homePath , final File loggingConfigFile ) throws SecurityException , FileNotFoundException , IOException {
2005-06-13 15:49:17 +02:00
FileInputStream fileIn = null ;
try {
2005-08-31 00:24:38 +02:00
System . out . println ( " STARTUP: Trying to load logging configuration from file " + loggingConfigFile . toString ( ) ) ;
2005-06-13 15:49:17 +02:00
fileIn = new FileInputStream ( loggingConfigFile ) ;
2005-08-31 00:24:38 +02:00
2005-06-13 15:49:17 +02:00
// loading the logger configuration from file
2008-08-02 14:12:04 +02:00
final LogManager logManager = LogManager . getLogManager ( ) ;
2005-06-13 15:49:17 +02:00
logManager . readConfiguration ( fileIn ) ;
2005-08-31 00:24:38 +02:00
2005-06-13 15:49:17 +02:00
// creating the logging directory
2008-08-02 14:12:04 +02:00
final String logPattern = logManager . getProperty ( " java.util.logging.FileHandler.pattern " ) ;
2007-11-04 11:36:25 +01:00
int stripPos = logPattern . lastIndexOf ( '/' ) ;
if ( stripPos < 0 ) stripPos = logPattern . lastIndexOf ( File . pathSeparatorChar ) ;
File log = new File ( logPattern . substring ( 0 , stripPos ) ) ;
if ( ! log . isAbsolute ( ) ) log = new File ( homePath , log . getPath ( ) ) ;
if ( ! log . canRead ( ) ) log . mkdir ( ) ;
2005-08-31 00:24:38 +02:00
2007-05-18 15:04:26 +02:00
// TODO: changing the pattern settings for the file handlers
2005-06-13 15:49:17 +02:00
// generating the root logger
2005-12-05 01:17:12 +01:00
/*Logger logger =*/ Logger . getLogger ( " " ) ;
2005-08-31 00:24:38 +02:00
2005-06-13 15:49:17 +02:00
// System.setOut(new PrintStream(new LoggerOutputStream(Logger.getLogger("STDOUT"),Level.FINEST)));
// System.setErr(new PrintStream(new LoggerOutputStream(Logger.getLogger("STDERR"),Level.SEVERE)));
} finally {
2008-08-02 14:12:04 +02:00
if ( fileIn ! = null ) try { fileIn . close ( ) ; } catch ( final Exception e ) { }
2005-06-13 15:49:17 +02:00
}
2005-06-09 11:25:08 +02:00
}
2005-10-23 19:50:27 +02:00
2008-08-02 14:12:04 +02:00
public static final String format ( final String s , int n , final int fillChar ) {
final int l = s . length ( ) ;
2005-10-23 19:50:27 +02:00
if ( l > = n ) return s ;
2008-12-04 13:54:16 +01:00
final StringBuilder sb = new StringBuilder ( l + n ) ;
2008-08-02 14:12:04 +02:00
for ( final int i = l + n ; i > n ; n - - ) sb . insert ( 0 , fillChar ) ;
2005-10-23 19:50:27 +02:00
return sb . toString ( ) ;
}
2006-10-23 02:59:55 +02:00
2008-08-02 14:12:04 +02:00
public static final String arrayList ( final byte [ ] b , final int start , int length ) {
2006-10-25 03:21:05 +02:00
if ( b = = null ) return " NULL " ;
if ( b . length = = 0 ) return " [] " ;
2007-04-19 15:37:02 +02:00
length = Math . min ( length , b . length - start ) ;
2008-12-04 13:54:16 +01:00
final StringBuilder sb = new StringBuilder ( b . length * 4 ) ;
2007-10-19 06:13:46 +02:00
sb . append ( '[' ) . append ( Integer . toString ( b [ start ] ) ) . append ( ',' ) ;
for ( int i = 1 ; i < length ; i + + ) sb . append ( ' ' ) . append ( Integer . toString ( b [ start + i ] ) ) . append ( ',' ) ;
2006-10-23 02:59:55 +02:00
sb . append ( ']' ) ;
return sb . toString ( ) ;
}
2006-10-24 15:48:16 +02:00
2008-08-02 14:12:04 +02:00
public static final String table ( final byte [ ] b , final int linewidth , final int marker ) {
2006-10-25 03:21:05 +02:00
if ( b = = null ) return " NULL " ;
if ( b . length = = 0 ) return " [] " ;
2008-12-04 13:54:16 +01:00
final StringBuilder sb = new StringBuilder ( b . length * 4 ) ;
2006-10-24 15:48:16 +02:00
for ( int i = 0 ; i < b . length ; i + + ) {
2006-10-25 03:21:05 +02:00
if ( i % linewidth = = 0 )
2007-02-20 09:35:51 +01:00
sb . append ( '\n' ) . append ( " # " ) . append ( Integer . toHexString ( i ) ) . append ( " : " ) ;
2006-10-24 15:48:16 +02:00
else
sb . append ( ',' ) ;
2007-10-19 06:13:46 +02:00
sb . append ( ' ' ) . append ( Integer . toString ( 0xff & b [ i ] ) ) ;
2006-10-25 03:21:05 +02:00
if ( i > = 65535 ) break ;
2006-10-24 15:48:16 +02:00
}
sb . append ( '\n' ) ;
return sb . toString ( ) ;
}
2008-08-02 14:12:04 +02:00
public static final boolean allZero ( final byte [ ] a ) {
2006-10-24 15:48:16 +02:00
return allZero ( a , 0 , a . length ) ;
}
2008-08-02 14:12:04 +02:00
public static final boolean allZero ( final byte [ ] a , final int astart , final int alength ) {
2006-10-24 15:48:16 +02:00
for ( int i = 0 ; i < alength ; i + + ) if ( a [ astart + i ] ! = 0 ) return false ;
return true ;
}
2005-06-09 11:25:08 +02:00
}