2017-03-09 43 views
0

相反,它打印到控制檯或在某些文件寫我要收集所有日誌(即消息)到這樣的Java對象的字符串的一些集合:我可以收集來自log4j的日誌(消息)到一個Java對象

class LogCollector { 
    List<String> messages; 
} 

這會讓我更容易生成自定義HTML報告(只需將「LogCollector」對象傳遞給某些方法)。如果我可以用Log4J實現這一點,那將是更可取的。

更詳細的問題: 我要做出一些類「LoggingPart」:

class LoggingPart { 
    String name; 
    List<String> messages; 
    List<LoggingPart> parts = new ArrayList<>(); 

    public static void start(String name) { 
     parts.add(new LoggingPart(name)); 

     /* Logic that after every call LoggingPart.start("someName") */ 
     /* in some other class will start collecting messages from */ 
     /* logger into LoggingPart instance of name "someName"  */ 

     /* Report will be generated only of messages from particular */ 
     /* instance with specified name        */ 
    } 
} 
+0

看吧方法:https://www.tutorialspoint.com/log4j/log4j_htmllayout.htm 是不是你想要做什麼? –

+0

好吧,不完全是,但你重定向我有趣的可能的解決方案 - 覆蓋HtmlLayout的方法格式像它這樣[鏈接]上回答(http://stackoverflow.com/questions/12583374/how-to-change-the-htmllayout-in -log4j)。 的事情是,我想在這種情況下,藉此產生由log4j的HTML,撤出它所需的信息(日誌消息)(通過正則表達式?),並在某種程度上,我想格式化。有什麼辦法可以跳過那個「拉動」步驟並直接獲取這些消息嗎? – milosdju

回答

0

我一直推薦的很多事情,都創建自定義TestNG的聽衆之中,但該解決方案是非常簡單的:做一個另外Log4j追加:

  1. 添加追加程序中log4j的配置文件(log4j.properties)
  2. 請參考您的自定義追加呃類將包含用於處理日誌