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 属性文件。我想保持stdout原样发送 INFO,但只是避免这些方法调用日志。

更新

我已经创建了一个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
4

3 回答 3

2

这些不是 log4j 调用。您看到的是默认的 java.util.logging 模式(到 stderr)。所以你需要适当地配置 java.util.logging。请参阅http://tutorials.jenkov.com/java-logging/configuration.html

于 2013-04-25T18:53:08.163 回答
1

如果您使用PatternLayout来配置 Appender,您应该删除%l输出具有完全限定名称的调用方法的 。您的布局应如下所示:

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

但我建议使用以下内容添加某种时间戳:

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

于 2013-04-17T20:53:14.603 回答
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());
    }
}
于 2013-04-21T09:57:32.693 回答