Monday

Oracle service bus logging with custom JDBCAppander

Last few weeks we are upgrading logging system of our MDM system, we have design a central place for all logging data. With central log system we can afford to manage our system very effectively and debug any fatal error. With OSB it's not straight forword to pulling logging data from console to somewhere, because a few subsystem uses different appender to write and filter log. See the following link to know more about oracle logging service. In my previous post i demonstrate how to use log4j JDBC appender in log4j.properties to redirect log to database table.
For the current post we also uses same DDL to create tables
1)
CREATE TABLE LOGS 
(USER_ID VARCHAR2(20), 
DOMAIN  varchar2(50), 
DATED   DATE NOT NULL, 
LOGGER  VARCHAR2(500) NOT NULL, 
LEVEL   VARCHAR2(50) NOT NULL, 
MESSAGE VARCHAR2(4000) NOT NULL 
);

2) Create a simple maven project with a few dependency to create custom JDBCAppender
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.xyz.wl.logging</groupId>
<artifactId>jdbc-logging</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jdbc-logging</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>weblogic</groupId>
<artifactId>wlfullclient</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>weblogic</groupId>
<artifactId>wllog4j</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>               
</plugin>
</plugins>
</build>
</project>

assume that you have already installed wlfullclient.jar and wllog4j.jar in your maven local repository.
3) Now it's time for coding, create two java class as follows:
package ru.xyz.wl.logging;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;

import weblogic.logging.log4j.WLLog4jLogEvent;

/**

*/
public class JDBCAppender extends AppenderSkeleton {
private Connection conn;
private PreparedStatement stm;
private String subSysName;
public JDBCAppender(String subSysName) {
this.subSysName = subSysName;
// get Initial ctx
try {
InitialContext ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup("jdbc/logDataSource");
conn = dataSource.getConnection();
stm = conn.prepareStatement("INSERT INTO EXT_LOGS2 VALUES(?,?,?,?,?,?)");
stm.setEscapeProcessing(true);
conn.setAutoCommit(true);

} catch (NamingException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
}

}

protected void append(LoggingEvent event) {
WLLog4jLogEvent logEvent = (WLLog4jLogEvent) event;
try {
stm.setString(1, logEvent.getUserId());
stm.setString(2, subSysName);
//stm.setDate(3, new Date(logEvent.getTimestamp()));
stm.setTimestamp(3, new Timestamp(logEvent.getTimestamp()));
stm.setString(4, logEvent.getLoggerName());
stm.setString(5, logEvent.getLevel().toString());
stm.setString(6, logEvent.getLogMessage());
stm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void close() {
try {
stm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public boolean requiresLayout() {
return false;
}
}

create AppenderStartup class to start up the appender
package ru.xyz.wl.logging;

import org.apache.log4j.Logger;
import weblogic.logging.log4j.Log4jLoggingHelper;
import weblogic.logging.log4j.WLLog4jLevel;
import weblogic.logging.LoggerNotAvailableException;
import weblogic.logging.NonCatalogLogger;

import java.sql.Date;
import java.util.Arrays;

/**
* User: sahmed

*/
public class AppenderStartup {
public static void main(String... args) {
System.out.println("Starting up .. Log4j appneder");
if(args.length < 2){
System.out.println("JDBC appender not configured ..");
System.out.println("AppenderStartup argumemnts must be 2, usages ERCI WARNING, where ERCI - Sub system name, WARNING log level");
System.out.println("Put arguments and restart Admin server.");
System.err.println("JDBC appender not configured ..");
return;
}
try {
Logger serverLogger = Log4jLoggingHelper.getLog4jServerLogger();
//Logger domainLogger = Log4jLoggingHelper.getLog4jDomainLogger();
JDBCAppender jdbcAppender = new JDBCAppender(args[0]);
serverLogger.addAppender(jdbcAppender);
jdbcAppender.setThreshold(WLLog4jLevel.toLevel(args[1].toUpperCase(), WLLog4jLevel.INFO));
//domainLogger.addAppender(jdbcAppender);

//NonCatalogLogger nc = new NonCatalogLogger("MyAppenderTest");
//nc.info("Test INFO message");
//nc.warning("Test WARNING message");
} catch (LoggerNotAvailableException e) {
e.printStackTrace();
}
}
}
this class required two arguments to startup, one of them are subsystem name and another one is the Log level, by default it's sets log level INFO. Now we are ready to implements our library to redirect all the log file to database tables. 4)First of all we have to create a datasource on web logic server with JNDI name jdbc/logDataSource and configure it. Next we have to put following libs on weblgic Domain lib directory
  • jdbc-logging-1.0-SNAPSHOT.jar
  • log4j-1.2.15.jar
  • ojdbc-14.jar (optional)
  • wllog4j.jar

Next logon to weblogic web console and create a startup class with AppenderStartup
Class Name : ru.xyz.wl.logging.AppenderStartup
arguments : erci WARNING, where erci - subsystme name, WARNING log level
5) "Enable Redirect stdout logging enabled" and "Redirect stderr logging enabled" on
environment->server-> admin server-> logging-> advance
also Select Logging implemention LOG4J.
6) restart admin server.
Now all the log messages from Server logger will redirect not only to console window but also to database table.
Post a Comment