Cloud Logging with Log4j

By | December 10, 2012

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 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.


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


Upload Java package

*Note that our java class 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(

   public static void main(String[] args){


      logger.debug("this is debug");"Sample info message");
      logger.warn("Sample warn message");
      logger.error("Sample error message");
      logger.fatal("Sample fatal message");"Hello this is an info message");


1. Upload your war file to the Deployment manager.


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


In our case, we use simple Java application to demonstrate how to work with log4j. You can download it here.

Configure database

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


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.



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



Configure application

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

2. Create 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
# Set Database Driver 
# Set database user name and 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 

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



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.


5. Save all the changes and restart Tomcat.


Now you can open your application in a web browser.


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


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

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
# Set the name of the file
# Set the immediate flush to true (default)
# Set the threshold to debug mode
# Set the append to false, overwrite
# Define the layout for file appender

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!

Subscribe to get the latest updates