Commit 21d592c7 authored by Lubomir Bulej's avatar Lubomir Bulej

Added utility classes for logging.

parent 2e7f4b17
......@@ -10,5 +10,6 @@
<classpathentry kind="lib" path="lib/asm-debug-all-5.0.3.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/junit-4.11.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/>
<classpathentry kind="output" path="output/build/disl"/>
</classpath>
......@@ -8,6 +8,9 @@ junit.core.path=${lib.dir}/${junit.core.lib}
junit.hamcrest.lib=hamcrest-core-1.3.jar
junit.hamcrest.path=${lib.dir}/${junit.hamcrest.lib}
log4j.lib=log4j-1.2.15.jar
log4j.path=${lib.dir}/${log4j.lib}
ant-contrib.lib=ant-contrib-0.6.jar
ant-contrib.resource=net/sf/antcontrib/antlib.xml
ant-contrib.path=${lib.dir}/${ant-contrib.lib}
......
......@@ -22,6 +22,10 @@
<pathelement location="${asm.path}" />
</path>
<path id="log4j.classpath">
<pathelement location="${log4j.path}" />
</path>
<path id="junit.classpath">
<pathelement location="${junit.core.path}" />
<pathelement location="${junit.hamcrest.path}" />
......@@ -202,7 +206,9 @@
<target name="compile-util">
<mkdir dir="${build.util}" />
<javac destdir="${build.util}" srcdir="${src.util}" debug="true" />
<javac destdir="${build.util}" srcdir="${src.util}" debug="true">
<classpath refid="log4j.classpath" />
</javac>
</target>
......
This diff is collapsed.
package ch.usi.dag.util.logging;
import java.util.HashMap;
import java.util.Map;
/**
* Provides a logging service implemented using the Java logging.
*
* @author Lubomir Bulej
*/
public final class JavaProvider implements Provider {
@SuppressWarnings ("serial")
private static final Map <Level, java.util.logging.Level>
__levels__ = new HashMap <Level, java.util.logging.Level> () {{
put (Level.TRACE, java.util.logging.Level.FINEST);
put (Level.DEBUG, java.util.logging.Level.FINE);
put (Level.INFO, java.util.logging.Level.INFO);
put (Level.WARN, java.util.logging.Level.WARNING);
put (Level.ERROR, java.util.logging.Level.SEVERE);
}};
/* ***********************************************************************
* Provider interface
* ***********************************************************************/
@Override
public String name () {
return "java";
}
@Override
public LevelLogger createLevelLogger (
final Level level, final String subsystem
) {
return new JavaLevelLogger (
__levels__.get (level),
java.util.logging.Logger.getLogger (subsystem)
);
}
@Override
public void setGlobalLevel (final Level level) {
java.util.logging.Logger.getGlobal ().setLevel (
__levels__.get (level)
);
}
/* ***********************************************************************
* JavaLevelLogger
* ***********************************************************************/
private static final class JavaLevelLogger implements LevelLogger {
private final java.util.logging.Logger __logger;
private final java.util.logging.Level __level;
//
JavaLevelLogger (
final java.util.logging.Level level,
final java.util.logging.Logger logger
) {
__logger = logger;
__level = level;
}
@Override
public boolean isEnabled () {
return __logger.isLoggable (__level);
}
@Override
public void log (final String message) {
__logger.log (__level, message);
}
@Override
public void log (final Throwable cause, final String message) {
__logger.log (__level, message, cause);
}
}
}
package ch.usi.dag.util.logging;
enum Level {
/**
* Represents a logging level for errors that typically result in program
* termination.
*/
ERROR,
/**
* Represents a logging level for problems that are not fatal to program
* execution, but may impede its function and should be noticed by users.
*/
WARN,
/**
* Represents a logging level for general informative messages.
*/
INFO,
/**
* Represents a logging level for debugging messages. These provide
* additional information compared to informative messages, which may be
* useful for high-level debugging.
*/
DEBUG,
/**
* Represents a logging level for trace messages. These provide detailed
* low-level debugging information for many operations, producing a high
* amount of log messages.
*/
TRACE;
//
public static Level parse (final String name) {
if (name != null) {
return valueOf (name.trim ().toUpperCase ());
} else {
return null;
}
}
}
package ch.usi.dag.util.logging;
/**
* Represents a logger for a particular logging level. The implementation is
* responsible for mapping the common {@link Level} to the
* implementation-specific logging level and invoking the corresponding
* operations on the underlying implementation-specific logger.
*
* @author Lubomir Bulej
*/
interface LevelLogger {
/**
* Checks if messages for the level represented by this {@link LevelLogger}
* would be actually logged. The check may be specific underlying logging
* infrastructure, i.e., it may be local to a logger, or global.
*
* @param level
* a common logging {@link Level}.
* @return {@code true} if the given message level is currently being
* logged.
*/
boolean isEnabled ();
/**
* Logs a message at the level represented by this {@link LevelLogger}.
*
* @param message
* a message to be logged.
*/
void log (String message);
/**
* Logs a message at the level represented by this {@link LevelLogger}
* associated with a particular {@link Throwable}.
*
* @param throwable
* a {@link Throwable} associated with the message.
* @param message
* a message to be logged.
*/
void log (Throwable cause, String message);
}
package ch.usi.dag.util.logging;
import java.util.HashMap;
import java.util.Map;
/**
* Provides a logging service implemented using the Apache Log4j framework.
*
* @author Lubomir Bulej
*/
public final class Log4jProvider implements Provider {
@SuppressWarnings ("serial")
private static final Map <Level, org.apache.log4j.Level>
__levels__ = new HashMap <Level, org.apache.log4j.Level> () {{
put (Level.TRACE, org.apache.log4j.Level.TRACE);
put (Level.DEBUG, org.apache.log4j.Level.DEBUG);
put (Level.INFO, org.apache.log4j.Level.INFO);
put (Level.WARN, org.apache.log4j.Level.WARN);
put (Level.ERROR, org.apache.log4j.Level.ERROR);
}};
//
static {
// TODO Configure logger from external file
org.apache.log4j.BasicConfigurator.configure ();
}
/* ***********************************************************************
* Provider interface
* ***********************************************************************/
@Override
public String name () {
return "log4j";
}
@Override
public LevelLogger createLevelLogger (
final Level level, final String subsystem
) {
return new Log4jLevelLogger (
__levels__.get (level),
org.apache.log4j.Logger.getLogger (subsystem)
);
}
@Override
public void setGlobalLevel (final Level level) {
org.apache.log4j.Logger.getRootLogger ().setLevel (
__levels__.get (level)
);
}
/* ***********************************************************************
* Log4jLevelLogger
* ***********************************************************************/
private static final class Log4jLevelLogger implements LevelLogger {
private final org.apache.log4j.Logger __logger;
private final org.apache.log4j.Level __level;
//
private Log4jLevelLogger (
final org.apache.log4j.Level level,
final org.apache.log4j.Logger logger
) {
__logger = logger;
__level = level;
}
@Override
public boolean isEnabled () {
return __logger.isEnabledFor (__level);
}
@Override
public void log (final String message) {
__logger.log (__level, message);
}
@Override
public void log (final Throwable cause, final String message) {
__logger.log (__level, message, cause);
}
}
}
This diff is collapsed.
package ch.usi.dag.util.logging;
interface Provider {
/**
* Returns the name of this logging provider.
*
* @return The name of this logging provider.
*/
String name ();
/**
* Creates a new logger for the specified common logging {@link Level},
* which maps logger operations to implementation-specific logging level
* corresponding to the common level.
*
* @param level
* the logging level the logger is created for.
* @param subsystem
* the name of the subsystem the logger is created for.
*/
LevelLogger createLevelLogger (Level level, String subsystem);
/**
* Sets global common logging {@link Level} for loggers produced by this
* provider.
*
* @param level
* the logging level to set.
*/
void setGlobalLevel (Level level);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment