简体   繁体   中英

Is it possible to use perf4j without log4j?

I'm writing a library and would like to use perf4j mainly to expose JMX performance counters.
According to the instructions here http://perf4j.codehaus.org/devguide.html perf4j is meant to be used in conjunction with log4j, however since I'm writing a library I have no control over log4j usage, users of the library may or may not wish to use log4j and I don't want to make this a requirement.
Is there a way to use perf4j, specifically expose perf4j JMX counters programatically without requiring use of log4j?

My answer is more or less. Perf4J needs to output its measurements somewhere and this somewhere is a logging framework. Perf4J is built to work with Log4J compatible frameworks, but you can change the underlying logging framework. For example you can choose to use SFL4J or Commons Logging instead.

Here is a sample that depends only on perf4j's jar:

import org.perf4j.LoggingStopWatch;
import org.perf4j.StopWatch;

public class Perf4JEx {
    public static void main (String[] args) {
        StopWatch stopWatch = new LoggingStopWatch();
        // for demo purposes just sleep
        Thread.sleep(170);
        stopWatch.stop("timing");
    }
}

This code will output the measurements to the standard output (the default minimalist logging framework).

start[1310075342634] time[171] tag[timing]

Note that Perf4J uses log4j configuration files and also supports JMX.

I would say yes. I followed the link you posted and noticed that the first paragraph said

Some of the the Perf4J tools require additional dependencies (for example, the custom log4j appenders require the log4j jar to also be in your classpath).

That suggests to me that not having log4j in your classpath is a normal thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM