Solved

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

Posted on 2011-02-16
15
1,315 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 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