2

I have class file like this, using log4j.jar is easy in eclips IDE. but not through windows command prompt.

import log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class log4jExample{
    /* Get actual class name to be printed on */
    static Logger log = Logger.getLogger( log4jExample.class.getName());

    public static void main(String[] args)
            throws IOException,SQLException {
        log.debug("Hello this is an debug message");
        log.info("Hello this is an info message");
    }
}

and I create log4j properties like this:

# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

what should I do to get log records in cmd and file

Rajesh
  • 10,318
  • 16
  • 44
  • 64
  • 1
    Are you specifying the path to your config file in the command line? if not, then there is already an answer here: http://stackoverflow.com/questions/778933/log4j-configuration-via-jvm-arguments – morgano Jul 08 '14 at 05:18
  • thank you morgano, i put these files under one folder and compile using javac -cp .;s.log4j-1.2.11.jar log4jExample.java but it wont work. Help me to improve my answer. thanks – Saravanamanikandan Jul 08 '14 at 06:16
  • Is not when you compile but when you execute: `java -cp .;log4j-1.2.11.jar -Dlog4j.configuration=log4j.properties log4jExample` try that one (if this time it works then don't forget to follow the link on my first comment and upvote both question and answer) – morgano Jul 08 '14 at 06:30
  • i got output . thanx for the response morgano – Saravanamanikandan Jul 08 '14 at 08:58

1 Answers1

2

Just define a ConsoleAppender and the log messages will go both to the file and to the console.

http://logging.apache.org/log4j/1.2/manual.html

Your config file should be

# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE, A1
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n


# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64