add log4j-1.2.XX.jar and commons-logging.jar to your classpath, then create a log4j.properties under your classes directory file like the following:
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
# INFO, WARN, ERROR
log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=myLog.log
log4j.appender.logfile.MaxFileSize=1024KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=5
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
# Print only messages of level INFO or above in some packages.
# Possible values : DEBUG, INFO, WARN, ERROR
log4j.logger.com.ct.main=INFO
see log4j documentation :
http://logging.apache.org/log4j/docs/documentation.html
Hi,
for writing in to log.txt file, you dont need to use log4j. You can use FileOutputStream for that.
the following code snippet will write one character to an end of file.
DataInputStream dis = new DataInputStream(System.in);
BufferedReader br = new BufferedReader(new FileReader(inputfile));
FileOutputStream fos = new FileOutputStream(new File(outputfile));
PrintWriter out=new PrintWriter(fos);
String line="";
while((line = br.readLine())!=null){
out.println(line+char_rep);
}
out.close();
}
If you want to write it to a .log file use log4J
Why would you want to reinvent logging using FileOutputStreams if there's various logging frameworks that do this for you? Before you even get close to the flexibility and stability of those frameworks, you'll have to put in a lot of effort. Think about rolling files, output patterns, different targets,...
> If you want to write it to a .log file use log4J
You can write to whatever file you want using log4j