将Log4J viewer(电锯)连接到MySql数据库

时间:2011-06-02 20:46:11

标签: java log4j log4net log4net-configuration

我想使用log4j viewer(Chainsaw)来读取log4j记录在MySql数据库中的错误日志。由于文档很少,我有点挣扎。

[strike]这是我对Chainsaw的暂定.xml配置:[/ strike] 这是我的新.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
  <plugin name="CustomDBReceiver" class="org.apache.log4j.db.CustomSQLDBReceiver">
     <connectionSource class="org.apache.log4j.db.DriverManagerConnectionSource">
       <param name="password" value="my_pwd"/>
       <param name="user" value="my_uid"/>
       <param name="driverClass" value="org.gjt.mm.mysql.Driver"/>
       <param name="url" value="jdbc:mysql://<my_host>:<my_host_port>/<my_database>"/>
  </connectionSource>
      <param name="refreshMillis" value="1000"/>
  <param name="sql" value='select ID as ID, Logger as LOGGER, Date as TIMESTAMP, Level as LEVEL, Thread as THREAD, Message as MESSAGE, Exception as EXCEPTION, DestID as DESTID, IPAddress as IP, Stacktrace as STACKTRACE from errorlogs'/>
  <param name="IDField" value="ID"/>
  </plugin>
  <root>
    <level value="debug"/>
  </root>
</log4j:configuration>

它告诉我:

No suitable driver found for jdbc:mysql://<my_host>:<my_host_port>/<my_database>

我已经下载了DBReceiver扩展(log4j-db-1.3alpha-7)并将其放在我的.chainsaw / plugins目录中。我还在.chainsaw目录中放了“mysql-connector-java-5.1.16-bin”,以确保它,因为它似乎是问题的原因。但是,它没有解决它。

你们有没有人知道如何将Chainsaw连接到MySql数据库?

谢谢!

2 个答案:

答案 0 :(得分:3)

有关如何使用未与Chainsaw分发的jar的更多信息:

http://logging.apache.org/chainsaw/distributionnotes.html

顺便说一下,您可能想要尝试Chainsaw的最新开发者快照,可在此处获取:

http://people.apache.org/~sdeboy

很多新功能......

斯科特

答案 1 :(得分:1)

以下是配置Chainsaw 2.1以阅读使用DBAppender保存的Logback日志的方法。

  1. 请勿下载任何插件
  2. 下载SQL驱动程序(例如mysql-connector-java-5.1.36-bin.jar)并将其保存到<chainsaw>\repo
  3. 编辑启动脚本以将jar添加到类路径中。例如,在chainsaw.bat中使用:
      

    设置CLASSPATH =“%BASEDIR%”\ etc;“%REPO%”\ log4j \ apache-log4j-extras \ 1.1 \ apache-log4j-extras-1.1.jar;“%REPO%”\ log4j \ log4j \ 1.2.16 \ log4j的-1.2.16.jar; “%REPO%” \的javax \ jmdns \ jmdns \ 3.4.1 \ jmdns-3.4.1.jar; “%REPO%” \ XStream的\ XStream的\ 1.1.2 \ XSTREAM-1.1.2.jar; “%REPO%” \公地VFS \公地VFS \ 1.0 \公地VFS-1.0.jar; “%REPO%” \共享记录\共享记录\ 1.1.1 \共享记录-1.1.1.jar; “%REPO%” \ COM \ jcraft \ jsch \ 0.1.42 \ jsch-0.1.42.jar; “%REPO%” \ log4j的\ Apache的链锯\ 2.1.0-快照\ Apache的链锯-2.1.0-SNAPSHOT.jar;的 “%REPO%” \ MySQL的连接器的Java-5.1.36-bin.jar

  4. 启动时选择“使用Chainsaw配置文件”
  5. 这是与Logback DBAppender兼容的配置文件:

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    
        <plugin name="Logs" class="org.apache.log4j.db.CustomSQLDBReceiver">
            <param name="sql" 
                   value="
                        SELECT 
                            logger_name AS 'LOGGER', 
                            from_unixtime (timestmp/1000) AS 'TIMESTAMP', 
                            level_string AS 'LEVEL', 
                            thread_name AS 'THREAD', 
                            formatted_message AS 'MESSAGE', 
                            (SELECT CONCAT('{{', group_concat(CONCAT(mapped_key, ',', mapped_value) SEPARATOR ','), '}}')
                                FROM logging_event_property
                                WHERE logging_event.event_id = logging_event_property.event_id 
                                GROUP BY logging_event_property.event_id) AS 'NDC', 
                            '' AS 'MDC',
                            caller_class AS 'CLASS', 
                            caller_method AS 'METHOD', 
                            caller_filename AS 'FILE', 
                            caller_line AS 'LINE',
                            CONCAT('{{application,test,hostname,', (SELECT mapped_value FROM logging_event_property WHERE logging_event.event_id = logging_event_property.event_id AND mapped_key = 'HOSTNAME'), ',log4jid,', event_id,'}}') AS 'PROPERTIES',
                            IFNULL((SELECT group_concat(trace_line ORDER BY i ASC SEPARATOR '\n')
                                FROM logging_event_exception 
                                WHERE logging_event.event_id = logging_event_exception.event_id 
                                GROUP BY logging_event_exception.event_id),'') AS 'THROWABLE'
                        FROM logging_event
                        WHERE 1=1"/>
            <connectionSource class="org.apache.log4j.db.DriverManagerConnectionSource">
                <param name="driverClass" value="<driver, eg com.mysql.jdbc.Driver>"/>
                <param name="url" value="jdbc:mysql://<url>/<schema>"/>
                <param name="user" value="<user>"/>
                <param name="password" value="<password>"/>
            </connectionSource>
            <param name="IDField" value="event_id"/>
            <param name="refreshMillis" value="3000"/>
        </plugin>
    
        <root>
            <level value="debug"/>
        </root>
    
    </log4j:configuration>
    

    select-in-select和where 1=1可以解决CustomSQLDBReceiver在SQL表达式末尾笨拙地连接WHERE event_id > -1AND event_id > -1的行为。 (See lines 306-313.

    CustomSQLDBReceiver的文档位于its javadoc

    对于Logback-Access(在带有ELB的EC2上),我使用:

    <plugin name="Access-Logs" class="org.apache.log4j.db.CustomSQLDBReceiver">
        <param name="sql" 
               value="
                    SELECT 
                        REPLACE (requestURI, '/', '.') AS 'LOGGER', 
                        from_unixtime (timestmp/1000) AS 'TIMESTAMP', 
                        'INFO' AS 'LEVEL', 
                        IFNULL((SELECT header_value FROM access_event_header WHERE access_event.event_id = access_event_header.event_id AND header_key = 'x-forwarded-for'),'') AS 'THREAD', 
                        requestURL AS 'MESSAGE', 
                        '' AS 'NDC', 
                        '' AS 'MDC',
                        '' AS 'CLASS', 
                        '' AS 'METHOD', 
                        '' AS 'FILE', 
                        '' AS 'LINE', 
                        CONCAT('{{application,test,hostname,access,log4jid,', event_id,'}}')  AS 'PROPERTIES',
                        (SELECT group_concat(concat(header_key, ': ', header_value) SEPARATOR '\n')
                            FROM access_event_header
                            WHERE access_event.event_id = access_event_header.event_id 
                            GROUP BY access_event_header.event_id) AS 'THROWABLE'
                    FROM access_event
                    WHERE (SELECT header_value 
                            FROM access_event_header 
                            WHERE access_event.event_id = access_event_header.event_id AND header_key = 'user-agent')
                        != 'ELB-HealthChecker/1.0'"/>
        <connectionSource class="org.apache.log4j.db.DriverManagerConnectionSource">
            <param name="driverClass" value="com.mysql.jdbc.Driver"/>
            <param name="url" value=""/>
            <param name="user" value=""/>
            <param name="password" value=""/>
        </connectionSource>
        <param name="IDField" value="access_event.event_id"/>
        <param name="refreshMillis" value="3000"/>
    </plugin>