Skip to content

Commit

Permalink
Adding logging formaters that simplifies the log output and the formater
Browse files Browse the repository at this point in the history
for console output adds coloring based on the log levels. It means that
the info and debug log messages in the console will no longer be colored
red. In order to be able to take advantage of this feature a plugin
needs to be installed into eclipse. More info will be provided on the
github project page.
  • Loading branch information
iridin committed Mar 18, 2015
1 parent 2ecc09f commit f293bb9
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 5 deletions.
1 change: 1 addition & 0 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/logging/Log.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.cuni.mff.d3s.deeco.logging;


/**
* API for sending log output.
*
Expand Down
2 changes: 1 addition & 1 deletion jdeeco-core/src/cz/cuni/mff/d3s/deeco/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/
interface Logger {

/**
* Mainly for debugging reasons.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cz.cuni.mff.d3s.deeco.logging;

import java.util.logging.LogRecord;

public class SimpleColorFormatter extends SimpleFormatter {

private enum LogColor{

/**
* ANSI_RESET
*/
DEFAULT("\u001B[0m"),
/**
* ANSI_BLACK
*/
BLACK("\u001B[30m"),
/**
* ANSI_RED
*/
RED("\u001B[91m"),
/**
* ANSI_GREEN
*/
GREEN("\u001B[32m"),
/**
* ANSI_YELLOW
*/
YELLOW("\u001B[33m"),
/**
* ANSI_BLUE
*/
BLUE("\u001B[34m"),
/**
* ANSI_PURPLE
*/
PURPLE("\u001B[35m"),
/**
* ANSI_CYAN
*/
CYAN("\u001B[36m"),
/**
* ANSI_WHITE
*/
WHITE("\u001B[37m");

public final String mark;

private LogColor(String mark){
this.mark = mark;
}
}

public String format(LogRecord record) {
StringBuilder builder = new StringBuilder();

if(record.getLevel().equals(CustomLevel.DEBUG)){
builder.append(LogColor.CYAN.mark);
} else if(record.getLevel().equals(CustomLevel.INFO)){
builder.append(LogColor.GREEN.mark);
} else if(record.getLevel().equals(CustomLevel.WARNING)){
builder.append(LogColor.YELLOW.mark);
} else if(record.getLevel().equals(CustomLevel.ERROR)){
builder.append(LogColor.RED.mark);
}

super.preFormat(builder, record);

builder.append(LogColor.DEFAULT.mark).append("\n");

return builder.toString();
}

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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class SimpleFormatter extends Formatter {

private static final DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss.SSS");

protected void preFormat(StringBuilder builder, LogRecord record) {
builder.append("[")
.append(record.getLevel())
.append("] [")
.append(dateFormat.format(new Date(record.getMillis())))
.append("] ")
.append(formatMessage(record));
}

public String format(LogRecord record) {
StringBuilder builder = new StringBuilder();
preFormat(builder, record);
builder.append("\n");
return builder.toString();
}

public String getHead(Handler h) {
return super.getHead(h);
}

public String getTail(Handler h) {
return super.getTail(h);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ void ensembleControllerActiveChanged(ComponentInstance instance, EnsembleControl
}

public void ensembleFormed(final EnsembleDefinition e, final ComponentInstance c, final String coordID, final String memberID) {
Log.w("Ensemble "+e+" formed at the side of " + c + " with coord: "+coordID+" and member: "+memberID);
Log.i("Ensemble "+e+" formed at the side of " + c + " with coord: "+coordID+" and member: "+memberID);

EnsembleInstance ensembleInstance = ArchitectureFactory.eINSTANCE.createEnsembleInstance();
ensembleInstance.setEnsembleDefinition(e);
Expand Down
4 changes: 2 additions & 2 deletions jdeeco-core/test/logging.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=WARNING
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.formatter=cz.cuni.mff.d3s.deeco.logging.SimpleColorFormatter

java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=logs/jdeeco.log
Expand All @@ -11,6 +11,6 @@ java.util.logging.FileHandler.append=true

# Number of rotating files to be used
java.util.logging.FileHandler.count=4
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.formatter=cz.cuni.mff.d3s.deeco.logging.SimpleFormatter

.level=INFO
1 change: 0 additions & 1 deletion jdeeco-network-plugin/test/.gitignore

This file was deleted.

16 changes: 16 additions & 0 deletions jdeeco-network-plugin/test/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=WARNING
java.util.logging.ConsoleHandler.formatter=cz.cuni.mff.d3s.deeco.logging.SimpleColorFormatter

java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=logs/jdeeco.log

# Write 10MB before rotating this file
java.util.logging.FileHandler.limit=10000000
java.util.logging.FileHandler.append=true

# Number of rotating files to be used
java.util.logging.FileHandler.count=4
java.util.logging.FileHandler.formatter=cz.cuni.mff.d3s.deeco.logging.SimpleFormatter

.level=INFO

0 comments on commit f293bb9

Please sign in to comment.