krysalis-update/src/java/org/krysalis/depot/common/log ILogger.java,NONE,1.1 Logger.java,NONE,1.1 StandardLogListener.java,NONE,1.1 LogConstants.java,NONE,1.1
Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:38 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/common/log
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/common/log
Added Files:
ILogger.java Logger.java StandardLogListener.java
LogConstants.java
Log Message:
Copied from the apache incubator.
--- NEW FILE: Logger.java ---
/*
* Copyright 2004 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.krysalis.depot.common.log;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import org.krysalis.depot.common.util.debug.DebugUtils;
/**
* @author anou_mana
*/
public class Logger
{
// Listener Tables...
private List m_debugListeners = null;
private List m_infoListeners = null;
private List m_verboseListeners = null;
private List m_warnListeners = null;
private List m_errorListeners = null;
// Chaining...
private Logger m_next = null;
// Quick check
//:TODO: Make it switchable later
public static boolean g_loggingOn = true;
// The root singleton instance
private static Logger l_logger = null;
class LoggingContext
{
//
// The context
//
Logger m_currentLogger = null;
Stack m_stack = null;
LoggingContext(Logger currentLogger)
{
m_stack = new Stack();
push(currentLogger);
}
public void push(Logger currentLogger)
{
m_stack.push(currentLogger);
m_currentLogger = (Logger) m_stack.peek();
}
public void pop(Logger currentLogger)
{
Logger currentTop = (Logger) m_stack.pop();
// Set current
m_currentLogger = (Logger) m_stack.peek();
if (currentTop != currentLogger)
m_currentLogger.warn("Popped a bad logging context ["
+ currentLogger + "] != [" + currentTop + "]");
}
public Logger getCurrentLogger()
{
return m_currentLogger;
}
}
//
// One per thread...
//
private static ThreadLocal l_context = null;
//:TODO: Store a Stack/Current Logger object in a ThreadLocal
// variable, so can have high level code push a "context" and
// lower level code will respect it (transparently)
// Push a logger which chains (next) the current logger, to
// get nested context...
static
{
l_logger = new Logger();
l_context = new ThreadLocal() {
protected synchronized Object initialValue()
{
return l_logger.new LoggingContext(l_logger);
}
};
}
public Logger(Logger next)
{
initClass();
m_next = next;
}
public Logger()
{
initClass();
m_next = null;
}
private void initClass()
{
m_debugListeners = new ArrayList();
m_infoListeners = new ArrayList();
m_verboseListeners = new ArrayList();
m_warnListeners = new ArrayList();
m_errorListeners = new ArrayList();
}
public static Logger getLogger()
{
return ((LoggingContext) l_context.get()).getCurrentLogger();
}
public static Logger pushContext(ILogger listener)
{
return pushContext(LogConstants.DEBUG, listener);
}
public static Logger pushContext(int level, ILogger listener)
{
Logger newLogger = pushContext();
// :TODO: Fix level, ask listener what they want...
newLogger.addListener(level, listener);
return newLogger;
}
public static Logger pushContext()
{
LoggingContext context = ((LoggingContext) l_context.get());
Logger newLogger = new Logger(context.getCurrentLogger());
context.push(newLogger);
return newLogger;
}
public static Logger pushContext(Logger newContext)
{
((LoggingContext) l_context.get()).push(newContext);
return newContext;
}
public static void popContext(Logger expectedLogger)
{
((LoggingContext) l_context.get()).pop(expectedLogger);
}
public void addListener(int listenerType, ILogger listener)
{
switch (listenerType)
{
default :
case LogConstants.DEBUG :
{
m_debugListeners.add(listener);
// Note: Falls through to INFO...
}
case LogConstants.INFO :
{
m_infoListeners.add(listener);
// Note: Falls through to VERBOSE...
}
case LogConstants.VERBOSE :
{
m_verboseListeners.add(listener);
// Note: Falls through to WARN...
}
case LogConstants.WARN :
{
m_warnListeners.add(listener);
// Note: Falls through to ERROR...
}
case LogConstants.ERROR :
{
m_errorListeners.add(listener);
break;
}
}
}
public void removeListener(int listenerType, ILogger listener)
{
switch (listenerType)
{
default :
case LogConstants.DEBUG :
{
m_debugListeners.remove(listener);
// Note: Falls through to VERBOSE...
}
case LogConstants.VERBOSE :
{
m_infoListeners.remove(listener);
// Note: Falls through to INFO...
}
case LogConstants.INFO :
{
m_infoListeners.remove(listener);
// Note: Falls through to WARN...
}
case LogConstants.WARN :
{
m_warnListeners.remove(listener);
// Note: Falls through to ERROR...
}
case LogConstants.ERROR :
{
m_errorListeners.remove(listener);
break;
}
}
}
// DEBUG
public boolean isDebug()
{
return !m_debugListeners.isEmpty();
}
public void debug(Object message)
{
notify(m_debugListeners, LogConstants.DEBUG, message, null);
if (null != m_next)
m_next.debug(message);
}
public void debug(Object message, Throwable thrower)
{
notify(m_debugListeners, LogConstants.DEBUG, message, thrower);
if (null != m_next)
m_next.debug(message, thrower);
}
// VERBOSE
public boolean isVerbose()
{
return !m_verboseListeners.isEmpty();
}
public void verbose(Object message)
{
notify(m_verboseListeners, LogConstants.VERBOSE, message, null);
if (null != m_next)
m_next.verbose(message);
}
public void verbose(Object message, Throwable thrower)
{
notify(m_verboseListeners, LogConstants.VERBOSE, message, thrower);
if (null != m_next)
m_next.verbose(message, thrower);
}
// INFO
public void info(Object message)
{
notify(m_infoListeners, LogConstants.INFO, message, null);
if (null != m_next)
m_next.info(message);
}
public void info(Object message, Throwable thrower)
{
notify(m_infoListeners, LogConstants.INFO, message, thrower);
if (null != m_next)
m_next.info(message, thrower);
}
// WARN
public void warn(Object message)
{
notify(m_warnListeners, LogConstants.WARN, message, null);
if (null != m_next)
m_next.warn(message);
}
public void warn(Object message, Throwable thrower)
{
notify(m_warnListeners, LogConstants.WARN, message, thrower);
if (null != m_next)
m_next.warn(message, thrower);
}
// ERROR
public void error(Object message)
{
notify(m_errorListeners, LogConstants.ERROR, message, null);
if (null != m_next)
m_next.error(message);
}
public void error(Object message, Throwable thrower)
{
notify(m_errorListeners, LogConstants.ERROR, message, thrower);
if (null != m_next)
m_next.error(message, thrower);
}
private void notify(List listeners, int severity, Object message,
Throwable thrower)
{
for (Iterator i = listeners.iterator(); i.hasNext();)
{
try
{
((ILogger) i.next()).log(severity, message, thrower);
} catch (Throwable t)
{
// Their problems are not ours...
System.err.println("Logger failed to log.");
}
}
}
public static void testInit()
{
DebugUtils.enableCommonsLogging();
// If we don't have a decent listener, yet
// (and above doesn't add one, but this might not be
// the first call to thie method) then add one.
if (!getLogger().isDebug())
{
getLogger().addListener(LogConstants.DEBUG,
new StandardLogListener());
getLogger().debug("Test logging enabled.");
}
}
}
--- NEW FILE: StandardLogListener.java ---
/*
* Copyright 2004 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.krysalis.depot.common.log;
/**
* @author anou_mana
*/
public class StandardLogListener implements ILogger {
public void log(int severity, Object message, Throwable thrower) {
String severityStr = "UNKNOWN";
switch (severity) {
case LogConstants.DEBUG :
{
severityStr = "DEBUG ";
break;
}
case LogConstants.INFO :
{
severityStr = "INFO ";
break;
}
case LogConstants.VERBOSE :
{
severityStr = "VERBOSE";
break;
}
case LogConstants.WARN :
{
severityStr = "WARN ";
break;
}
case LogConstants.ERROR :
{
severityStr = "ERROR ";
break;
}
}
System.out.print(severityStr);
System.out.print(" : ");
System.out.println(message.toString());
if (null != thrower) {
System.out.println(
"Exception: "
+ thrower.getClass().getName()
+ " : "
+ thrower.getLocalizedMessage());
thrower.printStackTrace(System.out);
}
}
}
--- NEW FILE: LogConstants.java ---
/*
* Copyright 2004 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.krysalis.depot.common.log;
/**
* @author anou_mana
*/
public class LogConstants {
/** Logging Severities *
*
*/
/**
* ERROR severity
*/
public static final int ERROR = 1;
public static final int WARN = 2;
public static final int INFO = 3;
public static final int VERBOSE = 4;
public static final int DEBUG = 5;
}
--- NEW FILE: ILogger.java ---
/*
* Copyright 2004 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.krysalis.depot.common.log;
/**
* @author anou_mana
*/
public interface ILogger {
/**
* A listener based log interface
*
* @param severity see LogConstants
* @param message what to log
* @param thrower. Note: thrower is null if not present
* @see LogConstants
*/
void log(int severity, Object message, Throwable thrower);
}
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/