Skip to content

Commit

Permalink
Logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
keznikl committed Oct 25, 2013
1 parent 068c29e commit 75f459b
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
29 changes: 29 additions & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/logging/CustomLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cz.cuni.mff.d3s.deeco.logging;

import java.util.logging.Level;

/**
* Class defining two additional levels (DEBUG,ERROR) to the logging levels of
* {@link java.util.logging.Level} class.
*
* @author Ilias Gerostathopoulos
*
*/
final class CustomLevel extends Level {

private static final long serialVersionUID = -3922922184407799203L;

public static final Level DEBUG = new CustomLevel("DEBUG",
Level.FINE.intValue() + 1);

public static final Level ERROR = new CustomLevel("ERROR",
Level.SEVERE.intValue() + 1);

protected CustomLevel(String name, int value) {
super(name, value);
}

public static void registerCustomLevels() {
}

}
51 changes: 51 additions & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/logging/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cz.cuni.mff.d3s.deeco.logging;

/**
* API for sending log output.
*
* There are 4 logging levels: DEBUG(d) < INFO(i) < WARNING(w) < ERROR(e)
*
* Could be extended in the future to choose between Standard- and OSGiLogger at
* runtime in the <code>getLogger</code> method.
*
* @author Ilias Gerostathopoulos
*
*/
public class Log {

private static Logger getLogger() {
return StandardLogger.getLogger();
}

public static void d(String msg) {
getLogger().debug(msg);
}

public static void d(String msg, Throwable t) {
getLogger().debug(msg, t);
}

public static void i(String msg) {
getLogger().info(msg);
}

public static void i(String msg, Throwable t) {
getLogger().info(msg, t);
}

public static void w(String msg) {
getLogger().warning(msg);
}

public static void w(String msg, Throwable t) {
getLogger().warning(msg, t);
}

public static void e(String msg) {
getLogger().error(msg);
}

public static void e(String msg, Throwable t) {
getLogger().error(msg, t);
}
}
88 changes: 88 additions & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/logging/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cz.cuni.mff.d3s.deeco.logging;

/**
* Common abstraction for OSGi-based & non OSGi-based Loggers.
*
* There are 4 logging levels: DEBUG(d) < INFO(i) < WARNING(w) < ERROR(e)
*
* @author Ilias Gerostathopoulos
*
*/
interface Logger {

/**
* Mainly for debugging reasons.
*
* @param msg
* the string message
*/
public void debug(String msg);

/**
* Log on the DEBUG level with associated Throwable information (exception
* message and stack trace).
*
* @param msg
* the string message
* @param thrown
* Throwable object (e.g. an Exception)
*/
public void debug(String msg, Throwable thrown);

/**
* General logging method.
*
* @param msg
* the string message
*/
public void info(String msg);

/**
* Log on the INFO level with associated Throwable information (exception
* message and stack trace).
*
* @param msg
* the string message
* @param thrown
* Throwable object (e.g. an Exception)
*/
public void info(String msg, Throwable thrown);

/**
* For warnings (soft errors)
*
* @param msg
* the string message
*/
public void warning(String msg);

/**
* Log on the WARNING level with associated Throwable information (exception
* message and stack trace).
*
* @param msg
* the string message
* @param thrown
* Throwable object (e.g. an Exception)
*/
public void warning(String msg, Throwable thrown);

/**
* For reporting exceptions & errors without the stack trace.
*
* @param msg
* the string message
*/
public void error(String msg);

/**
* Log on the ERROR level with associated Throwable information (exception
* message and stack trace).
*
* @param msg
* the string message
* @param thrown
* Throwable object (e.g. an Exception)
*/
public void error(String msg, Throwable thrown);
}
74 changes: 74 additions & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/logging/StandardLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cz.cuni.mff.d3s.deeco.logging;

import java.io.InputStream;
import java.io.Serializable;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;

/**
* Simple wrapper of java.util.logging.Logger with lazy-initialized singleton
* object and thread-safe methods
*
* @author Ilias Gerostathopoulos
*
*/
enum StandardLogger implements Logger {
INSTANCE;

private java.util.logging.Logger logger;

private StandardLogger() {
CustomLevel.registerCustomLevels();
logger = java.util.logging.Logger.getLogger(getClass().getPackage().getName());
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("logging.properties");
try {
LogManager.getLogManager().readConfiguration(inputStream);
} catch (Exception e) {
logger = java.util.logging.Logger.getLogger("default");
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(CustomLevel.DEBUG);
logger.addHandler(ch);
logger.setLevel(CustomLevel.DEBUG);
logger.severe("Could not load logging.properties file - falling backing to console logging");
}
}

public static Logger getLogger() {
return INSTANCE;
}

public synchronized void debug(String s) {
logger.log(CustomLevel.DEBUG, s);
}

public synchronized void debug(String s, Throwable t) {
logger.log(CustomLevel.DEBUG, s, t);
}

public synchronized void info(String s) {
logger.log(CustomLevel.INFO, s);
}

public synchronized void info(String s, Throwable t) {
logger.log(Level.INFO, s, t);
}

public synchronized void warning(String s) {
logger.log(CustomLevel.WARNING, s);
}

public synchronized void warning(String s, Throwable t) {
logger.log(Level.WARNING, s, t);
}

public synchronized void error(String s) {
logger.log(CustomLevel.ERROR, s);
}

public synchronized void error(String s, Throwable t) {
logger.log(CustomLevel.ERROR, s, t);
}

}

0 comments on commit 75f459b

Please sign in to comment.