Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-16
15
Medium Priority
?
1,379 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

772 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