0

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 i m 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.

pst
  • 1
  • 1
  • 1
  • Maybe the configuration file is not being picked properly. Add -Dlog4j.debug and you will see log4j debug information containing configruation. – Kasper Ziemianek Jun 25 '14 at 10:58
  • @xwid if i change something in it, gives me error so i think it is getting picked up – pst Jun 25 '14 at 11:01
  • I used your configuration, just changed appender's file to test-log.log and log4j created this file with log messages in it. – Kasper Ziemianek Jun 25 '14 at 11:07

1 Answers1

2

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.

Kasper Ziemianek
  • 1,329
  • 8
  • 15
  • Now with this it is writing logs into the file. Now they are not getting displayed on console. I want them to be displayed – pst Jun 25 '14 at 13:26
  • To make your log4j log messages to both console and file follow this question : http://stackoverflow.com/questions/3382985/how-to-make-log4j-to-write-to-the-console-as-well To make your appender apend log messages to existing file use `log4j.appender.LOGFILE.Append=true` – Kasper Ziemianek Jun 25 '14 at 13:30
  • Sorry got it I need to add ConsoleAppender Thanx ** xwid** – pst Jun 25 '14 at 13:35