Hi i am new to programming concepts and i am tend to work out something with log4j. So i am reading Log4j tutorials where i found the following code:
package test;
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
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");
}
}
But after running this in eclipse i am not able to locate the generated log file. Can anybody tell where is the file being dumped? Also help me with some best sites wherefrom i can learn Log4j and Java Doc from the scratch. Thanks!!
You have copy this sample code from Here,right?
now, as you can see there
property
file they have define, have you done same thing? if not then add below code in your project with property file for log4jSo the content of log4j.properties file would be as follows:
make changes as per your requirement like
log
pathYou can see the log info in the console view of your IDE if you are not using any log4j properties to generate log file. You can define log4j.properties in your project so that those properties would be used to generate log file. A quick sample is listed below.
To redirect your logs output to a file, you need to use the FileAppender and need to define other file details in your log4j.properties/xml file. Here is a sample properties file for the same:
Follow this tutorial to learn more about log4j usage:
http://www.mkyong.com/logging/log4j-log4j-properties-examples/
By default,
Log4j
logs to standard output and that means you should be able to see log messages on your Eclipse's console view. To log to a file you need to use aFileAppender
explicitly by defining it in alog4j.properties
file in your classpath.Create the following
log4j.properties
file in your classpath. This allows you to log your message to both a file as well as your console.Note: The above creates an example.log in your current working directory (i.e. Eclipse's project directory) so that the same log4j.properties could work with different projects without overwriting each other's logs.
References:
Apache log4j 1.2 - Short introduction to log4j