Experience indicates that logging is an important component of the development cycle. It offers several advantages. It provides precise context about a run of the application. Once inserted into the code, the generation of logging output requires no human intervention. Moreover, log output can be saved in persistent medium to be studied at a later time. In addition to its use in the development cycle, a sufficiently rich logging package can also be viewed as an auditing tool. Inserting log statements into code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is usually the case for multithreaded applications and distributed applications at large. But logging has some drawbacks. It can slow down an application. If too verbose, it can cause scrolling blindness.

logoLog4j is an open source tool developed for putting log statements into your application and it avoids the issues above. It is reliable, fast and extensible Java logging framework. Log4j has three main components: loggers, appenders and layouts. These three types of components work together to enable you to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported. With log4j it is possible to enable logging at runtime without modifying the application binary. It is designed so that log statements can remain in shipped code without incurring a high performance cost. It follows that the speed of logging (or rather not logging) is capital. Log output can be so voluminous that it quickly becomes overwhelming. One of the distinctive features of log4j is the notion of hierarchical loggers. Using loggers it is possible to selectively control which log statements are output at arbitrary granularity.

Today we'll show you how to store your Jelastic logs in the database (or separate file) using log4j utility for their further downloading. So, let's get started!

Create the environment

1. Go to jelastic.com and sign up if you haven't done it yet or log in with your Jelastic credentials by clicking the Sign In link on the page.

2. Ask Jelastic to create a new environment.

3. In the opened Environment topology dialog, pick Tomcat 7 as your application server, specify the resource limits (e.g. 16 cloudlets) and select MySQL as the database you want to use. Then type your environment name, for example, jelasticlog4j and click Create.

log4j1

In a minute the environment with Tomcat 7 and MySQL will be created.

log4j2

Upload Java package

*Note that our java class log4jJelastic.java is just an example which describes how to perform trivial logging using log4j framework. In your case you can use log actions as you wish. Here is our example:

package com;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4jJelastic {
   static Logger logger = Logger.getLogger(
                       Log4jJelastic.class.getName());
   public static void main(String[] args){
      PropertyConfigurator.configure("log4j.properties");
      BasicConfigurator.configure();
      logger.debug("this is debug");
      logger.info("Sample info message");
      logger.warn("Sample warn message");
      logger.error("Sample error message");
      logger.fatal("Sample fatal message");
      logger.info("Hello this is an info message");
   }
}

1. Upload your war file to the Deployment manager.

log4j3

2. Once the package is in Jelastic, deploy it to the environment you have just created.

log4j4

In our case, we use simple Java application to demonstrate how to work with log4j.

Configure database

1. Once the deployment is finished, click Open in browser button for MySQL.

log4j5

2. When you created the environment, Jelastic sent you an email with credentials to the database. Create an account and the database with the application using these credentials.

log4j6

log4j7

3. Navigate to the database you have just created and run the following scheme in order to create logs table:

CREATE TABLE LOGS
(LOGGER  VARCHAR(50) NOT NULL,
 LEVEL   VARCHAR(10) NOT NULL,
 MESSAGE VARCHAR(1000) NOT NULL
);

log4j10

Configure application

1. Click the config button for Tomcat and navigate to the home folder.

2. Create log4j.properties file and add there the following parameters. In this file we set Appender: JDBCAppender which stores logs in database (table 'logs' in our case):

# Define the root logger with appender file
log4j.rootLogger = DEBUG, DB
# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
# Set JDBC URL
log4j.appender.DB.URL=jdbc:mysql://mysql-{your_env_name}.{hoster_domain}/{db_name}
# Set Database Driver 
log4j.appender.DB.driver=com.mysql.jdbc.Driver
# Set database user name and password 
log4j.appender.DB.user=username
log4j.appender.DB.password=password
# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO LOGS VALUES('%C','%p','%m')
# Define the layout for file appender 
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

3. Navigate to the server folder and add to your common loader parameter the next path:

$catalina.home/temp

log4j14

4. This step is optional. You have to upload mysql connector and log4j library to the lib folder of Tomcat only in case if they are not included to your war package.

log4j11

5. Save all the changes and restart Tomcat.

log4j12

Now you can open your application in a web browser.

log4j-start-page-screenshot

Let's navigate to our database to ensure that our logs have been stored there.

log4j16

In case if you want to store your logs in a separate file, your log4j.properties file has to look like the following:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=${catalina.base}/logs/log4j-jelastic.log
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

That's all for today! As you can see you can store your Jelastic's logs wherever you want using log4j framework. Please let us know what other tools you are using for logging in the comments below. Thank you for attention!