diff options
Diffstat (limited to 'common/src/main/java/at/gv/egovernment/moa/logging')
4 files changed, 380 insertions, 0 deletions
diff --git a/common/src/main/java/at/gv/egovernment/moa/logging/LogMsg.java b/common/src/main/java/at/gv/egovernment/moa/logging/LogMsg.java new file mode 100644 index 000000000..890bbeff7 --- /dev/null +++ b/common/src/main/java/at/gv/egovernment/moa/logging/LogMsg.java @@ -0,0 +1,58 @@ +/* +* Copyright 2003 Federal Chancellery Austria +* +* 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 at.gv.egovernment.moa.logging; + +/** + * A unified message type to log messages from inside the MOA subsystem. + * + * @author Patrick Peck + * @version $Id$ + */ +public class LogMsg { + /** The message to log. */ + private Object message; + + /** + * Create a <code>LogMsg</code> object. + * + * @param message The actual message to log. May be <code>null</code>. + */ + public LogMsg(Object message) { + this.message = message; + } + + /** + * Convert this log message to a <code>String</code>. + * + * @return The <code>String</code> representation of this log message. + */ + public String toString() { + StringBuffer msg = new StringBuffer(); + LoggingContext ctx = + LoggingContextManager.getInstance().getLoggingContext(); + String tid = ctx != null ? ctx.getTransactionID() : null; + String nodeId = ctx != null ? ctx.getNodeID() : null; + + msg.append("TID="); + msg.append(tid != null ? tid : "<null>"); + msg.append(" NID="); + msg.append(nodeId != null ? nodeId : "<null>"); + msg.append(" MSG="); + msg.append(message != null ? message.toString() : "<null>"); + + return msg.toString(); + } +} diff --git a/common/src/main/java/at/gv/egovernment/moa/logging/Logger.java b/common/src/main/java/at/gv/egovernment/moa/logging/Logger.java new file mode 100644 index 000000000..dd36bfc4b --- /dev/null +++ b/common/src/main/java/at/gv/egovernment/moa/logging/Logger.java @@ -0,0 +1,190 @@ +/* +* Copyright 2003 Federal Chancellery Austria +* +* 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 at.gv.egovernment.moa.logging; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * A utility class acting as a facade to the logging subsystem. + * + * Configure the logging defaultHierarchy that the <code>Logger</code> uses by + * calling <code>setHierarchy</code> once before calling any of the logging + * output functions. + * + * @author Patrick Peck + * @version $Id$ + */ +public class Logger { + + /** The default logging hierarchy. */ + private static String defaultHierarchy = ""; + + /** + * Get the <code>Log</code> object for the default hierarchy. + * + * @return The <code>Log</code> object to write log messages to. + */ + private static Log getLog() { + return LogFactory.getLog(defaultHierarchy); + } + + /** + * Get the <code>Log</code> object for a given hierarchy. + * + * @param hierarchy The logging hierarchy for which to return the logger. + * @return The <code>Log</code> object to write log messages to. + */ + private static Log getLog(String hierarchy) { + return LogFactory.getLog(hierarchy); + } + + /** + * Set the default hierarchy to which the <code>Logger</code> should send its + * logging output. + * @param hierarchy The logging defaultHierarchy. + */ + public static void setHierarchy(String hierarchy) { + defaultHierarchy = hierarchy; + } + + /** + * Test, if the trace log level is enabled. + * + * @return boolean <code>true</code>, if tracing output is enabled + * <code>false</code> otherwise. + */ + public static boolean isTraceEnabled() { + return getLog().isTraceEnabled(); + } + + /** + * Test, if the trace log level is enabled for a given hierarchy. + * + * @param hierarchy requested log hierarchy + * @return boolean <code>true</code>, if tracing output is enabled + * <code>false</code> otherwise. + */ + public static boolean isTraceEnabled(String hierarchy) { + return getLog(hierarchy).isTraceEnabled(); + } + + /** + * Trace a message. + * + * @param message The message to trace. + */ + public static void trace(Object message) { + getLog().trace(message); + } + + /** + * Test, if the debug log level is enabled. + * + * @return boolean <code>true</code>, if debug output is enabled + * <code>false</code> otherwise. + */ + public static boolean isDebugEnabled() { + return getLog().isDebugEnabled(); + } + + /** + * Test, if the debug log level is enabled for a given hierarchy. + * + * @param hierarchy requested log hierarchy + * @return boolean <code>true</code>, if debug output is enabled + * <code>false</code> otherwise. + */ + public static boolean isDebugEnabled(String hierarchy) { + return getLog(hierarchy).isDebugEnabled(); + } + + /** + * Log a debug message. + * + * @param message The message to log. + */ + public static void debug(Object message) { + getLog().debug(message); + } + + /** + * Log an info message. + * + * @param message The message to log. + */ + public static void info(Object message) { + getLog().info(message); + } + + /** + * Log a warning message. + * + * @param message The message to log. + */ + public static void warn(Object message) { + getLog().warn(message); + } + + /** + * Log a warning message. + * + * @param message The message to log. + * @param t An exception that may be the cause of the warning. + */ + public static void warn(Object message, Throwable t) { + getLog().warn(message, t); + } + + /** + * Log an error message. + * + * @param message The message to log. + */ + public static void error(Object message) { + getLog().error(message); + } + + /** + * Log an error message. + * + * @param message The message to log. + * @param t An exception that may be the cause of the error. + */ + public static void error(Object message, Throwable t) { + getLog().error(message, t); + } + + /** + * Log a fatal error message. + * + * @param message The message to log. + */ + public static void fatal(Object message) { + getLog().fatal(message); + } + + /** + * Log a fatal error message. + * + * @param message The message to log. + * @param t An exception that may be the cause of the error. + */ + public static void fatal(Object message, Throwable t) { + getLog().fatal(message, t); + } + +} diff --git a/common/src/main/java/at/gv/egovernment/moa/logging/LoggingContext.java b/common/src/main/java/at/gv/egovernment/moa/logging/LoggingContext.java new file mode 100644 index 000000000..249c80184 --- /dev/null +++ b/common/src/main/java/at/gv/egovernment/moa/logging/LoggingContext.java @@ -0,0 +1,61 @@ +/* +* Copyright 2003 Federal Chancellery Austria +* +* 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 at.gv.egovernment.moa.logging; + +/** + * Encapsulates contextual information (i.e. per request information) for + * logging purposes. + * + * @author Patrick Peck + * @version $Id$ + */ +public class LoggingContext { + /** The name of the node ID system property. */ + public static final String NODE_ID_PROPERTY = "moa.node-id"; + + /** The current transaction ID. */ + private String transactionID; + /** The node ID. */ + private String nodeID; + + /** + * Create a new <code>LoggingContext</code>. + * + * @param transactionID The transaction ID. May be <code>null</code>. + */ + public LoggingContext(String transactionID) { + this.transactionID = transactionID; + this.nodeID = System.getProperty(NODE_ID_PROPERTY); + } + + /** + * Return the transaction ID. + * + * @return The transaction ID. + */ + public String getTransactionID() { + return transactionID; + } + + /** + * Return the node ID. + * + * @return The node ID. + */ + public String getNodeID() { + return nodeID; + } +} diff --git a/common/src/main/java/at/gv/egovernment/moa/logging/LoggingContextManager.java b/common/src/main/java/at/gv/egovernment/moa/logging/LoggingContextManager.java new file mode 100644 index 000000000..ef37dc6ba --- /dev/null +++ b/common/src/main/java/at/gv/egovernment/moa/logging/LoggingContextManager.java @@ -0,0 +1,71 @@ +/* +* Copyright 2003 Federal Chancellery Austria +* +* 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 at.gv.egovernment.moa.logging; + +/** + * Provides each thread with a single instance of <code>LoggingContext</code>. + * + * @author Patrick Peck + * @version $Id$ + */ +public class LoggingContextManager { + /** The single instance of this class. */ + private static LoggingContextManager instance = null; + + /** The <code>LoggingContext</code> for each thread. */ + private ThreadLocal context; + + /** + * Get the single instance of the <code>LoggingContextManager</code> class. + * + * @return LoggingContextManager The single instance. + */ + public static synchronized LoggingContextManager getInstance() { + if (instance == null) { + instance = new LoggingContextManager(); + } + return instance; + } + + /** + * Creates a new <code>LoggingContextManager</code>. + * + * Protected to disallow direct instantiation. + */ + protected LoggingContextManager() { + context = new ThreadLocal(); + } + + /** + * Set the <code>LoggingContext</code> context for the current thread. + * + * @param ctx The <code>LoggingContext</code> for the current thread. + */ + public void setLoggingContext(LoggingContext ctx) { + context.set(ctx); + } + + /** + * Return the <code>LoggingContext</code> for the current thread. + * + * @return LoggingContext The <code>LoggingContext</code> for the current + * thread, or <code>null</code> if none has been set. + */ + public LoggingContext getLoggingContext() { + return (LoggingContext) context.get(); + } + +} |