2013-04-15 72 views
4

我想禁用log4j中記錄所有正在調用的類/方法路徑的部分。例如...如何禁用日誌記錄調用路徑

Apr 15, 2013 10:50:52 AM com.production.tasks.ImportNewOrders checkForOrders 
INFO: ------- Order #295510 
Hibernate: select asset0_.id as id0_, asset0_.AssetID as AssetID0_, asset0_.barcode as barcode0_, asset0_.filename as filename0_, asset0_.orderID as orderID0_, asset0_.Priority as Priority0_, asset0_.qty as qty0_, asset0_.Status as Status0_, asset0_.TimeStamp_Received as TimeStamp9_0_, asset0_.type as type0_, asset0_.URL_Thumb as URL11_0_, asset0_.vendor as vendor0_ from production_queue.3D_Mgmt_v1_Assets asset0_ where asset0_.AssetID=? 
Apr 15, 2013 10:51:04 AM com.production.utility.File download 
INFO:  - DecoFile downloaded from https://secure-url/165054548?user[id]=xxxxx&key=xxxx to th_1107461838.png 
Apr 15, 2013 10:51:17 AM com.production.utility.File download 
INFO:  - DecoFile downloaded from https://secure-url/165054548?user[id]=xxxxx&key=xxxx to 1107461838.png 
Hibernate: insert into production_queue.3D_Mgmt_v1_Assets (AssetID, barcode, filename, orderID, Priority, qty, Status, TimeStamp_Received, type, URL_Thumb, vendor) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
Apr 15, 2013 10:51:17 AM com.production.tasks.ImportNewOrders checkForOrders 

我想成爲......

INFO: ------- Order #295510 
Hibernate: select asset0_.id as id0_, asset0_.AssetID as AssetID0_, asset0_.barcode as barcode0_, asset0_.filename as filename0_, asset0_.orderID as orderID0_, asset0_.Priority as Priority0_, asset0_.qty as qty0_, asset0_.Status as Status0_, asset0_.TimeStamp_Received as TimeStamp9_0_, asset0_.type as type0_, asset0_.URL_Thumb as URL11_0_, asset0_.vendor as vendor0_ from production_queue.3D_Mgmt_v1_Assets asset0_ where asset0_.AssetID=? 
INFO:  - DecoFile downloaded from https://secure-url/165054548?user[id]=xxxxx&key=xxxx to th_1107461838.png 
INFO:  - DecoFile downloaded from https://secure-url/165054548?user[id]=xxxxx&key=xxxx to 1107461838.png 
Hibernate: insert into production_queue.3D_Mgmt_v1_Assets (AssetID, barcode, filename, orderID, Priority, qty, Status, TimeStamp_Received, type, URL_Thumb, vendor) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 

目前我還沒有配置log4j的屬性文件。我想保持INFO被髮送到stdout,但是隻是避免這些方法調用日誌。

更新

因爲我已經創建了一個log4j.properties文件,但我找不到任何設置來禁用方法調用的記錄。

注意;我不是想改變日誌消息的「格式」,而是完全禁用方法調用的日誌記錄。

log4j.rootLogger=INFO, stdout 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c:%L - %m%n 

#prevent "no appenders" warning despite having appenders - http://stackoverflow.com/a/15912258/197606 
log4j.category.org.jboss.logging=INFO, stdout 

#log4j.category.org.springframework=DEBUG,stdout 
log4j.category.com.model.entity =DEBUG,stdout 
+0

你如何設置你的appender?如果您不使用屬性文件,請顯示配置代碼。 – Simulant

+0

什麼是日誌級別2013年4月15日上午10點50分52秒com.production.tasks.ImportNewOrders checkForOrders條目?信息? DEBUG? –

+0

@PiotrKochański這正是我想擺脫的路線。它似乎不受日誌記錄級別的影響。 – Webnet

回答

2

那些不是log4j調用。你所看到的是默認的java.util.logging模式(stderr)。所以你需要適當地配置java.util.logging。見http://tutorials.jenkov.com/java-logging/configuration.html

+0

謝謝,有關如何實現這一目標的任何建議? – Webnet

+0

@Webnet查看更新後的回答鏈接 – artbristol

+0

對我來說,這樣做的工作:'System.setProperty(「java.util.logging.SimpleFormatter.format」,「%4 $ s:%5 $ s%n」);' – Webnet

1

如果您使用的是PatternLayout配置您的追加程序,你應該刪除輸出與完全quallified名調用方法的%l。佈局應該是這個樣子:

PatternLayout layout = new PatternLayout("%-5p %m");//Level and Message 

但我建議把某種時間戳與這樣的:

%d{dd MMM yyyy HH:mm:ss,SSS}

1

如果你沒有log4j屬性文件設置在外部,那麼你的應用程序中的一些組件就以編程方式配置它。無論如何,你需要用你真正想要的東西來覆蓋你現在擁有的所有配置。最簡單且最易於管理的方法是創建一個有效的屬性文件並將其放入您的類路徑中。這樣做不僅可以覆蓋現有記錄器的配置,還可以定義新的記錄器。

如果需要,還可以編程方式修改Log4J配置。通過啓動類中的靜態初始化工具最容易實現。簡單示例:

public class LogTest { 
    private static final Logger logger = Logger.getLogger(LogTest.class); 
    static { 
     Logger.getRootLogger().getLoggerRepository().resetConfiguration(); 
     ConsoleAppender console = new ConsoleAppender(); 
     console.setLayout(new PatternLayout("%-5p %m%n")); 
     console.setThreshold(Level.TRACE); 
     console.activateOptions(); 
     Logger.getRootLogger().addAppender(console); 
    } 

    @Test 
    public void testLogging() throws Exception { 
     logger.info("I am a simplistic info log message"); 
     logger.error("I am a simplistic error log message", 
       new IllegalArgumentException()); 
    } 
}