Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

log4j use best way

 
shankha bhattacharya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I use log4j in the best way in my program?

I configure all logging properties in the properties file.

/*******************************************


#For File Append of the logs
log4j.rootCategory=DEBUG, rfware
log4j.appender.rfware=org.apache.log4j.RollingFileAppender
log4j.appender.rfware.File=c:/temp/aiicommunicator.log
log4j.appender.rfware.Threshold=DEBUG
log4j.appender.rfware.Append=false
log4j.appender.rfware.MaxFileSize=1000KB
log4j.appender.rfware.MaxBackupIndex=2
log4j.appender.rfware.layout=org.apache.log4j.PatternLayout
log4j.appender.rfware.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

/*********************************************************************

But I want to load this properties during the start up of my application for logging in my java all java classes.

can you provide some code guide line how can I achieve it ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put that into a file named log4j.properties and make sure that the properties file is in a directory that's in the classpath when you run your application, then Log4J will automatically find it and you don't have to do anything special to initialise Log4J in your application.
 
shankha bhattacharya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am not use the name log4j.properties then how can I use it to my application?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Promoting to the intermediate forum.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't this belong in "Other opensource"?

Anyway, take a look at the PropertyConfigurator. If you call configure(file) or configureAndWatch(file) before the first log statement, it will use the configuration in the given file.
 
jianfeng ding
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about use: PropertyConfigurator.configure(".\\src\\log4j.properties") first init the log4j?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"west west", please check your private messages. You can see them by clicking My Profile.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic