Browse DevX
Sign up for e-mail newsletters from DevX


Build a Distributed Logging Framework Using Java RMI : Page 4

The new J2SE logging API brings Java developers a variety of ways to perform logging. One thing you can do is to build a logging framework for your distributed and multithreaded Java applications with Java RMI.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Logging by Thread
Another feature you can add to this logging framework is logging by thread. It will be useful when you are writing a multi-threaded Java application and need to track the call flow for a particular thread. This feature is very handy compared to J2SE class logging, especially if the classes used by the thread are not from the same namespace hierarchy.

The following code block creates a logging-by-thread implementation through the setThreadLogLevel() and logByThread() methods of LogServerImpl. The idea is pretty simple: First, you need to assign each thread a namespace based on its own thread name. Then you associate each thread namespace with a logger of the same namespace. By intentionally separating a thread namespace with regular class namespace you get a logger for that particular thread only.

public void setThreadLogLevel(String threadName, Level logLevel) { String tName = THREAD_PREFIX + threadName; Logger logger = (Logger)loggerHash.get(tName); if (logger == null) { logger = Logger.getLogger(tName); loggerHash.put(tName, logger); } if (logLevel.equals(Level.OFF)) { loggerHash.remove(tName); } else { logger.setLevel(logLevel); } } public void logByThread(Level level, String msg) { String tName = THREAD_PREFIX + Thread.currentThread().getName(); Logger logger = (Logger)loggerHash.get(tName); if (logger != null) { logger.log(level, msg); } }

Taken as a whole, the logging framework demonstrated in this article could be useful in debugging your next distributed and multithreaded Java application. There are improvements you can make on this framework. For instance, you could enhance the in-memory log handler to send log records over SOAP to a web server, where an administrator can monitor the system remotely.

With the new J2SE logging API, the outlook for Java application logging is far more positive than it ever has been before.

Guang Yang is the founder and chief architect for Sunwest Technologies, a consulting firm that specializes in Web, e-commerce, and enterprise application development. Guang has been a Java developer since 1996. You can reach him by email at
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date