65

Can anyone please guide me as to how I can configure log4j to log to a specific file that I specify at run-time.The name and path of the log file are generated at run-time and the application must log to that particular file.

Generally file appender entries in the log4j.properties file points to the log file that will be used by the application.However in this case I would like to read the log file path from the command line and log to that particular file.

How can I achieve this ?

Charlie Brumbaugh
  • 219
  • 1
  • 5
  • 15
Fell
  • 755
  • 4
  • 11
  • 18

5 Answers5

101

You can also do this from the log4j.properties file. Using the sample file below I have added the system property ${logfile.name}:

# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd@HH\:mm\:ss,SSS}\:%c - %m%n

The log file name can then be set two different ways:

  1. As a command line, system property passed to java "-Dlogfile.name={logfile}"
  2. In the java program directly by setting a system property (BEFORE you make any calls to log4j).

    System.setProperty("logfile.name","some path/logfile name string");

jmq
  • 10,110
  • 16
  • 58
  • 71
  • 5
    _"system property passed to java `-Dlogfile.name={logfile}`"_ is exactly what I was looking for – snooze92 Aug 15 '14 at 13:19
  • 1
    Great - exactly what I am looking for - but what happens if the system property 'logfile.name' is NOT set on command-line? Runtime exception thrown? – t3az0r May 23 '16 at 10:12
  • 10
    if you are using log4j2, you should write `${sys:logfile.name}` in the xml – Jenkyn Sep 10 '16 at 02:44
  • So call me an idiot, but what if you do #2 and it does't pick it up? I have a main function with no log4j in it and the system property set. Any ideas? – markthegrea Nov 10 '17 at 19:38
  • This method doesn't work well with a static logger, which is initialized before the System.setProperty() call. – Guangliang Feb 26 '18 at 03:55
62

Adapted from the log4j documentation:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;

public class SimpandFile {
   static Logger logger = Logger.getLogger(SimpandFile.class);
   public static void main(String args[]) {

      // setting up a FileAppender dynamically...
      SimpleLayout layout = new SimpleLayout();    
      FileAppender appender = new FileAppender(layout,"your filename",false);    
      logger.addAppender(appender);

      logger.setLevel((Level) Level.DEBUG);

      logger.debug("Here is some DEBUG");
      logger.info("Here is some INFO");
      logger.warn("Here is some WARN");
      logger.error("Here is some ERROR");
      logger.fatal("Here is some FATAL");
   }
}
Sal Prima
  • 1,412
  • 2
  • 18
  • 23
Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
0

Can be also done by this properties define in log4j.properties file

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.maxFileSize=5000KB
log4j.appender.logfile.maxBackupIndex=5
log4j.appender.logfile.File=/WebSphere/AppServer/profiles/Custom01/error.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p [%t] %C %M %c{1}:%L - %m%n
Sumit
  • 1
  • 2
0

Working and same has been tested

// setting up a FileAppender dynamically...
SimpleLayout layout = new SimpleLayout(); 
RollingFileAppender appender = new RollingFileAppender(layout,"file-name_with_location",true);
                    appender.setMaxFileSize("20MB");
                    logger.addAppender(appender);
kenzie
  • 217
  • 1
  • 2
  • 12
0

Just remove your log4j code and replace this code in your java class it's working good no need to add properties or xml file


SimpleDateFormat dateFormatsd = new SimpleDateFormat("dd-MM-yyyy-hh-mm");
String dateandtime=dateFormatsd.format(new Date());

SimpleLayout layout = new SimpleLayout(); 
RollingFileAppender appender = new RollingFileAppender(layout,".\\Logs\\log "+dateandtime+".txt",true);
                            appender.setMaxFileSize("20MB");
                            logger.addAppender(appender);

The above code will log the logs for every minute.

Thanks