3
  1. I am a learner of java and log4j.
  2. I just want to have the output of all logs in a .log file its not getting generated or i don't know path.
  3. I am just using eclipse and running my program on it .Please post if you have a simple program which can help me to learn log4j and how can i use it in project thanks in advance.

//This Is my Java File///

package test;
import org.apache.log4j.Logger;
import java.io.*;

public class LoggingSample {
    private static Logger logger = Logger.getLogger("LoggingExample");

    public static void main(String[] args) {
        try {
            FileInputStream fstream = new FileInputStream("D:\\textfile.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                System.out.println(strLine);
            }
            in.close();
        } catch (FileNotFoundException fe) {
            logger.error("File Not Found", fe);
            logger.warn("This is a warning message");
            logger.trace("This message will not be logged since log  level is set as DEBUG");
        } catch (IOException e) {
            logger.error("IOEXception occured:", e);
        }
    }
}

//// This Is my property file////

log4j.rootLogger=INFO,CONSOLE,R
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=./logs/testlog.log
log4j.appender.R.MaxFileSize=200KB
log4j.appender.R.MaxBackupIndex=2
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Nitin
  • 59
  • 1
  • 2
  • 7

4 Answers4

2

You could add your custom path as below

log = /usr/home/log4j
log4j.appender.R.File=${log}/MyLog.log

EDIT

Adding my entire property file

log4j.rootLogger=INFO,Console,FILE

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n


log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d - %c - %p - %m%n
log4j.appender.FILE.File=C:/Log/MyLog.log
Ace
  • 700
  • 7
  • 37
  • please edit in the property file and tell me the exact path where it generated i dont have any server my project is in D/Vikas/SampleLogging and in which i have created a log folder so path should be like this D/Vikas/SampleLoggging/log/testlog.log folder – Nitin Jul 27 '15 at 07:21
  • you can just replace the /usr/home/log4j with your abolute path (D:/Vikas/SampleLoggging/log/) and try – Ace Jul 27 '15 at 07:28
  • Eedited like this but still log file is not generating log = D:\\Vikas\LoggingExample\logs log4j.appender.FILE.File=${log}/testlog.log – Nitin Jul 27 '15 at 07:37
  • you are suppose to replace FILE with R in log4j.appender.FILE.File for your context, Sorry i forgot to change when answering the post. I have edited it now. – Ace Jul 27 '15 at 08:35
  • Also you should be finding the current log file in the directory where you have the java file. There will be a folder logs and inside should be your log file (according the path specific i.e. current directory/logs/testlog.log) – Ace Jul 27 '15 at 09:08
  • thanks but please tell how can i use this in my entire project if any s.out in my project it should come in my log file – Nitin Jul 27 '15 at 09:19
  • I would recommend that you use .info .error etc to print the data. If you still prefer to use SysOut then i recommend that you go through this question [log4j redirect stdout to DailyRollingFileAppender](http://stackoverflow.com/questions/1200175/log4j-redirect-stdout-to-dailyrollingfileappender) I hope this will solve your problem. – Ace Jul 27 '15 at 09:51
1

It seems that the log4j configuration file (log4j.properties) does not exist on the classpath. Try to put the log4j.property on folder target/classes

Another way is that you can specify log4j to load your configuration file

static {
        PropertyConfigurator.configure("C:\\log4j.properties");
    }
optuyvu
  • 11
  • 2
  • its giving following error on run time please check log4j:ERROR Could not read configuration file [C:\log4j.properties]. java.io.FileNotFoundException: C:\log4j.properties (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372) at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403) – Nitin Jul 27 '15 at 07:28
  • C:\\log4j.properties path is just the example. You must replace it with your configuration file – optuyvu Jul 27 '15 at 08:07
  • you are great it really worked one last question please my other colleague wants to use this in his program to generate logs of his program how he will able to use this what i have to tell him..thanks a ton... – Nitin Jul 27 '15 at 08:21
0

If you don't know path the use

log4j.appender.file.File=${catalina.base}/logs/mylog.log

it generate the file in log folder of server i.e tomcat log folder.

Abhinav Sahu
  • 183
  • 2
  • 9
  • thanks @abhinav but i don't have server just eclipse as mentioned will it not generate if i don't have apache tomcat?? this is my path of project which have a log folder D:\Vikas\LoggingExample – Nitin Jul 27 '15 at 06:59
  • ok then use system.setProperty("root" ,"c:/logs/mylog.log") and set log4j.appender.file.File=${root} – Abhinav Sahu Jul 27 '15 at 07:05
  • Please edit the property file and give it to me with editing really thankful to you..and also tell me the path where i have to check file – Nitin Jul 27 '15 at 07:09
0

1.log4j configuration file (log4j.properties) does not exist on the classpath. So i put the log4j.property on folder target/classes.

2.Replaced that everything is working now thanks all & @optuyvu

package test;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.io.*;
public class LoggingSample {
 private static Logger logger=Logger.getLogger("LoggingExample");
 static {
      PropertyConfigurator.configure("D:/Vikas/LoggingExample/log4j.properties");
 }
  public static void main(String[] args){
   try{
      FileInputStream fstream = new FileInputStream("D:\\textfile.txt");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
       while ((strLine = br.readLine()) != null){
     System.out.println (strLine);
      }
      in.close();
   }catch (FileNotFoundException fe){
     logger.error("File Not Found",fe);
        logger.warn("This is a warning message");
        logger.trace("This message will not be logged since log  level is     set as DEBUG");
   }catch (IOException e){
     logger.error("IOEXception occured:", e);
  }
 }
}
Nitin
  • 59
  • 1
  • 2
  • 7