Solved

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

Posted on 2011-02-16
15
1,266 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
  • 5
  • 4
  • 4
  • +2
15 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 167 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 333 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 333 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 36
hibernate example using maven 12 41
Unhandled exception type Exception 18 28
Java: How do I open the default windows program for a given file type 3 28
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now