You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Aleksandar Stoisavljevic <a....@levi9.com> on 2010/09/04 14:31:01 UTC

JDBCAppender problem

Hi all,

I have a requirement to put some logs from my application into DB. My app. is based on PostgreSQL 8.4, so this is the targeted database.

I have Maven2 Java project, and project pom.xml looks like this:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>rs.in.staleksit.
learning</groupId>
  <artifactId>log4j-jdbcappender</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 
  <properties>
      <maven.sources.version>1.6</maven.sources.version>
      <maven.target.version>1.6</maven.target.version>
 
      <log4j.version>1.2.16</log4j.version>
  </properties>
 
 
  <dependencyManagement>
      <dependencies>
          <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>8.4-701.jdbc3</version>
        </dependency>
      </dependencies>
  </dependencyManagement>
 
  <dependencies>
      <dependency>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
      </dependency>
    <dependency>
           <groupId>postgresql</groupId>
           <artifactId>postgresql</artifactId>
    </dependency>
  </dependencies>
  <build>
      <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${maven.source.version}</source>
                <target>${maven.target.version}</target>
                <encoding>UTF-8</encoding>
            </configuration>             
          </plugin>
      </plugins>
  </build>
</project>

So You see I am using Log4j 1.2.16 and PostgreSQL 8.4 dependencies.

I have a Main class like this one:

package rs.in.staleksit.learning.log4j.jdbcappender;

import org.apache.log4j.Logger;

public class MainClass {

    private static final Logger log = Logger.getLogger(MainClass.class);
   
    /**
     * @param args
     */
    public static void main(String[] args) {
        log.info("main method ENTER");
       
        log.info("main method LEAVE");
    }
}

So this is just a sample, as proof of concept.

I've log4j.xml configuration like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
        </layout>   
    </appender>   
    <appender name="DB" class="org.apache.log4j.jdbc.JDBCAppender">
        <param name="URL" value="jdbc:postgresql://localhost:5432/log4jtest"/>                                 
        <param name="Driver" value="org.postgresql.Driver"/>
        <param name="user" value="postgres"/>
        <param name="password" value="postgres"/>
        <param name="sql" value="INSERT INTO logs (user_id, dated, logger, level, message) VALUES ('%x', to_timestamp('%d', 'YYYY-MM-DD HH24:MI:SS,MS'), '%C', '%p', '%m')"/>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="DB"/>
    </root>
</log4j:configuration>

And at last I have created log4jtest DB and one single table LOGS (script of DB creation follows).
CREATE TABLE "LOGS" (
    "USER_ID" character varying(60) NOT NULL,
    "DATED" date NOT NULL,
    "LOGGER" character varying(50) NOT NULL,
    "LEVEL" character varying(10) NOT NULL,
    "MESSAGE" character varying(1000)
);

But when I invoke program start, I am getting following error:

13:41:33,409  INFO MainClass:20 - main method ENTER
log4j:ERROR Failed to excute sql
org.postgresql.util.PSQLException: ERROR: relation "logs" does not exist
  Position: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:353)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:299)
    at org.apache.log4j.jdbc.JDBCAppender.execute(JDBCAppender.java:218)
    at org.apache.log4j.jdbc.JDBCAppender.flushBuffer(JDBCAppender.java:290)
    at org.apache.log4j.jdbc.JDBCAppender.append(JDBCAppender.java:186)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.info(Category.java:666)
    at rs.in.staleksit.learning.log4j.jdbcappender.MainClass.main(MainClass.java:20)
13:41:33,611  INFO MainClass:22 - main method LEAVE

>From stackTrace I see that message is that there is no "logs" relation (table) but I am pretty sure that there is one.

I tried to attach complete maven2 project here and postgreSQL backupscript, but then mail failed.

Any help would be appreciated.

RE: JDBCAppender problem

Posted by Yogesh Rao <Yo...@mphasis.com>.
Hi Aleksandar,

Had a quick look at the config and everything else, they do seem fine...

Can you do the following and get back ?

1. Connect to DB and try manually inserting a record using the username and id specified?
2. i see the table name as "logs" can you try changing this to "LOGS".. some databases can be case sensitive.. i have worked on oracle and it isnt but this will atleast let us know remove a possibility.

Do let us know with your results.

Cheers!
-Yogesh


________________________________________
From: Aleksandar Stoisavljevic [a.stoisavljevic@levi9.com]
Sent: Saturday, September 04, 2010 5:31 AM
To: log4j-user@logging.apache.org
Subject: JDBCAppender problem

Hi all,

I have a requirement to put some logs from my application into DB. My app. is based on PostgreSQL 8.4, so this is the targeted database.

I have Maven2 Java project, and project pom.xml looks like this:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>rs.in.staleksit.
learning</groupId>
  <artifactId>log4j-jdbcappender</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
      <maven.sources.version>1.6</maven.sources.version>
      <maven.target.version>1.6</maven.target.version>

      <log4j.version>1.2.16</log4j.version>
  </properties>


  <dependencyManagement>
      <dependencies>
          <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>8.4-701.jdbc3</version>
        </dependency>
      </dependencies>
  </dependencyManagement>

  <dependencies>
      <dependency>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
      </dependency>
    <dependency>
           <groupId>postgresql</groupId>
           <artifactId>postgresql</artifactId>
    </dependency>
  </dependencies>
  <build>
      <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${maven.source.version}</source>
                <target>${maven.target.version}</target>
                <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
      </plugins>
  </build>
</project>

So You see I am using Log4j 1.2.16 and PostgreSQL 8.4 dependencies.

I have a Main class like this one:

package rs.in.staleksit.learning.log4j.jdbcappender;

import org.apache.log4j.Logger;

public class MainClass {

    private static final Logger log = Logger.getLogger(MainClass.class);

    /**
     * @param args
     */
    public static void main(String[] args) {
        log.info("main method ENTER");

        log.info("main method LEAVE");
    }
}

So this is just a sample, as proof of concept.

I've log4j.xml configuration like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
        </layout>
    </appender>
    <appender name="DB" class="org.apache.log4j.jdbc.JDBCAppender">
        <param name="URL" value="jdbc:postgresql://localhost:5432/log4jtest"/>
        <param name="Driver" value="org.postgresql.Driver"/>
        <param name="user" value="postgres"/>
        <param name="password" value="postgres"/>
        <param name="sql" value="INSERT INTO logs (user_id, dated, logger, level, message) VALUES ('%x', to_timestamp('%d', 'YYYY-MM-DD HH24:MI:SS,MS'), '%C', '%p', '%m')"/>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="DB"/>
    </root>
</log4j:configuration>

And at last I have created log4jtest DB and one single table LOGS (script of DB creation follows).
CREATE TABLE "LOGS" (
    "USER_ID" character varying(60) NOT NULL,
    "DATED" date NOT NULL,
    "LOGGER" character varying(50) NOT NULL,
    "LEVEL" character varying(10) NOT NULL,
    "MESSAGE" character varying(1000)
);

But when I invoke program start, I am getting following error:

13:41:33,409  INFO MainClass:20 - main method ENTER
log4j:ERROR Failed to excute sql
org.postgresql.util.PSQLException: ERROR: relation "logs" does not exist
  Position: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:353)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:299)
    at org.apache.log4j.jdbc.JDBCAppender.execute(JDBCAppender.java:218)
    at org.apache.log4j.jdbc.JDBCAppender.flushBuffer(JDBCAppender.java:290)
    at org.apache.log4j.jdbc.JDBCAppender.append(JDBCAppender.java:186)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.info(Category.java:666)
    at rs.in.staleksit.learning.log4j.jdbcappender.MainClass.main(MainClass.java:20)
13:41:33,611  INFO MainClass:22 - main method LEAVE

>From stackTrace I see that message is that there is no "logs" relation (table) but I am pretty sure that there is one.

I tried to attach complete maven2 project here and postgreSQL backupscript, but then mail failed.

Any help would be appreciated.

Information transmitted by this e-mail is proprietary to MphasiS, its associated companies and/ or its customers and is intended for use only by the individual or entity to which it is addressed, and may contain information that is privileged, confidential or exempt from disclosure under applicable law. If you are not the intended recipient or it appears that this mail has been forwarded to you without proper authority, you are notified that any use or dissemination of this information in any manner is strictly prohibited. In such cases, please notify us immediately at mailmaster@mphasis.com and delete this mail from your records.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org