Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Log4j.properties, set location of rolling File according to program arguments

Posted on 2011-02-16
15
Medium Priority
?
1,343 Views
Last Modified: 2012-05-11
Hello,

I've created a log4j.properties file with the below code.  How do I change the location of the file according to program arguments?  

log4j.appender.rollingFile = org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File = D:/mylog.log
log4j.appender.rollingFile.MaxFileSize = 2MB
log4j.appender.rollingFile.MaxBackupIndex = 2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

Open in new window


Thanks
0
Comment
Question by:AndyC1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +2
15 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 668 total points
ID: 34912684
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 34913526
Use like this  log4j.appender.ROLLING.File=${user.home}/mylog.log .it will take user(login) home .

Or you can use java.util.property to change the  property file deatils

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34914387
>>Use like this  log4j.appender.ROLLING.File=${user.home}/mylog.log .it will take user(login) home .

That's correct, and can be extended to use a custom jvm arg too, which you can pass at startup
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AndyC1000
ID: 34922665
Thanks I'll look into your responses.

I'm not creating a web app.

I'd like to use java.util.property to change the property file details, based on the directory specified in the batch file by the user.
0
 
LVL 92

Expert Comment

by:objects
ID: 34922686
Possibly easier to just specify the name on the command line in your batch file as for_yan suggested in first comment.
0
 

Author Comment

by:AndyC1000
ID: 34940354
Thanks, I've looked at the link provided by for_yan

Tried adding the following line in the log4j.properties file,
log4j.appender.rollingFile = ${logFile_var}/mylog.log

and the following line to the run configuration VM args
-DlogFile_var = "C:/temp"

I'm getting an error, wondering if the syntax is incorrect?

java.lang.NoClassDefFoundError: =
Caused by: java.lang.ClassNotFoundException: =
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main"

Thanks
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34940362
you need to add -DlogFile_var  before the class - this is how you were doing it?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34940385

and in the log4j,properties, I believe, it looks something like that:

log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = logs/bensApps.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

http://www.benmccann.com/dev-blog/sample-log4j-properties-file/


0
 
LVL 92

Assisted Solution

by:objects
objects earned 1332 total points
ID: 34940393
> -DlogFile_var = "C:/temp"

remove the spaces
0
 

Author Comment

by:AndyC1000
ID: 34940435
I decided to get it running in Eclipse.

Added to Run Configurations -  VM arguments
-DlogFile_var = C:/temp

The error has now changed to:

og4j:ERROR Could not instantiate class [C:/temp/mylog.log].
java.lang.ClassNotFoundException: C:/temp/mylog.log
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at org.apache.log4j.helpers.Loader.loadClass(Loader.java:178)
	at org.apache.log4j.helpers.OptionConverter.instantiateByClassName(OptionConverter.java:319)
	at org.apache.log4j.helpers.OptionConverter.instantiateByKey(OptionConverter.java:120)
	at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:629)
	at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:612)
	at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:509)
	at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:415)
	at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:441)
	at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:470)
	at org.apache.log4j.LogManager.<clinit>(LogManager.java:122)
	at org.apache.log4j.Logger.getLogger(Logger.java:117)
	at project.Adaptor.<clinit>(project.Adaptor:21)
log4j:ERROR Could not instantiate appender named "rollingFile".
log4j:WARN No appenders could be found for logger (project.Adaptor).
log4j:WARN Please initialize the log4j system properly.

Open in new window


The error is caused by:

private static final Logger log = Logger.getLogger(Adaptor.class);
0
 
LVL 92

Expert Comment

by:objects
ID: 34940462
see my last comment
0
 

Author Comment

by:AndyC1000
ID: 34940463
Thanks objects removed the spaces and found the error changed to the above.
0
 
LVL 92

Accepted Solution

by:
objects earned 1332 total points
ID: 34940469
> log4j.appender.rollingFile = ${logFile_var}/mylog.log

should be:


log4j.appender.rollingFile.File = ${logFile_var}/mylog.log
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34940470
You probably still have spaces between equal signs
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34940475
or maybe

log4j.appender.rolling.File = ${logFile_var}/mylog.log

or
log4j.appender.R.File =...
as they show in the sampel above
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question