Logger injection example
Javadoc for this example.
src_examples/net/sourceforge/anotherfsm/examples/loggerinjection/package-info.java
/*
* Copyright 2013 Michal Turek, AnotherFSM
*
* http://anotherfsm.sourceforge.net/
*
* 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.
*/
/**
* Example to show how to inject unsupported logger to AnotherFSM.
* Log4j (http://logging.apache.org/log4j/1.2/) will be used for demonstration.
*
* <h3>Classes</h3>
*
* <ul>
* <li>{@link net.sourceforge.anotherfsm.examples.loggerinjection.Log4jLogger}
* Wrapper of log4j's logger.</li>
* <li>{@link net.sourceforge.anotherfsm.examples.loggerinjection.Log4jLoggerFactory}
* Factory for wrapper of log4j's logger.</li>
* <li>{@link net.sourceforge.anotherfsm.examples.loggerinjection.LoggerInjectionExample}
* Defines main() method, log sample messages using log4j wrapper.</li>
* </ul>
*
* @author Michal Turek
*/
package net.sourceforge.anotherfsm.examples.loggerinjection;
src_examples/net/sourceforge/anotherfsm/examples/loggerinjection/Log4jLogger.java
/*
* Copyright 2013 Michal Turek, AnotherFSM
*
* http://anotherfsm.sourceforge.net/
*
* 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 net.sourceforge.anotherfsm.examples.loggerinjection;
import net.sourceforge.anotherfsm.logger.FsmLogger;
import org.apache.log4j.Logger;
/**
* Wrapper of log4j's logger.
*
* @author Michal Turek
*/
class Log4jLogger implements FsmLogger {
/** The wrapped log4j logger. */
private final Logger logger;
/**
* Create the object.
*
* @param clazz
* the class
*/
public Log4jLogger(Class<?> clazz) {
logger = Logger.getLogger(clazz);
}
/**
* Create the object.
*
* @param name
* the logger name
*/
public Log4jLogger(String name) {
logger = Logger.getLogger(name);
}
@Override
public String getName() {
return logger.getName();
}
@Override
public void fatal(String message) {
logger.fatal(message);
}
@Override
public void fatal(String message, Throwable throwable) {
logger.fatal(message, throwable);
}
@Override
public void error(String message) {
logger.error(message);
}
@Override
public void error(String message, Throwable throwable) {
logger.error(message, throwable);
}
@Override
public void warn(String message) {
logger.warn(message);
}
@Override
public void warn(String message, Throwable throwable) {
logger.warn(message, throwable);
}
@Override
public void info(String message) {
logger.info(message);
}
@Override
public void info(String message, Throwable throwable) {
logger.info(message, throwable);
}
@Override
public void debug(String message) {
logger.debug(message);
}
@Override
public void debug(String message, Throwable throwable) {
logger.debug(message, throwable);
}
@Override
public void trace(String message) {
logger.trace(message);
}
@Override
public void trace(String message, Throwable throwable) {
logger.trace(message, throwable);
}
@Override
public boolean isInfoEnabled() {
return logger.isInfoEnabled();
}
@Override
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
@Override
public boolean isTraceEnabled() {
return logger.isTraceEnabled();
}
}
src_examples/net/sourceforge/anotherfsm/examples/loggerinjection/Log4jLoggerFactory.java
/*
* Copyright 2013 Michal Turek, AnotherFSM
*
* http://anotherfsm.sourceforge.net/
*
* 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 net.sourceforge.anotherfsm.examples.loggerinjection;
import net.sourceforge.anotherfsm.AnotherFsm;
import net.sourceforge.anotherfsm.Helpers;
import net.sourceforge.anotherfsm.logger.FsmLogger;
import net.sourceforge.anotherfsm.logger.FsmLoggerFactory;
/**
* Factory for wrapper of log4j's logger.
*
* Note AnotherFSM supports by default the following factories/backends.
*
* <ul>
* <li>{@link net.sourceforge.anotherfsm.logger.NoLoggerFactory}
* Disable logging at all.</li>
* <li>{@link net.sourceforge.anotherfsm.logger.BasicLoggerFactory}
* Redirecting of the messages to the standard streams.</li>
* <li>@link {@link net.sourceforge.anotherfsm.logger.StdStreamLoggerFactory}
* Logging to standard output and standard error output.</li>
* <li>{@link net.sourceforge.anotherfsm.logger.JavaLoggerFactory}
* Logging to java.util.logging subsystem.</li>
* </ul>
*
* @author Michal Turek
*
* @see AnotherFsm#setLoggerFactory(FsmLoggerFactory)
*/
public class Log4jLoggerFactory implements FsmLoggerFactory {
@Override
public FsmLogger getLogger(Class<?> clazz) {
return new Log4jLogger(clazz);
}
@Override
public FsmLogger getLogger(Class<?> clazz, String instance) {
// Instance part is not supported by log4j directly
return new Log4jLogger(clazz.getName()
+ Helpers.CLASS_INSTANCE_DELIMITER + instance);
}
@Override
public FsmLogger getLogger(String name) {
return new Log4jLogger(name);
}
}
src_examples/net/sourceforge/anotherfsm/examples/loggerinjection/LoggerInjectionExample.java
/*
* Copyright 2013 Michal Turek, AnotherFSM
*
* http://anotherfsm.sourceforge.net/
*
* 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 net.sourceforge.anotherfsm.examples.loggerinjection;
import net.sourceforge.anotherfsm.AnotherFsm;
import net.sourceforge.anotherfsm.ContainerEvent;
import net.sourceforge.anotherfsm.DeterministicStateMachine;
import net.sourceforge.anotherfsm.FsmException;
import net.sourceforge.anotherfsm.State;
import net.sourceforge.anotherfsm.StateMachine;
import net.sourceforge.anotherfsm.Transition;
import net.sourceforge.anotherfsm.logger.FsmLogger;
import org.apache.log4j.BasicConfigurator;
/**
* Log a message using log4j library.
*
* @author Michal Turek
*/
public class LoggerInjectionExample {
static {
// Register factory of loggers before any logger is created
AnotherFsm.setLoggerFactory(new Log4jLoggerFactory());
// Configure log4j somehow, this may be at the beginning of main()
BasicConfigurator.configure();
}
/** The logger object for this class. */
private final static FsmLogger logger = AnotherFsm
.getLogger(LoggerInjectionExample.class);
/**
* The application start function.
*
* @param args
* the input arguments, unused
*/
public static void main(String[] args) {
// Log something using the log4j wrapper
logger.info("Hello world.");
// Create state machine to demonstrate that log4j is used internally
try (StateMachine machine = new DeterministicStateMachine("test")) {
State state = new State("state");
Transition transition = new Transition(state,
new ContainerEvent<String>("event"));
machine.addState(state);
machine.addTransition(transition);
machine.setStartState(state);
// Messages should be logged using log4j here
machine.start();
machine.process(new ContainerEvent<String>("event"));
} catch (FsmException e) {
// Process any exception that may occur
logger.fatal("Unexpected exception occurred", e);
}
logger.debug("End of main()");
}
}
Sample outputs
0 [main] INFO net.sourceforge.anotherfsm.examples.loggerinjection.LoggerInjectionExample - Hello world.
13 [main] INFO net.sourceforge.anotherfsm.DeterministicStateMachine.test - Transition started: @INITIAL@ -> StartEvent -> state
14 [main] INFO net.sourceforge.anotherfsm.DeterministicStateMachine.test - Transition finished: @INITIAL@ -> StartEvent -> state
14 [main] INFO net.sourceforge.anotherfsm.DeterministicStateMachine.test - Transition started: state -> ContainerEvent(event) -> state
14 [main] INFO net.sourceforge.anotherfsm.DeterministicStateMachine.test - Transition finished: state -> ContainerEvent(event) -> state
14 [main] DEBUG net.sourceforge.anotherfsm.examples.loggerinjection.LoggerInjectionExample - End of main()