简体   繁体   中英

If wrapping Log4j how does one get it to print the calling location's line number

Log4j neatly extracts the line number for log prints most likely from using a Throwable to get the line number from by looking at it's StackTrace.

Alas Logger is not an Interface in Log4j so one can't just make another implementation of it with debug,info,error etc methods on it and can then switch implementations as needed for the platform.

so if one creates a wrapper ie:

class Parent {
    static final Logger log = Logger.getLogger(Parent.class);

    static void debugLog(String str) {
        log.debug(str);
    }
    static void debugLog(String str, Throwable t) {
        log.debug(str, t);
    }
}

how can one have it put the line number of what is calling debugLog() into the log4j logger so it works as one would desire. Or is this simply a horrid design flaw in Log4j.

You have to calculate the location yourself and build a LocationInfo . Then you have to create a LoggingEvent and call the callAppenders method.

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