简体   繁体   English

如何禁用日志记录调用路径

[英]How to disable logging call paths

I'd like to disable the portion of log4j that logs all class/method paths that are being called. 我想禁用log4j中记录所有被调用的类/方法路径的部分。 For instance... 例如...

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

I would like to be... 我想成为...

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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Currently I haven't configured a log4j properties file. 目前我还没有配置log4j属性文件。 I want to keep INFO being sent to stdout as it is, but just avoid these method calls logs. 我想保持INFO按原样发送到stdout ,但只是避免使用这些方法调用日志。

update 更新

I've since created a log4j.properties file, but I can't find any settings to disable the logging of method calls. 我已经创建了一个log4j.properties文件,但是我找不到任何设置来禁用方法调用的日志记录。

NOTE; 注意; I'm not trying to change the "format" of log messages, but disable the logging of method calls altogether. 我不是要改变日志消息的“格式”,而是完全禁用方法调用的记录。

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

Those are not log4j calls. 那些不是log4j调用。 What you're seeing is the default java.util.logging pattern (to stderr). 你所看到的是默认的java.util.logging模式(到stderr)。 So you need to configure java.util.logging appropriately. 所以你需要适当地配置java.util.logging。 See http://tutorials.jenkov.com/java-logging/configuration.html 请参见http://tutorials.jenkov.com/java-logging/configuration.html

If you are using a PatternLayout to configure your Appender, you should remove the %l that outputs the calling method with the fully quallified name. 如果您使用PatternLayout配置Appender,则应删除使用完全quallified名称输出调用方法的%l Your Layout should be look something like this: 你的布局应该是这样的:

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

But I recommend to put in some kind of a time stamp with something of this: 但是我建议在某种时间戳上添加一些:

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

If you do not have a log4j properties file setup externally then some component in your app is programmatically configuring it. 如果您没有在外部设置log4j属性文件,则应用程序中的某个组件将以编程方式配置它。 In any case, you need to override whatever current configuration you have now with what you really want. 在任何情况下,您都需要使用您真正想要的内容覆盖现有的任何当前配置。 The easiest and most manageable way to do this would be to create a valid properties file and place it in your class path. 最简单,最易于管理的方法是创建一个有效的属性文件并将其放在类路径中。 Doing so will allow you to not only override the configuration of existing loggers, but also to define new ones. 这样做不仅可以覆盖现有记录器的配置,还可以定义新的记录器。

If needed, you can also programmatically modify your Log4J configuration. 如果需要,您还可以以编程方式修改Log4J配置。 Easiest accomplished via a static initializer in startup class. 通过启动类中的静态初始化程序最容易完成。 Simple example: 简单的例子:

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM