4j log file is not generating / I don't know the path
- I am involved with java and log4j.
- I just want the output of all the logs in the .log file to not get generated or I don't know the path.
- I just use eclipse and run my program on it . Please post if you have a simple program to help me learn log4j and how can I use it in a 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 properties 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
+3
source to share
4 answers
You can add your own path below
log = /usr/home/log4j
log4j.appender.R.File=${log}/MyLog.log
EDIT
Adding my entire properties 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
+2
source to share
The config file 1.log4j (log4j.properties) does not exist on the classpath. So I put log4j.property in target / classes folder.
2.Replaced that everything works now thanks to everyone and @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);
}
}
}
0
source to share