You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by oz...@apache.org on 2004/05/26 11:06:37 UTC
cvs commit: jakarta-slide/proposals/jdk14/src/org/apache/slide/util/logger/jdk14 SimpleLogfileFormatter.java Jdk14Logger.java LogfileFormatter.java LoggerConfiguration.java ConsoleLogfileFormatter.java
ozeigermann 2004/05/26 02:06:36
Added: src/jdk14/org/apache/slide/util/logger/jdk14
SimpleLogfileFormatter.java Jdk14Logger.java
ConsoleLogfileFormatter.java
LoggerConfiguration.java LogfileFormatter.java
Removed: proposals/jdk14/src/org/apache/slide/util/logger/jdk14
SimpleLogfileFormatter.java Jdk14Logger.java
LogfileFormatter.java LoggerConfiguration.java
ConsoleLogfileFormatter.java
Log:
Moved jdk1.4 logger from proposals to src
Revision Changes Path
1.1 jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/SimpleLogfileFormatter.java
Index: SimpleLogfileFormatter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/SimpleLogfileFormatter.java,v 1.1 2004/05/26 09:06:36 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/26 09:06:36 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.slide.util.logger.jdk14;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/**
*
* @author <a href="mailto:dflorey@c1-fse.de">Daniel Florey</a>
* @author <a href="mailto:ozeigermann@c1-fse.de">Oliver Zeigermann</a>
*/
public class SimpleLogfileFormatter extends java.util.logging.Formatter {
public final static String SIMPLE_FORMAT_STRING = "MM dd HH:mm:ss";
private SimpleDateFormat dateFormatter;
/** Creates a new instance of LogfileFormatter */
public SimpleLogfileFormatter(String dateFormatPattern) {
dateFormatter = new SimpleDateFormat(dateFormatPattern);
}
public SimpleLogfileFormatter() {
this(SIMPLE_FORMAT_STRING);
}
/**
* Format the given log record and return the formatted string.
* <p>
* The resulting formatted String will normally include a
* localized and formated version of the LogRecord's message field.
* The Formatter.formatMessage convenience method can (optionally)
* be used to localize and format the message field.
*
* @param record the log record to be formatted.
* @return the formatted log record
*/
public String format(LogRecord record) {
// give it a special appearance if it is a warning or an error
boolean highlight = record.getLevel().intValue() > Level.INFO.intValue();
StringBuffer message = new StringBuffer();
Date logDate = new Date(record.getMillis());
if (highlight) message.append("\n");
message.append(record.getSequenceNumber());
message.append(" - ");
message.append(dateFormatter.format(logDate));
message.append(", ");
message.append(record.getLevel());
message.append(" - ");
message.append(record.getLoggerName());
message.append(" - ");
message.append(record.getSourceMethodName());
message.append(":\n");
message.append(formatMessage(record));
message.append("\n");
if (record.getThrown() != null) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
pw.close();
message.append(sw.toString());
} catch (Exception ex) {
}
}
if (highlight) message.append("\n");
return message.toString();
}
}
1.1 jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/Jdk14Logger.java
Index: Jdk14Logger.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/Jdk14Logger.java,v 1.1 2004/05/26 09:06:36 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/26 09:06:36 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.slide.util.logger.jdk14;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Default logger implementation. Uses java.util.logging implementation provided by Java 1.4.
*
* @author <a href="mailto:ozeigermann@c1-fse.de">Oliver Zeigermann</a>
*/
public class Jdk14Logger implements org.apache.slide.util.logger.Logger {
public final static int DEFAULT_LEVEL = INFO;
public Jdk14Logger() {
LoggerConfiguration.configure();
}
public void log(Object data, Throwable throwable, String channel, int slideLevel) {
Logger logger = Logger.getLogger(channel);
Level level = getLevelForSlideLevel(slideLevel);
logger.log(level, data.toString(), throwable);
}
public void log(Object data, String channel, int slideLevel) {
Logger logger = Logger.getLogger(channel);
Level level = getLevelForSlideLevel(slideLevel);
if (data instanceof Throwable) {
logger.log(level, data.toString(), (Throwable) data);
} else {
logger.log(level, data.toString());
}
}
public void log(Object data, int level) {
this.log(data, DEFAULT_CHANNEL, level);
}
public void log(Object data) {
this.log(data, DEFAULT_CHANNEL, DEFAULT_LEVEL);
}
public int getLoggerLevel(String channel) {
Logger logger = Logger.getLogger(channel);
Level level = logger.getLevel();
return getSlideLevelForLevel(level);
}
public int getLoggerLevel() {
return getLoggerLevel(DEFAULT_CHANNEL);
}
public void setLoggerLevel(int slideLevel) {
Logger.getLogger("").setLevel(getLevelForSlideLevel(slideLevel));
setLoggerLevel(DEFAULT_CHANNEL, slideLevel);
}
public void setLoggerLevel(String channel, int slideLevel) {
Logger logger = Logger.getLogger(channel);
Level level = getLevelForSlideLevel(slideLevel);
logger.setLevel(level);
}
public boolean isEnabled(String channel, int slideLevel) {
Logger logger = Logger.getLogger(channel);
Level level = getLevelForSlideLevel(slideLevel);
return logger.isLoggable(level);
}
public boolean isEnabled(int level) {
return isEnabled(DEFAULT_CHANNEL, level);
}
protected Level getLevelForSlideLevel(int slideLevel) {
Level level = Level.ALL;
switch (slideLevel) {
case EMERGENCY :
level = Level.SEVERE;
break;
case CRITICAL :
level = Level.SEVERE;
break;
case ERROR :
level = Level.SEVERE;
break;
case WARNING :
level = Level.WARNING;
break;
case INFO :
level = Level.INFO;
break;
case DEBUG :
level = Level.FINE;
break;
}
return level;
}
protected int getSlideLevelForLevel(Level level) {
if (level.equals(Level.ALL))
return DEBUG;
if (level.equals(Level.FINEST))
return DEBUG;
if (level.equals(Level.FINER))
return DEBUG;
if (level.equals(Level.FINE))
return DEBUG;
if (level.equals(Level.CONFIG))
return DEBUG;
if (level.equals(Level.INFO))
return INFO;
if (level.equals(Level.WARNING))
return WARNING;
if (level.equals(Level.SEVERE))
return ERROR;
if (level.equals(Level.OFF))
return -1;
return DEFAULT_LEVEL;
}
}
1.1 jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/ConsoleLogfileFormatter.java
Index: ConsoleLogfileFormatter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/ConsoleLogfileFormatter.java,v 1.1 2004/05/26 09:06:36 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/26 09:06:36 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.slide.util.logger.jdk14;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/**
* Formatter for console output. Tries to present essential information in a manner similar to tomcat.
*
* @author <a href="mailto:ozeigermann@c1-fse.de">Oliver Zeigermann</a>
*/
public class ConsoleLogfileFormatter extends java.util.logging.Formatter {
public String format(LogRecord record) {
// give it a special appearance if it is a warning or an error
boolean highlight = record.getLevel().intValue() > Level.INFO.intValue();
StringBuffer message = new StringBuffer();
if (highlight) message.append("\n");
message.append(record.getSequenceNumber());
message.append(" ");
message.append(record.getLevel());
message.append(" ");
if (highlight) message.append("\n");
message.append(record.getLoggerName());
message.append(" - ");
message.append(formatMessage(record));
message.append("\n");
if (record.getThrown() != null) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
pw.close();
message.append(sw.toString());
} catch (Exception ex) {
}
}
if (highlight) message.append("\n");
return message.toString();
}
}
1.1 jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/LoggerConfiguration.java
Index: LoggerConfiguration.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/LoggerConfiguration.java,v 1.1 2004/05/26 09:06:36 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/26 09:06:36 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.slide.util.logger.jdk14;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.io.*;
/**
* Configures Java 1.4 logger.
*
* @author <a href="mailto:ozeigermann@c1-fse.de">Oliver Zeigermann</a>
*/
public class LoggerConfiguration {
private final static Logger logger = Logger.getLogger(LoggerConfiguration.class.getName());
public final static String DEFAULT_LOG_FILE = "Slide.log";
public final static Level DEFAULT_LOG_LEVEL = Level.ALL;
public final static String DEFAULT_FORMAT_STRING = SimpleLogfileFormatter.SIMPLE_FORMAT_STRING;
// new LogfileFormatter(DEFAULT_FORMAT_STRING));
public final static Formatter DEFAULT_FORMATTER = new SimpleLogfileFormatter(DEFAULT_FORMAT_STRING);
public final static Formatter DETAIL_FORMATTER = new LogfileFormatter(DEFAULT_FORMAT_STRING);
private static boolean isConfigured = false;
public final static void configure() {
configure(DEFAULT_LOG_FILE, DEFAULT_LOG_LEVEL, Level.INFO);
}
/**
* Configures standard Java1.4 logger
*
*/
public final static void configure(String logFilePath, Level logLevel, Level logToConsoleThreshold) {
try {
if (isConfigured)
return;
isConfigured = true;
// XXX first set new formatter to (default) console handler(s)
Handler[] handlers = Logger.getLogger("").getHandlers();
for (int i = 0; i < handlers.length; i++) {
Handler handler = handlers[i];
if (handler instanceof ConsoleHandler) {
handler.setFormatter(new ConsoleLogfileFormatter());
handler.setLevel(logToConsoleThreshold);
// could exit loop here, but keep on going for robustness (might be more than on handler)
}
}
// set default logging
Logger.getLogger("").setLevel(logLevel);
Handler fileHandler = new FileHandler(logFilePath);
fileHandler.setFormatter(DETAIL_FORMATTER);
Logger.getLogger("").addHandler(fileHandler);
logger.log(Level.INFO, "Logging Service configured");
} catch (IOException ioe) {
logger.log(Level.WARNING, "Could not set logging configuration", ioe);
}
}
}
1.1 jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/LogfileFormatter.java
Index: LogfileFormatter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/LogfileFormatter.java,v 1.1 2004/05/26 09:06:36 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/26 09:06:36 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.slide.util.logger.jdk14;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/**
*
* @author <a href="mailto:dflorey@c1-fse.de">Daniel Florey</a>
*/
public class LogfileFormatter extends java.util.logging.Formatter {
public final static String COMPLEX_FORMAT_STRING =
"yyyy-MM-dd HH:mm:ss,SSSS";
private SimpleDateFormat dateFormatter;
/** Creates a new instance of LogfileFormatter */
public LogfileFormatter(String dateFormatPattern) {
dateFormatter = new SimpleDateFormat(dateFormatPattern);
}
public LogfileFormatter() {
this(COMPLEX_FORMAT_STRING);
}
/**
* Format the given log record and return the formatted string.
* <p>
* The resulting formatted String will normally include a
* localized and formated version of the LogRecord's message field.
* The Formatter.formatMessage convenience method can (optionally)
* be used to localize and format the message field.
*
* @param record the log record to be formatted.
* @return the formatted log record
*/
public String format(LogRecord record) {
// give it a special appearance if it is a warning or an error
boolean highlight = record.getLevel().intValue() > Level.INFO.intValue();
StringBuffer message = new StringBuffer();
Date logDate = new Date(record.getMillis());
message.append(record.getSequenceNumber());
message.append(" - ");
message.append(dateFormatter.format(logDate));
message.append(", ");
message.append(record.getLevel());
message.append(" [Thread: ");
message.append(Thread.currentThread());
message.append("] ");
message.append(record.getLoggerName());
message.append(" - ");
message.append(record.getSourceMethodName());
message.append(":\n");
message.append(formatMessage(record));
message.append("\n");
if (record.getThrown() != null) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
pw.close();
message.append(sw.toString());
} catch (Exception ex) {
}
}
if (highlight) message.append("\n");
return message.toString();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org