项目作者: faiza-aslam

项目描述 :
JDBC Logger
高级语言: Java
项目地址: git://github.com/faiza-aslam/jdbc-logger.git
创建时间: 2018-06-24T12:24:12Z
项目社区:https://github.com/faiza-aslam/jdbc-logger

开源协议:

下载


jdbc-logger

JDBC Logger is a utility app that logs events to database. For this, a custom Handler “EventLoggingHandler” is defined which extends java.util.logging.Handler.
This utility is cross container (i.e. it can be run on different containers). SQL script to setup database and table is also added. The sample to test this functionality is available here.

Clone this repo and execute following command to build this project:

  1. mvn clean install

Tomcat & WildFly configuration is as follows:

Tomcat

  1. Place following jars in %TOMCAT_HOME%/bin

    • jdbc-logger-0.0.1-SNAPSHOT.jar [this utility]
    • c3p0-0.9.5.1.jar
    • commons-dbutils-1.6.jar
    • mchange-commons-java-0.2.10.jar
    • mysql-connector-java-5.1.39-bin.jar
  2. Modify catalina.bat file and add following lines just after :noJuliManager label:
    ```
    set LOGGER=”%CATALINA_HOME%\bin\jdbc-logger-0.0.1-SNAPSHOT.jar”
    set MCHANGE=”%CATALINA_HOME%\bin\mchange-commons-java-0.2.10.jar”
    set C3P0=”%CATALINA_HOME%\bin\c3p0-0.9.5.1.jar”
    set DBUTILS=”%CATALINA_HOME%\bin\commons-dbutils-1.6.jar”
    set MYSQL=”%CATALINA_HOME%\bin\mysql-connector-java-5.1.39-bin.jar”

set “CLASSPATH=%CLASSPATH%;%LOGGER%;%MCHANGE%;%C3P0%;%DBUTILS%;%MYSQL%”

  1. This is because these jars need to be loaded before tomcat loads logging configuration.
  2. 3. Add following lines in logging.properties file placed at %TOMCAT_HOME%/conf

handlers= …., com.examples.logging.EventLoggingHandler

com.examples.logging.EventLoggingHandler.level = ALL
com.examples.logging.EventLoggingHandler.driverClassName = com.mysql.jdbc.Driver
com.examples.logging.EventLoggingHandler.jdbcUrl = jdbc:mysql://localhost:3306/test?useSSL=false&autoReconnect=true
com.examples.logging.EventLoggingHandler.username = root
com.examples.logging.EventLoggingHandler.password = root
com.examples.logging.EventLoggingHandler.insertStatement = insert into user_event_log values (?,?,?,?,?,?,?)

CustomEventLogger.handlers = com.examples.logging.EventLoggingHandler
CustomEventLogger.level = ALL

  1. ------
  2. ### WildFly
  3. 1. Create global module for this logger jar. For this, create directory structure as:
  4. %WILDFLY_HOME%/modules/com/examples/logging/main
  5. - Place following jars in this directory. The jars can be found from %USER_PROFILE%\.m2\repository\com\mchange\
  6. * jdbc-logger-0.0.1-SNAPSHOT.jar [this utility]
  7. * c3p0-0.9.5.1.jar
  8. * mchange-commons-java-0.2.10.jar
  9. - Place module.xml (included in this repo) in above location. Add following lines in standalone.xml in domain:ee subsystem before <spec-descriptor-property-replacement> element:
  1. <global-modules>
  2. <module name="com.examples.logging" slot="main"></module>
  3. </global-modules>
  4. ```
  1. Add 2 handlers [“ASYNC”, “DB”] and logger in standalone.xml in logging subsystem:

    1. <async-handler name="ASYNC">
    2. <level name="ALL"></level>
    3. <queue-length value="1024"></queue-length>
    4. <overflow-action value="block"></overflow-action>
    5. <subhandlers>
    6. <handler name="DB"></handler>
    7. </subhandlers>
    8. </async-handler>
    9. .....
    10. <custom-handler name="DB" class="com.examples.logging.EventLoggingHandler" module="com.examples.logging">
    11. <level name="ALL"></level>
    12. <formatter>
    13. <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"></pattern-formatter>
    14. </formatter>
    15. <properties>
    16. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    17. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useSSL=false&autoReconnect=true"></property>
    18. <property name="username" value="{username}"></property>
    19. <property name="password" value="{password}"></property>
    20. <property name="insertStatement" value="insert into user_event_log values (?,?,?,?,?,?,?)"></property>
    21. </properties>
    22. </custom-handler>
    23. .....
    24. <logger category="CustomEventLogger">
    25. <level name="ALL"></level>
    26. <handlers>
    27. <handler name="ASYNC"></handler>
    28. </handlers>
    29. </logger>
  2. Add module for commons-dbutils as follows:

    • create directory structure -> %WILDFLY_HOME%\modules\system\layers\base\org\apache\commons\dbutils\main
    • place jar from %USER_PROFILE%.m2\repository\commons-dbutils\commons-dbutils\1.6 to this directory
    • add module.xml file with following content:
      1. <module xmlns="urn:jboss:module:1.3" name="org.apache.commons.dbutils">
      2. <resources>
      3. <resource-root path="commons-dbutils-1.6.jar" ></resource-root>
      4. </resources>
      5. <dependencies>
      6. <module name="javax.sql.api"></module>
      7. </dependencies>
      8. </module>
  3. Add module for mysql as follows:

    • create directory structure -> %WILDFLY_HOME%\modules\system\layers\base\com\mysql\driver\main
    • place jar from %USER_PROFILE%.m2\repository\mysql\mysql-connector-java to this directory
    • add module.xml file with following content:
      1. <module xmlns="urn:jboss:module:1.3" name="com.mysql.driver">
      2. <resources>
      3. <resource-root path="mysql-connector-java-5.1.39-bin.jar" ></resource-root>
      4. </resources>
      5. <dependencies>
      6. <module name="javax.api"></module>
      7. <module name="javax.transaction.api"></module>
      8. </dependencies>
      9. </module>

That’s it.. Your logger is ready to log into your database.