0

In my default cstr i an instantiating my log4j logger wherein i want to send in log file dir path during run time.

My default cstr has this:

logger = new LoggerSetup().SetLogger(Logger.getLogger(ServiceController.class), "FileLogger", LogDirPath); 

I have the path set in my properties file and i read that via

@Value("#{settings['ApplicationLogDirPath']}") private String LogDirPath; 

However as the cstr gets called before the @Value is wired up, the LogDirPath in the cstr is always null.

Is there another annotation that I should use else is there a better way?

What I am trying to achieve is to set the log4j log path set dynamically from log file in addition my controller has need for 2 loggers so that application level logs are logged to one spot and a long running database call [ controller calls biz layer ] writes to another location. the controller logs before calling the biz layer and then once the biz layer returns it logs that and hence 2 different log files. Customer needs this bizare logging so it is what it is

9
  • You may have to pass the path as a vm argument like -DlogPath=<path> Commented Feb 27, 2013 at 15:44
  • How does this work when i deploy? Commented Feb 27, 2013 at 15:46
  • what is the deployment process Commented Feb 27, 2013 at 15:50
  • BUild war file with eclipse, stop tomcat, place in webapps, start tomcat and app is deployed. stop tomcat, remove war file and restart tomcat Commented Feb 27, 2013 at 15:54
  • In that case it is not possbile, if you were using an installer we could have set the argument. Were is the source of the log location? is it loaded from a properties file? Commented Feb 27, 2013 at 15:57

2 Answers 2

1

You can use the ${log.dir} property substitution technique. Here's how:

String dynamicLog = // log directory somehow chosen... Properties p = new Properties( Config.ETC + "/log4j.properties" ); p.put( "log.dir", dynamicLog ); // overwrite "log.dir" PropertyConfigurator.configure( p ); 

Following should also address your question.

Sign up to request clarification or add additional context in comments.

1 Comment

I initially tried using the @PostConstruct but that seemed to be getting called twice when the application was launched in debug on server mode. The article you provided was much helpful and got to a solution similar to one proposed
0

You can try to load the property file manually and fetch the property

File resource = new File(SO15116168.class.getClassLoader().getResource(".").getFile()); File file = new File(resource.getParent()+"/spring","customprops.properties"); System.out.println(file); Properties p = new Properties(); p.load(new FileInputStream(file)); String property = p.getProperty("ApplicationLogDirPath"); 

replace SO15116168 with your class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.