Solved

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

Posted on 2011-02-16
15
1,257 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

705 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

21 Experts available now in Live!

Get 1:1 Help Now