Attempting to load PostgreSQL driver with Log4j throws ClassNotFoundException in EAP 6
Issue
- When attempting to startup a service with
log4jthat outputs to aPostgreSQLdatabase, the log messages does not write to the database, and the Log4j configuration will fail with aClassNotFoundException. The EAP server will also sometimes fail to deploy the service. The back trace of theClassNotFoundExceptionis the following:
ERROR : log4j:ERROR Failed to load driver
ERROR : java.lang.ClassNotFoundException: org.postgresql.Driver from [Module "org.jboss.log4j.logmanager:main" from local module loader (finder: local module finder)]
ERROR : at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:213)
ERROR : at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:459)
ERROR : at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:408)
ERROR : at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:389)
ERROR : at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:134)
ERROR : at org.apache.log4j.config.PropertySetter.setProperty(PropertySetter.java:245)
ERROR : at org.apache.log4j.config.PropertySetter.setProperty(PropertySetter.java:204)
ERROR : at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:169)
ERROR : at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:104)
ERROR : at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:809)
ERROR : at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
ERROR : at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
ERROR : at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)
ERROR : at org.jboss.as.logging.LoggingDeploymentUnitProcessor.configure(LoggingDeploymentUnitProcessor.java:306)
ERROR : at org.jboss.as.logging.LoggingDeploymentUnitProcessor.processDeploymentLogging(LoggingDeploymentUnitProcessor.java:177)
ERROR : at org.jboss.as.logging.LoggingDeploymentUnitProcessor.deploy(LoggingDeploymentUnitProcessor.java:82)
ERROR : at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:159)
ERROR : at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1980)
ERROR : at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1913)
- The
log4j.propertiesfile we are using is the following:
# Define the DB appender
log4j.appender.DB=org.apache.log4j.DBAppender
#java:jboss/datasources/PostgresDS
# Set JDBC URL
log4j.appender.DB.ConnectionSource=org.apache.log4j.receivers.db.JNDIConnectionSource
log4j.appender.DB.ConnectionSource.JndiLocation=java:jboss/postgresDS
log4j.appender.DB.URL=jdbc:postgresql://localhost:<port-no>/testdb
jdbc:postgresql://localhost:<port_no>/testdb
# Set Database Driver
log4j.appender.DB.driver=org.postgresql.Driver
# Set database user name and password
log4j.appender.DB.user=*****
log4j.appender.DB.password=****
# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO <table_name> VALUES(' ',' ',' ',' ',' ')
# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.appender.sql=TestAppender
log4j.appender.sql.sql=INSERT INTO <table_name> (<col1>, <col2>, <col3>, <col4>) VALUES ('','','','')
log4j.appender.sql.jndiDataSource=java:jboss/postgresDS
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
- Our
pom.xmlfile includes the following as a dependency:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
- The data-source configuration file we are using is the following:
</datasource>
<datasource jndi-name="java:jboss/datasources/PostgresDS"
pool-name="PostgresDS" enabled="true" use-java-context="true">
<connection-url><url> </connection-url>
<driver>postgresql</driver>
<security>
<user-name>****</user-name>
<password>****</password>
</security>
</datasource>
- We've also made sure that the
postgresql-9.4-1201.jdbc4.jarfile is inside theJBOSS_HOME\modules\org\postgresql\main, JBOSS_HOME\modules\system\layers\base\org\postgresql\main, and JBOSS_HOME\standalone\libdirectories. We've verified that the Log4j configuration does work with the database if it's run with a local Java application. The expectation is to have Log4j initialize on the JBoss EAP server without exceptions and to write logging to a PostgreSQL database.
Environment
- Red Hat JBoss Enterprise Application Platform
- 6.4.0.GA
- Log4j
- PostgreSQL 9.4
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
