1

I want to get the path to the log4j log file from another properties file. So, if I have this:

config.properties:

# Path to logs
logPath=/home/flow/logs/projectName.log

I want to get that path and change the value of log4j.appender.LOGFILE.File=axis2.log inside log4j.properties.

How can I accomplish this?

EDIT: My log4j.properties file:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE
log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n

# LOGFILE is set to be a File appender using a HTML Layout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=axis2.log
log4j.appender.LOGFILE.Append=true
#log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
log4j.appender.LOGFILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.LOGFILE.layout.Title=NPC Simulator Log

Update (this way didn't work):

public class Foo 
{
    public void test() {
        System.setProperty("my.log", ConfigHandle.getProperty("logPath");
        Logger log = Logger.getLogger(MyClass.class.getName());

       log.info("Testing...");
    }
}

config.properties

# log4j.properties
logPath=/home/flow/logs

log4j.properties

...
log4j.appender.LOGFILE.File=${my.log}/axis2.log
...
o.o
  • 3,563
  • 9
  • 42
  • 71
  • Add log file path in log4j.properties with key *log4j.appender.R.File* – PVR Jun 15 '16 at 15:12
  • As I just started using log4j, how would adding that key help me? I'm not sure how to use it. (Added my `log4j.properties` file to the op) – o.o Jun 15 '16 at 15:17
  • If you are trying to change file path dynamically Please check this : http://stackoverflow.com/q/11846593/3632059 – PVR Jun 15 '16 at 15:30
  • 1
    From that link, I got that I should set a System property, get the path from `config.properties` then make the `static Logger`? I'll try it out. – o.o Jun 15 '16 at 15:42
  • @PVR I don't think I'm doing it correctly. I updated op with what I did. No HTML or log file is created. – o.o Jun 15 '16 at 16:17

1 Answers1

2

Refer this example:

Log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${my.log}
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

config.properties

FILE_PATH=D://logfile.log

Sample code:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class HelloExample {

static { 
    String filePath=readFile();
    System.setProperty("my.log", filePath); }

final static Logger logger = Logger.getLogger(HelloExample.class);

    public static void main(String[] args) {

        HelloExample obj = new HelloExample();
        obj.runMe("myRun");

    }

    private void runMe(String parameter){

        if(logger.isDebugEnabled()){
            logger.debug("This is debug : " + parameter);
        }

        if(logger.isInfoEnabled()){
            logger.info("This is info : " + parameter);
        }

        logger.warn("This is warn : " + parameter);
        logger.error("This is error : " + parameter);
        logger.fatal("This is fatal : " + parameter);

    }

    public static String readFile()
    {
        Properties prop = new Properties();
        InputStream input = null;

        try {
            //load file from classpath
            input=   HelloExample.class.getClassLoader().getResourceAsStream("config.properties");

            // load a properties file
            prop.load(input);

            // get the property value and print it out
            return prop.getProperty("FILE_PATH");

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}

So here read new path from config.properties and place it in static block which will replace it at run time.

PVR
  • 885
  • 9
  • 18
  • Thanks, however, I get this error. `13:28:26,254 ERROR [STDERR] log4j:ERROR setFile(null,true) call failed. 13:28:26,254 ERROR [STDERR] java.io.FileNotFoundException: ` Not sure why it's null as I get the property from my config file. I print it out inside the `static` block to make sure and it prints out the path. – o.o Jun 16 '16 at 17:33
  • Sorry, here you go: https://gist.github.com/anonymous/22bc8f3c6eab2e2c170e1266f43c88ed – o.o Jun 16 '16 at 19:09
  • @m.o Have edited my answer which will give you idea to read config.properties file. – PVR Jun 16 '16 at 19:10
  • That's pretty similar to how I read it. My way for reading works fine as I use it throughout my program: https://gist.github.com/anonymous/7acda670455e32929a5bb181c353bd5c. However, I tried it your way (moved `config.properties` inside project folder), but I still get the same error. – o.o Jun 16 '16 at 19:15
  • Try this: Keep properties in same folder where your class file is and read like this : InputStream inputStream=ConfigHandle .class.getResourceAsStream("config.properties"); – PVR Jun 16 '16 at 19:27