40

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!!

Raedwald
  • 46,613
  • 43
  • 151
  • 237
manisha
  • 665
  • 3
  • 8
  • 15
  • Have you already tried the standard output? – morgano Jun 27 '13 at 06:32
  • This SO answer may help you: http://stackoverflow.com/questions/2619160/sample-xml-configuration-for-log4j-have-a-main-java-application-and-want-to-w – Jiri Patera Jun 27 '13 at 06:37
  • If you want to have a log in a file you have to define where should your log file create. – Ruchira Gayan Ranaweera Jun 27 '13 at 06:39
  • ya the following warning is being displayed at standard output: log4j:WARN No appenders could be found for logger (test.Log4jExample). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. – manisha Jun 27 '13 at 06:40
  • @shruti That's because there's no log4j.properties in the classpath. It's not an error because Log4j reverts to defaults that is logging to console. More info at the official [site](http://logging.apache.org/log4j/1.2/manual.html). – Ravi K Thapliyal Jun 27 '13 at 06:44
  • @Ravi so can u pls tell me where is my log info going as it is not being displayed at console and where do i place my .properties file as well – manisha Jun 27 '13 at 06:47
  • @shruti, i have added answer with its proper tutorial from where you have taken this java sample code, which may help you to understand its work, configuration, and much more – Jubin Patel Jun 27 '13 at 06:49

4 Answers4

38

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:

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Follow this tutorial to learn more about log4j usage:

http://www.mkyong.com/logging/log4j-log4j-properties-examples/

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
8

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 a FileAppender explicitly by defining it in a log4j.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.

log4j.rootLogger=debug, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=example.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%p %t %c - %m%n

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

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • so can u pls tell me where is my log info going as it is not being displayed at console and where do i place my .properties file as well – manisha Jun 27 '13 at 06:49
  • Anything inside "/src" is in your *classpath*. – Ravi K Thapliyal Jun 27 '13 at 06:51
  • 2
    Please, note that this creates an example.log in your current dir (Eclipse's project dir) so the same log4j.properties would work with diff projects without over writing logs. – Ravi K Thapliyal Jun 27 '13 at 06:59
  • @Ravi, please use the word "directory" or "CWD" for people searching this page with Ctrl+F ;) – golimar Mar 24 '15 at 16:51
  • @golimar, comments cannot be edited after a while so I'm including mine as an edit to my answer. Thanks for the feedback. I'll make sure I don't use this shorthand in future :) – Ravi K Thapliyal Mar 25 '15 at 03:36
  • 2
    This is the only answer that actually answered the question of where log4j logs by default. – fzzfzzfzz Oct 27 '15 at 21:10
  • @RaviThapliyal: thanks for the answer. I have a question, after I create a JAR and ran it what is the location of log file? – Aniket Kulkarni May 25 '18 at 09:16
  • I've always wondered about this severely brain-damaged default. Was it "chosen" because STDERR is not shown in Eclipse's or whatever's "console"? See [here](https://www.jstorimer.com/blogs/workingwithcode/7766119-when-to-use-stderr-instead-of-stdout), e.g., for Doug McIllroy on why STDERR was even invented to begin with. – arayq2 Dec 05 '19 at 12:37
8

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 log4j

So the content of log4j.properties file would be as follows:

# 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

make changes as per your requirement like log path

Jubin Patel
  • 1,959
  • 19
  • 38
0

You 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.

# Global logging configuration
log4j.rootLogger=DEBUG, stdout, R

# SQL Map logging configuration...
log4j.logger.com.ibatis=INFO
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=INFO
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=INFO
log4j.logger.com.ibatis.SQLMap.engine.impl.SQL MapClientDelegate=INFO

log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=INFO

log4j.logger.org.apache.http=ERROR

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=MyLog.log
log4j.appender.R.MaxFileSize=50000KB
log4j.appender.R.Encoding=UTF-8

# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %5p [%t] (%F\:%L) - %m%n
IConfused
  • 714
  • 2
  • 8
  • 20