Skip to content

Commit

Permalink
* Change logger configuration
Browse files Browse the repository at this point in the history
* Add activity lifecycle logger
  • Loading branch information
Jan Rehwaldt committed Feb 28, 2011
1 parent 8dda625 commit 6c17839
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 46 deletions.
17 changes: 5 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
/target
/test-output
/testlog.txt
/benchmark.txt

/bin
/engine-core-api/target
/engine-core-api/bin
/engine-core-impl/target
/engine-core-impl/target
/engine-core-impl/testlog.txt

test-output
target
bin
reports
logs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.hpi.oryxengine.activity;

import java.util.Observable;

import javax.annotation.Nonnull;

import de.hpi.oryxengine.process.instance.ProcessInstance;
Expand All @@ -10,6 +12,7 @@
* An activity is the behaviour of a node. So to say what it does.
*/
public abstract class AbstractActivityImpl
extends Observable
implements Activity {

private ExecutionState state;
Expand All @@ -18,7 +21,7 @@ public abstract class AbstractActivityImpl
* Instantiates a new activity. State is set to INIT.
*/
protected AbstractActivityImpl() {
this.state = ExecutionState.INIT;
changeState(ExecutionState.INIT);
}

/**
Expand All @@ -30,23 +33,24 @@ protected AbstractActivityImpl() {
}

/**
* Sets the state of the node.
* Changes the state of the node.
*
* @param state the new state
*/
protected void setState(@Nonnull ExecutionState state) {
private void changeState(@Nonnull ExecutionState state) {
this.state = state;
setChanged();
notifyObservers(state);
}

// start execution

/**
* {@inheritDoc}
*/
@Override
public final void execute(@Nonnull ProcessInstance instance) {
setState(ExecutionState.ACTIVE);
changeState(ExecutionState.ACTIVE);
executeIntern(instance);
setState(ExecutionState.COMPLETED);
changeState(ExecutionState.COMPLETED);
}

/**
Expand All @@ -55,4 +59,12 @@ public final void execute(@Nonnull ProcessInstance instance) {
* @param instance the instance this activity operates on
*/
protected abstract void executeIntern(@Nonnull ProcessInstance instance);

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package de.hpi.oryxengine.plugin;

import java.util.Observable;
import java.util.Observer;

import javax.annotation.Nonnull;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import de.hpi.oryxengine.activity.AbstractActivityImpl;
import de.hpi.oryxengine.activity.ExecutionState;

/**
* This class is a logger and may be injected to observe
* the lifecycle of certain activities.
*/
public final class ActivityLifecycleLogger
implements Observer {

private static ActivityLifecycleLogger instance;

private final Logger logger = Logger.getLogger(getClass());
private final Level level = Level.DEBUG;

/**
* Hide singleton constructor.
*/
private ActivityLifecycleLogger() {

}

/**
* Returns a lazily initialized logger instance.
*
* @return a logger instance
*/
public static ActivityLifecycleLogger getInstance() {
if (instance == null) {
instance = new ActivityLifecycleLogger();
}

return instance;
}

/**
* Register this logger instance with the provided activity.
*
* @param activity the observed activity
*/
public void registerWithActivity(@Nonnull Observable activity) {
activity.addObserver(this);
}

/**
* This method is invoked whenever the activity's state changes.
*
* {@inheritDoc}
*/
@Override
public void update(@Nonnull Observable activity,
@Nonnull Object state) {
stateChanged((AbstractActivityImpl) activity, (ExecutionState) state);
}

/**
* Logs the change event to the default logger.
*
* @param activity the activity, which changed
* @param state the new state
*/
private void stateChanged(@Nonnull AbstractActivityImpl activity,
@Nonnull ExecutionState state) {
logger.log(level, "Activity " + activity.toString() + " changed. New state: " + state.toString());
}
}
37 changes: 26 additions & 11 deletions engine-core-impl/src/main/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="file" value="benchmark.txt"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss:SSS} - %p: %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="datePattern" value="'.'yyyy-MM-dd_HH-mm" />
<param name="file" value="logs/logger.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
</layout>
</appender>


<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
</layout>
</appender>

<logger name="de.hpi.oryxengine.example.ExampleProcessForReview">
<level value="DEBUG" />
</logger>

<root>
<priority value="debug" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>

<root>
<priority value ="debug" />
<appender-ref ref="file" />
</root>

</log4j:configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.util.UUID;

import de.hpi.oryxengine.activity.AbstractActivityImpl;
import de.hpi.oryxengine.activity.Activity;
import de.hpi.oryxengine.activity.impl.AddNumbersAndStoreActivity;
import de.hpi.oryxengine.activity.impl.EndActivity;
import de.hpi.oryxengine.activity.impl.MailingVariable;
import de.hpi.oryxengine.activity.impl.PrintingVariableActivity;
import de.hpi.oryxengine.activity.impl.StartActivity;
import de.hpi.oryxengine.navigator.NavigatorImpl;
import de.hpi.oryxengine.plugin.ActivityLifecycleLogger;
import de.hpi.oryxengine.process.instance.ProcessInstanceImpl;
import de.hpi.oryxengine.process.structure.NodeImpl;
import de.hpi.oryxengine.routing.behaviour.RoutingBehaviour;
Expand Down Expand Up @@ -60,13 +62,18 @@ private static ProcessInstanceImpl processInstanceForReview() {
/*
* The process looks like this: start => calc5Plus5 => printResult => mailingTheResult => end
*/

ActivityLifecycleLogger lifecycleLogger = ActivityLifecycleLogger.getInstance();

Activity start = new StartActivity();
Activity calc5Plus5 = new AddNumbersAndStoreActivity("result", 5, 5);
Activity printResult = new PrintingVariableActivity("result");
AbstractActivityImpl calc5Plus5 = new AddNumbersAndStoreActivity("result", 5, 5);
lifecycleLogger.registerWithActivity(calc5Plus5);
PrintingVariableActivity printResult = new PrintingVariableActivity("result");
lifecycleLogger.registerWithActivity(printResult);
// Default to gerardo.navarro-suarez@student.hpi.uni-potsdam.de
Activity mailingResult = new MailingVariable("result");
Activity end = new EndActivity();
MailingVariable mailingResult = new MailingVariable("result");
lifecycleLogger.registerWithActivity(mailingResult);
EndActivity end = new EndActivity();
lifecycleLogger.registerWithActivity(end);

RoutingBehaviour behaviour = new TakeAllBehaviour();

Expand Down
37 changes: 26 additions & 11 deletions engine-core-impl/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="file" value="testlog.txt"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss:SSS} - %p: %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="datePattern" value="'.'yyyy-MM-dd_HH-mm" />
<param name="file" value="logs/logger.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
</layout>
</appender>


<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
</layout>
</appender>

<logger name="de.hpi.oryxengine.example.ExampleProcessForReview">
<level value="DEBUG" />
</logger>

<root>
<priority value="debug" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>

<root>
<priority value ="debug" />
<appender-ref ref="file" />
</root>

</log4j:configuration>

0 comments on commit 6c17839

Please sign in to comment.