Configuring Logging in SCXML

SCXML uses commons-logging [1], a wrapper that works between others with Log4J [2]. In any case after you decide what logging engine is best suitable for your project you need to set it up before SCXML is able to actually log ussing commons-logging.


Use Case # 1

problem: You are using SCXML which in turns uses commons-logging but your code cannot use commons-logging.

solution: You need to initialize one of the supported by commons-logging engines like Log4J. You better use a separate file to log SCXML-commons traces.

implementation: If you do not want to include a new resource into your project (you can use properties file or xml file for Log4J configuration), you can choose (not reccomended but in some cases necessary) to configure it programmatically:

    logger = LogManager.exists("");
    if (logger == null)
        Logger logger = Logger.getLogger("");
        Logger rootLogger = Logger.getRootLogger();
        SimpleLayout rootLayout = new SimpleLayout();
        Appender rootAppender = new ConsoleAppender();

        //Specific layout pattern for custom application
        String pattern = "%d{yyyy/MM/dd HH:mm:ss.SSS} @ " + Constants.LOG_HINT + " @ SCXML Engine @ %m%n";
        PatternLayout layout = new PatternLayout(pattern);
        boolean append = true;
        boolean bufferedIO = false;
        int bufferSize = 10;
        String filename = "completePathOfYourFileNameAddingTimestampToNameForExample.log";
        if(logger.getAppender("") == null)
                FileAppender appdr = null;
                        appdr = new FileAppender(layout, filename, append, bufferedIO, bufferSize);
                    catch (IOException e)
                        //deal with it.

With the code above you will be able to obtain the info messages from Var and Assign needed to debug what is going on inside the State Machine Engine (Using last version of commons-logging -1.1- and Log4J -1.2.14- the TRACE comes up like a DEBUG message so at least for these versions it makes sense to use INFO instead):

For Var:

String msg = "Var." + name + "=" + varStr;
        if( appLog.isInfoEnabled() )
  msg );

Warning: With the above Log4J version you should avoid using the same file to log your custom System messages and the ones from commons-SCXML if your System does not use commons-logging as well. Otherwise you might experiment some flushing problems resulting in trace lines missing in the output. There are several posts related to this issue and there is no a clear and homegeneous solution about it.




SCXML/Tutorials/Logging (last edited 2009-09-20 23:48:33 by localhost)