Java J2EE Portal
Enterprise Java Station
J2EE curve
Java News / Articles
Xml Security using Xml Encryption and Xml Digital Signature
Cryptographic Algorithms - Impact On Application Performance
Offline Ajax Applications Using Google Gears
Processing...
Buy Java, Deals On Software Technology Store
Click here for great deals on computers, laptops, software and books
Logging on a shared Java hosting with java.util.logging PDF Print
Written by Harshad Oak   
Feb 16, 2006 at 11:19 AM

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


User Comments

Security Check. Please enter this code Listen to code


Add This Feed Button

Enter your Email
Java News / Articles
Java Expert Interviews
MarkSchiefelbein-BackbaseAjaxFramework
All Ajax development can happen serverside using the Backbase framework and JSF
AndreyGrebnev
Use a spade to dig a hole - Use a bulldozer to dig a trench
RubyOnRailscreatorDavidHeinemeierHansson
Let Java retire from the spotlight of web applications in dignity
Processing...
Go to top of page  Home |
SiteMap

Copyright 2004 to 2008 Rightrix Solutions. All rights reserved. All product names are trademarks of their respective companies. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Rightrix Solutions and IndicThreads.com are independent of Sun Microsystems, Inc.

Views expressed at IndicThreads.com reflect the views of the authors alone, and do not necessarily reflect those of IndicThreads.com. IndicThreads.com and it's authors are not responsible for reader comments and opinions.

Enterprise Java J2EE JEE Portal >> IndicThreads.com