You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Tan Yoke Yew <yy...@infopro.com.my> on 2003/04/17 12:55:33 UTC

do a simple logging to txt file

hi there;

I am doing Java application, not using Java swing or Java awt.

today i am just picking up Log4J.

I am interest on it, but the problem is the example I go thru is all console
log, i want to log into a txt file.
May I get an example for that? (maybe for a simple hello word, how to log
some particular info into a txt file.

Thanks!

Regards;
yoke yew


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org


Re: do a simple logging to txt file

Posted by Erik Price <ep...@ptc.com>.

Tan Yoke Yew wrote:
> hi there;
> 
> I am doing Java application, not using Java swing or Java awt.
> 
> today i am just picking up Log4J.
> 
> I am interest on it, but the problem is the example I go thru is all console
> log, i want to log into a txt file.
> May I get an example for that? (maybe for a simple hello word, how to log
> some particular info into a txt file.

Hi Yoke,

It is easy to change from a console log to a text file log.  The class 
remains unchanged:

public class HelloWorld {
   org.apache.log4j.Logger logger =
     org.apache.log4j.Logger.getLogger(HelloWorld.class.getName());

   public static void main(String[] args) {
     String msg = "Hello World!";
     logger.info("printing msg: " + msg);
     System.out.println(msg);
     logger.info("printed msg");
   }
}

It is in the properties file where you can specify the type of Appender 
(I have simply borrowed the properties file from the short manual in the 
docs):

# Set root logger level, and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# But ignore most messages from Apache libs
log4j.logger.org.apache=WARN

# A1 is set to be a FileAppender.
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=build/test/testing.log

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


So, you specify the logging levels, the Appender type and file path if 
applicable, then the layout you want.  I haven't really played with 
Log4J very much, the above just happens to do exactly what I need.  Hope 
this helps,



Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org