log4j2的SQL异常

时间:2013-12-04 04:36:18

标签: java mysql logging jdbc log4j2

每当我尝试使用JDBC appender运行log4j2时,我都会遇到java sql异常

2013-12-03 22:07:32,610 ERROR No matching driver found for database URL [jdbc:mysql://localhost:3306/minecraft]. java.sql.SQLException: No suitable driver
        at java.sql.DriverManager.getDriver(DriverManager.java:278)
        at org.apache.logging.log4j.core.appender.db.jdbc.DriverManagerConnectionSource.createConnectionSource(DriverManagerConnectionSource.java:88)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.logging.log4j.core.config.BaseConfiguration.createPluginObject(BaseConfiguration.java:841)
        at org.apache.logging.log4j.core.config.BaseConfiguration.createConfiguration(BaseConfiguration.java:595)
        at org.apache.logging.log4j.core.config.BaseConfiguration.createConfiguration(BaseConfiguration.java:587)
        at org.apache.logging.log4j.core.config.BaseConfiguration.createConfiguration(BaseConfiguration.java:587)
        at org.apache.logging.log4j.core.config.BaseConfiguration.doConfigure(BaseConfiguration.java:244)
        at org.apache.logging.log4j.core.config.BaseConfiguration.start(BaseConfiguration.java:142)
        at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:339)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:378)
        at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:149)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:85)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:34)
        at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:387)
        at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:322)
        at net.minecraft.server.MinecraftServer.<clinit>(SourceFile:47)

2013-12-03 22:07:32,617 ERROR Unable to invoke method createAppender in class org.apache.logging.log4j.core.appender.db.jdbc.JDBCAppender for element JDBC java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.logging.log4j.core.config.BaseConfiguration.createPluginObject(BaseConfiguration.java:841)
        at org.apache.logging.log4j.core.config.BaseConfiguration.createConfiguration(BaseConfiguration.java:595)
        at org.apache.logging.log4j.core.config.BaseConfiguration.createConfiguration(BaseConfiguration.java:587)
        at org.apache.logging.log4j.core.config.BaseConfiguration.doConfigure(BaseConfiguration.java:244)
        at org.apache.logging.log4j.core.config.BaseConfiguration.start(BaseConfiguration.java:142)
        at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:339)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:378)
        at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:149)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:85)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:34)
        at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:387)
        at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:322)
        at net.minecraft.server.MinecraftServer.<clinit>(SourceFile:47)
Caused by: java.lang.NullPointerException
        at org.apache.logging.log4j.core.appender.db.jdbc.JDBCAppender.createAppender(JDBCAppender.java:80)
        ... 17 more

2013-12-03 22:07:32,619 ERROR Null object returned for JDBC in Appenders
2013-12-03 22:07:32,625 ERROR Unable to locate appender databaseAppender for logger

我的类路径是

$ echo $CLASSPATH
.:/home/user/minecraft/mysql-jdbc.5.1.23.jar

以下是我正在使用的log4j2 xml配置文件的内容

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
        <Appenders>

                <Console name="console" target="SYSTEM_OUT">
                        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%level] %msg%n" />
                </Console>

                <RollingFile name="rolling_server_log" fileName="logs/server.log"
                                filePattern="logs/server_%d{yyyy-MM-dd}-%i.log.gz">
                        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%level] %msg%n" />
                        <Policies>
                                <TimeBasedTriggeringPolicy />
                        </Policies>
                </RollingFile>

                <JDBC name="databaseAppender" tableName="minecraft">
                        <DriverManager url="jdbc:mysql://localhost:3306/minecraft" username="xxxx" password="xxxx" />
                        <Column name="eventDate" isEventTimestamp="true" />
                        <Column name="level" pattern="%level" />
                        <Column name="logger" pattern="%logger" />
                        <Column name="message" pattern="%message" />
                </JDBC>

        </Appenders>
        <Loggers>
                <Root level="info">
                        <AppenderRef ref="rolling_server_log" />
                        <AppenderRef ref="console" />
                        <AppenderRef ref="databaseAppender"/>
                </Root>
        </Loggers>
</Configuration>

我是JDBC安装/ log4j2集成的新手。是否有一些我缺少的配置步骤?

2 个答案:

答案 0 :(得分:1)

看起来像是一个类路径问题。假设启动应用程序的命令看起来像“java -cp jar1.jar:jar2.jar:jar3.jar myMainClass”,您是否尝试在jars列表中包含jdbc驱动程序类?

答案 1 :(得分:0)

CLASSPATH出现错误。

  

CLASSPATH变量是一种告诉应用程序的方法,包括   JDK工具,在哪里查找用户类。

http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

因此,您的CLASSPATH应该只有/home/user/minecraft/。只需确保驱动程序jar位于此目录中。