English 中文(简体)
java wrapper around log4j logger and java.util.logging
原标题:

I am writing a library. The library can be used by applications that use log4j loggers and java.util.logging loggers.

So, I wrote a quick wrapper class, that encapsulates both loggers. I allow the application to set one or both loggers. And in my library I use the encapsulated class to print to either logger.

My question is, since many threads can simultaneously be using the same instance of the wrapper class to log messages using the class methods (for example: fatal() below), what steps should be taken to make these methods thread safe?

public class MultiLogger {
    private static org.apache.log4j.Logger _log4jLogger = null;
    private static java.util.logging.Logger _javaUtilLogger = null;

    private MultiLogger () {
    }

    // log4j FATAL, log util SEVERE
    public void fatal (Object message) {
        if (_log4jLogger != null) {
            _log4jLogger.log("", Level.FATAL, message, null);
        }

        if (_javaUtilLogger != null) {
            _javaUtilLogger.severe((String) message);
        }
    }
    ...
}

Any other comments appreciated too.

最佳回答

See my comment:

You should check out slf4j before you proceed with this library. It wraps all the major logging libraries and is very high quality.

But I presume that your wrapper class will be used in the same manner as the loggers that it wraps. In that case, the wrapped classes should handle the synchronization for you.

问题回答

Option 1: slf4j, as per comment on question.

Option 2: cxf.apache.org has such a device in it. We use it instead of slf4j because slf4j lacks internationalization support. You are welcome to grab the code.

Provided that you re just using log4j and util Loggers as shown above, I don t think you ll run into any sync problems.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签