2013-02-27 124 views
0

在我的默認cstr我實例化我的log4j記錄器,其中我想在運行時發送日誌文件目錄路徑。在默認cstr設置日誌路徑

我的默認CSTR具有這樣的:

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

我在屬性中設置文件的路徑和我讀通過

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

然而,由於@Value連線之前的CSTR被調用,cstr中的LogDirPath始終爲空。

是否有另外一個我應該使用的註釋是否有更好的方法?

我想實現的是從日誌文件中動態設置log4j日誌路徑此外,我的控制器需要2個記錄器,以便應用程序級日誌記錄到一個點和長時間運行的數據庫調用[控制器調用biz層]寫入另一個位置。控制器在調用biz層之前進行日誌記錄,然後一旦biz層返回日誌,並因此記錄2個不同的日誌文件。客戶需要這種bizare日誌記錄,所以它是它是什麼

+0

您可能必須將該路徑作爲vm參數傳遞,例如'-DlogPath = ' – 2013-02-27 15:44:15

+0

當我部署時,這是如何工作的? – user1361914 2013-02-27 15:46:41

+0

什麼是部署過程 – 2013-02-27 15:50:30

回答

1

您可以使用$ {log.dir}屬性替換技術。方法如下:

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也應該解決您的問題。

+0

我最初嘗試使用'@ PostConstruct',但似乎在應用程序在調試服務器模式下啓動時調用了兩次。您提供的文章非常有用,並得到了類似於所提議的解決方案 – user1361914 2013-02-28 21:44:03

0

你可以嘗試手動加載屬性文件並獲取財產

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"); 

與你的類取代SO15116168