Striim 4.0.4 documentation

Configuring Log4j

Edit the conf/log4j.server.properties file and add the following new section:

log4j.logger.com.mycompany.custom.packagename.MyCustomFunctions=debug, MyCustomFunctionsAppender
log4j.additivity.com.mycompany.custom.packagename.MyCustomFunctions=false
log4j.appender.MyCustomFunctionsAppender=org.apache.log4j.FileAppender
log4j.appender.MyCustomFunctionsAppender.File=logs/MyCustomFunctions.log.out
# Define the layout for file appender
log4j.appender.MyCustomFunctionsAppender.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.MyCustomFunctionsAppender.layout.conversionPattern=%-6p:%-15M:%m%n

In this example, a separate logs/MyCustomFunctions.log.out file will be created. Its log level will be debug, and it will output the log level, method, and message. Some code examples for good log entries:

  • logger.trace("execute() started");

  • logger.debug(COMMANDS, "{} received {}", name, param);

  • logger.debug( "sending encrypted request, length = {}\n{}", () -> fullEncryptedRequest.length, () -> Arrays.toString(fullEncryptedRequest) );

In the following Java class, the logger is used to output the running sum and count values:

    @AggHandlerDesc(handler=AvgDebug_Float.class)
    public abstract Float AvgDebug( String sKey, Float fArg );
    public static class AvgDebug_Float
    {
        float fRunningSum = 0;
        int iRunningCount = 0;
        
        public Float getAggValue()
        {
            logger.debug( "RunningSum: {},  RunningCount: {}\n", fRunningSum, iRunningCount);
            return (iRunningCount == 0) ? 0 : (fRunningSum / iRunningCount);
        }
        public void incAggValue(String sKey, Float fArg)
        {
            if(fArg != null) {
            	iRunningCount++;
                fRunningSum += fArg;
            }
            logger.debug( "[Key: {}, Value: {}] RunningSum is now: {},  RunningCount is now: {}",
              sKey, fArg, fRunningSum, iRunningCount);
        }
        public void decAggValue(String sKey, Float fArg)
        {
            if(fArg != null) {
            	iRunningCount--;
                fRunningSum -= fArg;
            }
            logger.debug( "[Key: {}, Value: {}] RunningSum is now: {},  RunningCount is now: {}",
              sKey, fArg, fRunningSum, iRunningCount);
        }
    }

One recommendation for your logging applications is to use StringBuffer to concatenate the strings used in your logging output. This will increase efficiency and ensure greater security in your applications.