Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:23:46+00:00 2026-06-10T05:23:46+00:00

I am using log4j for logging. I have a requirement where the log files

  • 0

I am using log4j for logging. I have a requirement where the log files should be created in the path entered by the user in command prompt. Is it possible to change the log file path dynamically. I also have to create a separate log file with current time stamp in file name.Is this possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:23:47+00:00Added an answer on June 10, 2026 at 5:23 am

    To setup the Filepath depending on the users input:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import org.apache.log4j.FileAppender;
    import org.apache.log4j.Logger;
    import org.apache.log4j.PatternLayout;
    
    public class DemoClass {
    
      public static void main(String[] args) {
        Logger logger = Logger.getLogger("main");
        FileAppender fileAppender = new FileAppender();
        fileAppender.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
        logger.addAppender(fileAppender);
        String userLogFileName = "";
        BufferedReader  reader = new BufferedReader(new InputStreamReader(System.in));
        try {
          userLogFileName = reader.readLine();//read the users input
        } catch (IOException e) {
          System.out.println("error on reading user input");
          e.printStackTrace();
        }
        File file = new File(userLogFileName);
        try {
          if(!file.exists()) {//if the file does not exist
            file.createNewFile();//create a new file
          }
        } catch (IOException e) {
          System.out.println("error on creating file");
          e.printStackTrace();
        }
        fileAppender.setFile(userLogFileName);//the file must exist
        fileAppender.activateOptions();//activate the new file
        logger.error("test");
      }
    
    }
    

    to your second Question:
    The filename should be updated every time you log an event? This sounds like a bad performance. Better use a DailyRollingFileAppender to get a new logfile one a timestep of your choice and put the timestamp of the last event in the filename(default).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using log4j for my logging purposes and all my log files in .log
I have discovered some issues in my program. I was using log4j for logging,
My Application is using logging in two manner....1) programatic 2) log4j.xml I have to
We have multiple batch jobs and using log4j for logging for each on of
I have been told to use log4j for my logging project,but before using log4j,
I have successfully configured an application that uses log4j for it's logging to log
I have multiple applications using log4j for their logging, each loading settings from separate
I have a web application that dumps logging information in a file using log4j
I have a java application using log4j SyslogAppender (facility=USER) and I can see using
We are using Log4J to writing log files in our batch applications. The log

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.