Logging, Weblogic

How to filter OSB logging and redirect log records to any desirable destination?

The best practice to accomplish this task is to use Weblogic server startup class. In this class we should take Weblogic server “Logger” object and use it to attach additional appender. This appender can be implemented to redirect log messages to any destination You prefer. Log4j supports dozen of standard destinations, providing built in appenders like JDBCAppender, JMSAppender, FileAppender and many others. Lets say we need to redirect log messages from Oracle Service Bus to separate file. We are going to implement this approach on WebLogic version 10.3.5.0.

What is Weblogic server startup class?

This is general kind of java class which main method is called during server startup. Here is an example:

public class StartupClass {
    public static void main(String[] args) {
// Do some startup processing here
          }
}

 

Steps to configure Weblogic startup class for log redirecting to separate file

  • Create Log4j custom filter class to filter messages from “ALSB Logging” logger:
    package logging;
    
    import org.apache.log4j.spi.Filter;
    import org.apache.log4j.spi.LoggingEvent;
    
    public class ALSBFilter extends Filter {
    
     @Override
     public int decide(LoggingEvent event) {
      if (event.getLoggerName() == "ALSB Logging") {
       return NEUTRAL;
      }
      return DENY;
     }
    }
  • Create general java class with log4j appender code inside. Class example:
     
    package logging;
    
    import org.apache.log4j.Logger;
    import org.apache.log4j.PatternLayout;
    import weblogic.logging.log4j.Log4jLoggingHelper;
    import weblogic.logging.log4j.WLLog4jLevel;
    import org.apache.log4j.RollingFileAppender;
    
    public class AppenderStartup {
    
     public static void main(String[] args) {
    
      try {
    
       Logger serverLogger = Log4jLoggingHelper.getLog4jServerLogger();
       RollingFileAppender fa = new RollingFileAppender();
       fa.setFile(args[0]);
       fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
       fa.setThreshold(WLLog4jLevel.INFO);
       fa.setAppend(true);
       fa.setMaxFileSize("10MB");
       fa.setMaxBackupIndex(2);
                            fa.addFilter(new ALSBFilter());
       fa.activateOptions();
       serverLogger.addAppender(fa);
    
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    }

Any appender option could be configurable passing an argument to main method. In the case of example just file name is received through arguments.

  • Export either ALSBFilter and AppenderStartup classes to JAR.
  • Login to Weblogic console and register newly created class as startup one. To do this follow the steps:
    • In the left-hand domain structure menu expand “Enviroment” and select “Startup and shutdown classes”.
    • Check “Startup class”, click next
    • In the fileld “Name” enter desired name for this class to be listed in weblogic (not actual class name, i.e. My-startup-class-1). In the field “Class name” enter actual class name(our example case – AppenderStartup). Click next.
    • Select target server where this class should be executed during startup (i.e. “osb_server1”). Click finish.
    • Click on newly registered class in the “Summary of Startup and Shutdown Classes” table. Now few more configuration options are available. In the field “Arguments” enter file name for log. Save.
  • Enable “Log4j” logging implementation in Weblogic:
    Environment –> Servers –> [Target server](i.e.”osb_server1″) -> Logging-> Advanced ->
    Logging implemention -> Select LOG4J
  • Ensure to have these libs on server classpath (recomended just to put under domain lib folder – %DOMAIN_HOME%/lib):
    • log4j-1.2.15.jar
    • wllog4j.jar
    • AppenderStartup.jar (our startup class inside)
  • Restart server. Check for logs appearing in new log file.