简体   繁体   中英

log4j not writing to the file

This is my log4j.properties

# Define the root logger with appender file
log4j.rootLogger = DEBUG,FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=/media/.I have given the whole path../MyProject/log.log

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{MM-dd@HH:mm:ss} %-5p (%13F:%L) %3x - %m%n

This is my java file

package examples;

import java.util.logging.Logger;

import org.apache.log4j.PropertyConfigurator;

public class Test {

    static Logger logger = Logger.getLogger(Test.class.getName());

    public static void main(String a[]){

        //PropertiesConfigurator is used to configure logger from properties file
        PropertyConfigurator.configure("log4j.properties");

        //Log in console in and log file
        //logger.debug("Log4j appender configuration is successful !!");
        logger.info("Log4j appender configuration is successful !!");

    }   

}

Still im getting output message on console only my log.log file is empty. I have write permissions to the file I am running this in eclipse is that a problem.

Use Logger from log4j package.

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

Create your logger like this :

static final Logger logger = LogManager.getLogger(Test.class.getName());

log4j doesn't catch java.util.logging log messages.

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