Logging on a shared Java hosting with java.util.logging

JavaLoggingLoadI recently deployed a Java web application on a shared hosting server. I faced a number of problems that made it obvious why Java is struggling when it comes to shared hosting. As the ‘E’ in J2EE stands for enterprise, I guess the creators have banked on enterprises always having dedicated servers and not deploying on a shared environment.

In this article, I will take up the problems we encountered with J2SE logging and the fix we came up with.

Since Java introduced logging in version 1.4, many developers have adopted java.util.logging instead of using a third party API like Log4J or even Commons Logging. Feature wise, J2SE logging looks good enough and I did not face any issues while I was deploying applications on a dedicated server and had control over the JVM settings. However once I moved to a shared hosting, I discovered a serious limitation of JDK / J2SE Logging, an inability to have per-web application logging. All the logging configurations are at a virtual machine level and in a shared hosting machine, the host does not give you control over the virtual machine configuration.

Logging that worked fine on my development machine as well as a dedicated server just went dead on the shared hosting. With reference to the java.util.logging javadocs and other resources online, me and my hosting provider tried to find a way around the problem. We tried using various FileHandler settings, the %h pattern is supposed to create the log files in the “user.home”. But that didn’t work as I was told by my host that “user.home” is creating the files in unexpected places, as the value for user.home kept changing. I did not explore further. If anybody knows what might be the reason, pls add a comment.

Modifying the code and using log4j or similar was not an option available. The solution that I found thanks to some help on forums online, was to not directly use Logger.getLogger(“classname”) in my classes, but replace the default LogManager with my own and create the logger I required.

Below is a simple java application that shows what needs to be done. You can make your method configurable by defining the parameters in a properties or XML file instead of hard coding them as I have.

package demo;
import java.util.logging.*;

/**
* @author Harshad Oak
*/
public class LoggerDemo {

public static void main(String[] args) throws Exception {

Logger log= logger(“demo.LoggerDemo”);
log.info(“A TEST INFO MSG”);
log.warning(“A TEST WARNING MSG”);

}

public static Logger logger(String loggingResourceName) throws Exception {

//refer Filehandler javadoc

//limit – the maximum number of bytes to write to any one file
//count – the number of files to use
//”%u – a unique number to resolve conflicts
FileHandler fileHandler=new FileHandler(“log%u.txt”, 500000, 10);

//use SimpleFormatter / XMLFormatter
fileHandler.setFormatter(new java.util.logging.SimpleFormatter()) ;

LogManager lm = LogManager.getLogManager();
Logger logger = Logger.getLogger(loggingResourceName);
//Message levels lower than this value will be discarded.
logger.setLevel(Level.INFO);

logger.addHandler(fileHandler);

//register logger
lm.addLogger(logger);
return logger;

}

}

On running this code, a new log file named log0.txt.1 should get created with the following messages

Feb 16, 2006 4:43:40 PM demo.LoggerDemo main
INFO: A TEST INFO MSG
Feb 16, 2006 4:43:40 PM demo.LoggerDemo main
WARNING: A TEST WARNING MSG

So get the logger using the logger method and log the message at the level you desire. You are free from the JVM level LogManger settings and can configure and use your new LogManager anyway you want to. Have fun and don’t forget to keep a log 🙂

Related:
>> Logging in Java with the JDK 1.4 Logging API and Apache log4j
>> Jakarta Commons Book with Commons Logging gets a 5 out of 5
>> Harshad Oak Books

Content Team

The IndicThreads Content Team posts news about the latest and greatest in software development as well as content from IndicThreads' conferences and events. Track us social media @IndicThreads. Stay tuned!

4 thoughts on “Logging on a shared Java hosting with java.util.logging

Leave a Reply