Ana Franco
Ana Franco

Reputation: 1821

Why are my logs being removed?

I have an app in java using log4j as my logging jar, but when I try to debug my app and open the log file I find it short and with only the last lines I logged and the rotation files don't show any of the logs that I have seen before.

Is there something wrong with my configuration file? I have multiple projects pointing to the same log file, Could this be the problem?

Here is my log4j.properties:

log4j.rootLogger=ERROR,logfile

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=/opt/tomcat7/logs/mylogs.log

log4j.appender.logfile.MaxFileSize=500KB
# Keep one backup file
log4j.appender.logfile.MaxBackupIndex=2

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %5p (%F:%L) - %m%n

Thanks

Upvotes: 0

Views: 158

Answers (2)

Yurii
Yurii

Reputation: 877

You specified level of root logger to be ERROR, while in most cases there won't be a lot of ERROR messages - maybe one-two per many INFO level messages, while you suppressed them: ERROR level allows only ERROR and FATAL messages to be logged. And it filders out any INFO, WARN, DEBUG, TRACE level. Usually you don't use such setup in development. And in production you usually suppress only some verbose classes/packages, but not everything like in config you posted (meaning here ERROR level applies to every class).

So I suppose you should replace ERROR with INFO in your config.

UPDATE: Also likely log4j simply doesn't see your config file. I encountered this issues several times personally and each time I had a workaround like this:

public class Log4JProperties {

    public static void setupLog4j(){
        String log4jPathFile = Log4JProperties.class.getResource("/log4j.properties").getFile();

        Properties props = new Properties();
        try {
            InputStream configStream = new FileInputStream(log4jPathFile);
            props.load(configStream);
            configStream.close();
        } catch (IOException e) {
            System.out.println("log4j configuration file not found");
        }
        LogManager.resetConfiguration();
        PropertyConfigurator.configure(props);
    }

}

This is not necessary when I run code from inside Intellij, but when I run using Maven, logging doesn't work without calling this function in the beginning of the execution.

Upvotes: 2

Asela Senanayake
Asela Senanayake

Reputation: 351

When you specify MaxBackupIndex=2 it will only keep last 2 backups, if log file fills up quickly old backup files will be overridden.

You might have to increase the MaxFileSize or MaxBackupIndex based on your file logging behaviour…

https://logging.apache.org/log4php/docs/appenders/rolling-file.html

Upvotes: 0

Related Questions