简体   繁体   中英

log4j not logging the details coming from some java classes

I am trying to log data from my project into files based on the input parameter. My project has 5 classes of which 3 contain log messages.

I used the following code to configure log4j.

public static void configureLog4j(String log4j,String logs,String timeStamp) throws IOException {
    PropertyConfigurator.configure(log4j);      
    FileAppender fileapp = (FileAppender) Logger.getRootLogger().getAppender("FILE");
    if(fileapp != null)
        logger.removeAppender(fileapp);
    String filename = logs + "/test_" + info + "_" + timeStamp;
    fileapp.setFile(filename);
    fileapp.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"));
    fileapp.setWriter(new FileWriter(new File(fileapp.getFile())));
    Logger.getRootLogger().addAppender(fileapp);
}

info represent name of the user. Now once I configure this in the beginning of the applications only two classes are writing logs in the file the third file which contains code to send & receive data does not write the logs to the log file. Is there any thing I am missing here.

Please help me with this issue.

Chances are that the third class is already logging before you have a chance to configure your logger. This is one reason why I switched to logback: It has a much more powerful (and better documented) config file format.

The code above also contains a problem:

if(fileapp != null)
    logger.removeAppender(fileapp);

logger isn't defined anywhere and fileapp must be non-null or you'd get NPEs later.

请确保配置之前不要记录您的第三类

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