Log4j is always available to deployed TransSECS Devices applications and MIX applications built from MIStudio. You can use the pre-configured DataSource message logger (DataSource.logMessage()) which will log to an appender (most likely a log file, but may be a console output) set up in your log4j.xml file. Or you can set up a custom logger in log4j and use this logger in your scripts. Both examples are below.

To use log4j you will need to either use the DataSource.logMessage() set up your own Logger. The first example below shows how to use DataSource.logMessage().

var DataSource = Java.type("com.ergotech.vib.servers.DataSource"); 
...
...
//use the DataSource message logger
DataSource.logMessage("log this text");

Whatever log4j appender is set up for “console” in your log4j.xml file will be used for this logger. Usually for TransSECS applications this is a file called SECSMessages.log. For MIX applications this will be the MIX.log.

You may define a logger in your log4j.xml configuration and use it in your scripts. This allows you to choose the way your messages are logged and the level of the logging (the logger can be turned “off” by setting the log level OFF in the log4j.xml configuration).

Here is an example log4j entry to set up a logger called “ScriptLogger”. You need to define the “appender” first (in the section of the log4j file where other appenders are located), then the logger is defined which uses this appender. Appenders define how the data will be logged - whether this is a file or to the console. In the example below this is a log file called ScriptLog.log:

  <appender name="ExtraLogger" class="org.apache.log4j.RollingFileAppender">
	    <param name="append" value="false" />
	    <param name="maxFileSize" value="1MB" />
	    <param name="maxBackupIndex" value="5" />
	    <param name="file" value="ScriptLog.log" />
	    <layout class="org.apache.log4j.PatternLayout">
		  <param name="ConversionPattern"  value="%d{ISO8601} %m %n" />
	    </layout>
 </appender>
 
   <logger name="ScriptLogger" additivity="false">
      <level value="info"/> 
	  <appender-ref ref="ExtraLogger" />
    </logger>

In your script you will need to define “Logger”:

var Logger = Java.type("org.apache.log4j.Logger");

Then set up the Logger:

  extralogger = Logger.getLogger("ScriptLogger"); //get the ScriptLogger from log4j

Then use the logger to log an “info” level message:

  extralogger.info("Host Command START");

or you can log a debug message (which will only print in the log if the level is set to debug or all)

  extralogger.debug("setPortStatus port "+port+ " status "+status);

You can set the logging level in the log4j.xml file. If you set the level to “off” nothing will be printed. If it is set to “debug” only info, warn,fatal, and debug level messages will be printed. If the level is set to “info”, only info, warn, and fatal messages will be logged. A level of “all” will print every message.

  • configuring_and_using_log4j_in_scripts.txt
  • Last modified: 2021/04/13 01:46
  • by wikiadmin