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).
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:
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:
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:
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:
Free Demo for Corporate & Online Trainings.