Log4J

hi friends,my class reside in : com.ct.main.Main.javai want to print output in file called log.txt for example want to printprint "Enter"print "Application"print "Exit"how can i do? please help me guy's.....?
[266 byte] By [MCA134a] at [2007-11-27 0:40:44]
# 1

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

java_2006a at 2007-7-11 22:53:47 > top of Java-index,Java Essentials,Java Programming...
# 2
Kind of a vaguely defined quetsion;)Do you ask for the 'use' of logfj, or how to set up log4j to print to different files (Enter-file, app-file, exit-file?) during runtime.Cincerly P
olspala at 2007-7-11 22:53:47 > top of Java-index,Java Essentials,Java Programming...
# 3

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

Mathewhaia at 2007-7-11 22:53:47 > top of Java-index,Java Essentials,Java Programming...
# 4
Thank you man....
MCA134a at 2007-7-11 22:53:47 > top of Java-index,Java Essentials,Java Programming...
# 5

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

Peetzorea at 2007-7-11 22:53:47 > top of Java-index,Java Essentials,Java Programming...