Mindmajix

Custom handlers

Custom handlers 

So far, we have seen just a few basic log handlers, which are usually included in your server configuration. If you need a more advanced approach to your logs, you can define a custom logging handlers. In order to add a custom handler, you need to define class that extends the java.util.logging.Handler interface and overrides its abstract methods. For example, this class named JdbcLogger is used to write the logs on a database storage (full code is available at: http://community.jboss.org/wiki/CustomLogHandlersOn701).

public class JdbcLogger extends Handler{

@Override

public void publish(LogRecord record){

try{

insertRecord(record);     }

catch (SQLException e)  {

e.printStackTrace();

}

}

@Override

public void flush() {     . . . .

}

@Override

public void close() {     . . . .    }

}

Once compiled, this class needs to be packaged in an archive (for example, logger.jar) and installed as a module into the application server. We will name the module com.JDBCLogger, which requires the following structure under the modules folder:
Screenshot_28

The label Path to be created shows off the directory structure under which we will place the logger.jar archive and its configuration file (module.xml), which follows here:

<module xmlns=”urn:jboss:module:1.0″ name=”com.JDBCLogger”>

<resources>

<resource-root path=”logger.jar”/>

</resources>   <dependencies>

<module name=”javax.api”/>

<module name=”org.jboss.logging”/>

<module name=”com.mysql”/>

</dependencies> </module>

Please notice that this module has a dependency on another module named com.mysql. In the next chapter, we will show how to connect to a database by installing the appropriate module.

We are almost done. Now, insert the handler in the logging subsystem, which contains within its properties the database connection strings and the statement that will be used to insert logs into the database:

<custom-handler name=”DB” class=”com.sample.JdbcLogger” module=”com.JDBCLogger”>

<level name=”INFO”/>

<formatter>

<pattern-formatter pattern=”%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n”/>

</formatter>

<properties>

<property name=”driverClassName” value=”com.mysql.jdbc.Driver”/>

<property name=”jdbcUrl” value=”jdbc:mysql://localhost:3306/mydb”/>

<property name=”username” value=”root”/>

<property name=”password” value=”admin”/>

<property name=”insertStatement” value=”insert into log_table values (?, $TIMESTAMP, $LEVEL, $MDC[ip], $MDC[user], $MESSAGE, hardcoded)”/>

</properties>

</custom-handler>

<root-logger>

<level name=”INFO”/>

<handlers>

<handler name=”CONSOLE”/>

<handler name=”FILE”/>

<handler name=”DB”/>

</handlers>

</root-logger>

The new handler, named DB, is enlisted in the root-logger to collect all logging statements that have a priority of INFO or above. Before testing the logger, don’t forget to create the required tables on your MySql database:

CREATE TABLE log_table(

id INT(11) NOT NULL AUTO_INCREMENT,

`timestamp` VARCHAR(255) DEFAULT NULL,

level VARCHAR(255) DEFAULT NULL,

mdc_ip VARCHAR(255) DEFAULT NULL,

mdc_user VARCHAR(255) DEFAULT NULL,

message VARCHAR(1500) DEFAULT NULL,

hardcoded VARCHAR(255) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE = INNODB

AUTO_INCREMENT = 1

If you have carefully followed all the required steps, you will notice that the log_table contains the logging events that have been triggered since server startup:

Screenshot_29


0 Responses on Custom handlers"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.