Log4j 2
  1. Log4j 2
  2. LOG4J2-442

Log4j2 Database insert problem in Websphere

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta8
    • Fix Version/s: 2.0-rc1
    • Component/s: Appenders, Configurators
    • Labels:
      None
    • Environment:

      Java Web Service in Websphere server

      Description

      In my local,

      I created an log4j2.xml to config like that :

      log4j2.xml
      
          <?xml version="1.0" encoding="UTF-8"?>
      
          <configuration status="OFF">
            <appenders>
              <!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
      
              <FastFile name="AsyncFastFile" fileName="${sys:logFilename}" 
                        immediateFlush="false" append="true">
                <PatternLayout>
                  <pattern>test %m %m %ex%n</pattern>
                </PatternLayout>
              </FastFile>
      
          <JDBC name="databaseAppender" tableName="mytablename">
                <DriverManager url="jdbc:oracle:thin:ORCL_USERNAME/PASS@//MYSERVERNAME:PORTNO/DBNAME" />
                <Column name="KEY1" pattern="%X{sayi1}" />
                <Column name="KEY2" pattern="%X{sayi2}" />
                </JDBC>
      
              <JDBC name="databaseAppenderJNDI" tableName="mytablename">
            <DataSource jndiName="java:/comp/env/jdbc/logWS" />
            <Column name="KEY1" pattern="%X{sayi1}" />
            <Column name="KEY2" pattern="%X{sayi2}" />
           </JDBC>  
      
      
            </appenders>
      
            <loggers>
      
      
              <!-- pattern layout actually uses location, so we need to include it -->
              <asyncLogger name="ASYNC" level="TRACE" additivity="false">
                <appender-ref ref="databaseAppender"/>
              </asyncLogger>
      
       <!-- pattern layout actually uses location, so we need to include it -->
              <asyncLogger name="ASYNCwithJNDI" level="TRACE" additivity="false">
                <appender-ref ref="databaseAppenderJNDI"/>
              </asyncLogger>
      
      
              <root level="info" includeLocation="true">
                <appender-ref ref="databaseAppender"/>
              </root>
      
            </loggers>
          </configuration>
      

      I can insert logs to text file and DB by switching loggername in my java class which I call log function. There no problem in my local.

      In Webshere server,

      I call a web service for logging. I am using same config file and same java codes which calls log function. I can insert logs to a file but I can't insert logs to database. I tried both driverManager url and JNDI. But nothing changed. In two ways, I can't insert log to DB and program doesn't throw any error in try-catch. So I can't find problem where is.

      Note : There is no problem at JNDI url and drivermanager url. Because I can perfectly use these urls in different webservice in webshere. Also I can insert log to Database in my local with same codes.

      Note : There is no problem to insert logs to txt file. only DB insertion is problem in Websphere server. So there isn't problem at config file classpath.

      For your info, I am using oracle DB but the problem isn't about DB.
      I trace network packages in server and I realize that web service don't send any package to DB. Packages don't reach to DB.

      How can I solve this problem?

      THX.

        Issue Links

          Activity

          Hide
          Matt Sicker added a comment -

          What version of WebSphere? I'm taking a look at this, but I've never used WS before (I'm more familiar with WebLogic), and there's like 20 different editions of this AS!

          Show
          Matt Sicker added a comment - What version of WebSphere? I'm taking a look at this, but I've never used WS before (I'm more familiar with WebLogic), and there's like 20 different editions of this AS!
          Hide
          Matt Sicker added a comment -

          Alright, I've been at this for a while, and I've gotten it to work. Here's what I did (using HSQLDB):

          <?xml version="1.0" encoding="UTF-8"?>
          <configuration status="OFF">
              <appenders>
                  <JDBC name="databaseAppender" tableName="log">
                      <ConnectionFactory class="org.mjunx.HyperConnectionFactory" method="getDataSource"/>
                      <Column name="eventDate" isEventTimestamp="true"/>
                      <Column name="level" pattern="%level"/>
                      <Column name="logger" pattern="%logger"/>
                      <Column name="message" pattern="%message"/>
                      <Column name="exception" pattern="%ex{full}"/>
                  </JDBC>
              </appenders>
              <loggers>
                  <root level="info" includeLocation="true">
                      <appender-ref ref="databaseAppender"/>
                  </root>
              </loggers>
          </configuration>
          

          And the connection factory:

          package org.mjunx;
          
          import org.hsqldb.jdbc.JDBCDataSourceFactory;
          
          import javax.sql.DataSource;
          import java.util.Properties;
          
          public class HyperConnectionFactory {
              private static final Properties p;
              static {
                  p = new Properties();
                  p.setProperty("url", "jdbc:hsqldb:file:/opt/dev/logdb/log");
                  p.setProperty("user", "SA");
                  p.setProperty("password", "");
              }
              public static DataSource getDataSource() throws Exception {
                  return JDBCDataSourceFactory.createDataSource(p);
              }
          }
          

          Since I'm not entirely sure how to set up JNDI sources in WebSphere (Liberty profile version), I can't test that part out. I'm not entirely sure if the DriverManager version worked or not because I was having database locking issues from all the undeploy/deploy operations and such I've been doing to test this out.

          The only other possibly relevant things I've got for this would be my pom file:

          <?xml version="1.0" encoding="UTF-8"?>
          <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>org.mjunx</groupId>
              <artifactId>websphere-logging</artifactId>
              <packaging>war</packaging>
              <version>1.0-SNAPSHOT</version>
          
              <name>websphere-logging</name>
              <url>http://maven.apache.org</url>
          
              <dependencies>
                  <dependency>
                      <groupId>org.apache.logging.log4j</groupId>
                      <artifactId>log4j-core</artifactId>
                      <version>2.0RC1-SNAPSHOT</version>
                  </dependency>
                  <dependency>
                      <groupId>org.hsqldb</groupId>
                      <artifactId>hsqldb</artifactId>
                      <version>2.3.1</version>
                  </dependency>
                  <dependency>
                      <groupId>javax.servlet</groupId>
                      <artifactId>javax.servlet-api</artifactId>
                      <version>3.0.1</version>
                      <scope>provided</scope>
                  </dependency>
              </dependencies>
          
              <build>
                  <finalName>${project.name}</finalName>
                  <outputDirectory>${project.basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
                  <plugins>
                      <plugin>
                          <artifactId>maven-ear-plugin</artifactId>
                          <version>2.9</version>
                          <configuration>
                              <encoding>UTF-8</encoding>
                              <version>6</version>
                          </configuration>
                      </plugin>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-compiler-plugin</artifactId>
                          <version>3.1</version>
                          <configuration>
                              <source>1.6</source>
                              <target>1.6</target>
                          </configuration>
                      </plugin>
                      <plugin>
                          <groupId>com.ibm.websphere.wlp.maven.plugins</groupId>
                          <artifactId>liberty-maven-plugin</artifactId>
                          <version>1.1</version>
                          <extensions>true</extensions>
                          <configuration>
                              <serverHome>/opt/dev/wlp</serverHome>
                          </configuration>
                      </plugin>
                  </plugins>
              </build>
          </project>
          
          Show
          Matt Sicker added a comment - Alright, I've been at this for a while, and I've gotten it to work. Here's what I did (using HSQLDB): <?xml version= "1.0" encoding= "UTF-8" ?> <configuration status= "OFF" > <appenders> <JDBC name= "databaseAppender" tableName= "log" > <ConnectionFactory class= "org.mjunx.HyperConnectionFactory" method= "getDataSource" /> <Column name= "eventDate" isEventTimestamp= "true" /> <Column name= "level" pattern= "%level" /> <Column name= "logger" pattern= "%logger" /> <Column name= "message" pattern= "%message" /> <Column name= "exception" pattern= "%ex{full}" /> </JDBC> </appenders> <loggers> <root level= "info" includeLocation= "true" > <appender-ref ref= "databaseAppender" /> </root> </loggers> </configuration> And the connection factory: package org.mjunx; import org.hsqldb.jdbc.JDBCDataSourceFactory; import javax.sql.DataSource; import java.util.Properties; public class HyperConnectionFactory { private static final Properties p; static { p = new Properties(); p.setProperty( "url" , "jdbc:hsqldb:file:/opt/dev/logdb/log" ); p.setProperty( "user" , "SA" ); p.setProperty( "password" , ""); } public static DataSource getDataSource() throws Exception { return JDBCDataSourceFactory.createDataSource(p); } } Since I'm not entirely sure how to set up JNDI sources in WebSphere (Liberty profile version), I can't test that part out. I'm not entirely sure if the DriverManager version worked or not because I was having database locking issues from all the undeploy/deploy operations and such I've been doing to test this out. The only other possibly relevant things I've got for this would be my pom file: <?xml version= "1.0" encoding= "UTF-8" ?> <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> org.mjunx </groupId> <artifactId> websphere-logging </artifactId> <packaging> war </packaging> <version> 1.0-SNAPSHOT </version> <name> websphere-logging </name> <url> http://maven.apache.org </url> <dependencies> <dependency> <groupId> org.apache.logging.log4j </groupId> <artifactId> log4j-core </artifactId> <version> 2.0RC1-SNAPSHOT </version> </dependency> <dependency> <groupId> org.hsqldb </groupId> <artifactId> hsqldb </artifactId> <version> 2.3.1 </version> </dependency> <dependency> <groupId> javax.servlet </groupId> <artifactId> javax.servlet-api </artifactId> <version> 3.0.1 </version> <scope> provided </scope> </dependency> </dependencies> <build> <finalName> ${project.name} </finalName> <outputDirectory> ${project.basedir}/src/main/webapp/WEB-INF/classes </outputDirectory> <plugins> <plugin> <artifactId> maven-ear-plugin </artifactId> <version> 2.9 </version> <configuration> <encoding> UTF-8 </encoding> <version> 6 </version> </configuration> </plugin> <plugin> <groupId> org.apache.maven.plugins </groupId> <artifactId> maven-compiler-plugin </artifactId> <version> 3.1 </version> <configuration> <source> 1.6 </source> <target> 1.6 </target> </configuration> </plugin> <plugin> <groupId> com.ibm.websphere.wlp.maven.plugins </groupId> <artifactId> liberty-maven-plugin </artifactId> <version> 1.1 </version> <extensions> true </extensions> <configuration> <serverHome> /opt/dev/wlp </serverHome> </configuration> </plugin> </plugins> </build> </project>
          Hide
          Matt Sicker added a comment -

          Oh, almost forgot, my test servlet:

          package org.mjunx;
          
          import org.apache.logging.log4j.LogManager;
          import org.apache.logging.log4j.Logger;
          
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import java.io.IOException;
          import java.io.PrintWriter;
          
          /**
           * @author msicker
           * @version 1.0.0
           */
          public class LogServlet extends HttpServlet {
              private static final Logger logger = LogManager.getLogger();
              protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                  response.setContentType("text/plain");
                  PrintWriter out = response.getWriter();
                  String message = request.getParameter("message");
                  out.print("Logging INFO: ");
                  out.println(message);
                  logger.info(message);
                  out.close();
              }
          }
          
          Show
          Matt Sicker added a comment - Oh, almost forgot, my test servlet: package org.mjunx; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * @author msicker * @version 1.0.0 */ public class LogServlet extends HttpServlet { private static final Logger logger = LogManager.getLogger(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/plain" ); PrintWriter out = response.getWriter(); String message = request.getParameter( "message" ); out.print( "Logging INFO: " ); out.println(message); logger.info(message); out.close(); } }
          Hide
          Nick Williams added a comment -

          Fixed with r1565858 and r1565878. The database appenders now connect to the database (i.e., borrow from the connection pool) and begin a transaction on every flush or every non-buffered writeInternal, then commit and disconnect (i.e., return to pool) at the end of the flush or non-buffered writeInternal.

          Note also that the <DriverManager...> connection source plugin is no longer available. It was removed because it was unsafe and didn't support connection pooling. Please use the <DataSource...> or <ConnectionFactory...> connection source plugins, instead.

          Show
          Nick Williams added a comment - Fixed with r1565858 and r1565878. The database appenders now connect to the database (i.e., borrow from the connection pool) and begin a transaction on every flush or every non-buffered writeInternal , then commit and disconnect (i.e., return to pool) at the end of the flush or non-buffered writeInternal . Note also that the <DriverManager...> connection source plugin is no longer available. It was removed because it was unsafe and didn't support connection pooling. Please use the <DataSource...> or <ConnectionFactory...> connection source plugins, instead.

            People

            • Assignee:
              Nick Williams
              Reporter:
              Barış Taşkend
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development