Hello,

Please clarify the following for me-

(1) I want to use a rolling file appender with Log4j, so that the log messages are stored into a single log file....Also I want that the log file should be passed as parameter during program runtime.

(2) What all classes have to be imported to allow the use of log file as per point 1.

(3) The application in which I want to use log4j has multiple different classes... I want to log all messages to the same log file... Is it possible to use the same log file across different java classes? How do I invoke a logger to write data to the same log file (same
file to which another class in code is writing the log messages to)?

I would appreciate it if you can post a working code sample that accomplishes the above.

Regards,
Arvind.

1) Not really a question asked there, but yes you can have it stored into single log file. Nevertheless it is better to have specifically log for ERROR type and then another one that will cover anything from INFO, WARNING etc
2) Single jar file of log4j is sufficient
3) Log4j or any other logging system is build in mind of logging for whole application not just single class so do not worry.

I will not post any code, because you should be able to get it working based on their example from official site. If that is not enough there is a good book called Pro Apache Log4j

Hello Peter,

Thank you, your response is very helpful to me.

I have one more request though- the manual at the log4j site explains cases wherein the folder location and name of the log file is set through a properties file... I am looking at a scenario wherein the end user specifies the name and location of log file when he runs the application.

I would appreciate it if you could point me to a code sample which deals with this specific scenario.

Regards,
Arvind.

It is possible, by checking if the property file has declared log destination on start of application. In case of AWT/Swing application just provide customized file chooser and let user select destination and then write it in property file. In case of web application, instead of index page redirect to some page with form where user type in details

Hello again,

What you are saying is, I should ask the user where he wants messages to be logged, and then put that file name +path into the properties file...
Is it possible to not have a log4j "properties" file and directly declare the file name + its path in code only? Or is it compulsory to have a properties file?

Also I have one more query- where exactly do i put the log4j properties file? Should it be where I have the final JAR file for my application? Or is it at root of source code?

I am sorry if my questions sound too dumb to you, I am a beginner at log4j, hence I am troubling you with my 'dumb' sounding queries...

Thanks,
Arvind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.