简体   繁体   English

Log4j修剪常用类别前缀

[英]Log4j trim common category prefix

I have a project that uses Apache Commons Logging & log4j with a large number of classes doing logging. 我有一个使用Apache Commons Logging和log4j的项目,其中有大量的类正在进行日志记录。 95% of my logs show up with the same prefix 95%的日志显示相同的前缀

log4j.appender.MyCompany.layout.ConversionPattern=[%d][%-5p][%c] %m%n log4j.appender.MyCompany.layout.ConversionPattern = [%d] [% - 5p] [%c]%m%n

[2010-08-05 11:44:18,940][DEBUG][com.mycompany.projectname.config.XMLConfigSource] Loading config from [filepath]
[2010-08-05 12:05:52,715][INFO ][com.mycompany.projectname.management.ManagerCore] Log entry 1
[2010-08-05 12:05:52,717][INFO ][com.mycompany.projectname.management.ManagerCore] Log entry 2

I know with %c{1}, I can just show the last part of the category (ie the class name), but is there a way to trim off the common portion 'com.mycompany.projectname' from each log under that package, considering how much room it takes up on each line? 我知道%c {1},我可以只显示类别的最后一部分(即类名),但有没有办法从该包下的每个日志中删除公共部分'com.mycompany.projectname' ,考虑每条线路占用多少空间?

If you are using Log4j 1.2.16, you can change your layout to EnhancedPatternLayout , which lets you specify a negative value for the category parameter. 如果您使用的是Log4j 1.2.16,则可以将布局更改为EnhancedPatternLayout ,从而可以为category参数指定负值 From the docs: 来自文档:

For example, for the category name "alpha.beta.gamma" ... %c{-2} will remove two elements [from the front] leaving "gamma" 例如,对于类别名称“alpha.beta.gamma”...%c {-2}将删除 [从前面]的两个元素 ,留下“gamma”

Here is a more complete example: 这是一个更完整的例子:

log4j.appender.C.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.C.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%t] [%c{-3}] %m%n

which in your case should chop off com.mycompany.projectname . 在你的情况下,应该砍掉com.mycompany.projectname

However, this will apply to every message that is logged, even if it didn't come from your code. 但是,这将适用于记录的每条消息,即使它不是来自您的代码。 In other words, the category org.hibernate.engine.query.HQLQueryPlan would be trimmed to query.HQLQueryPlan , which may not be what you want. 换句话说,类别org.hibernate.engine.query.HQLQueryPlan将被修剪为query.HQLQueryPlan ,这可能不是您想要的。

If you need absolute control over this (ie you want to specifically strip out the text "com.mycompany.projectname" from every message), then you will need to implement your own Layout class. 如果您需要对此进行绝对控制(即您希望从每条消息中专门删除文本“com.mycompany.projectname”),那么您将需要实现自己的Layout类。 Something like this should do it: 这样的事情应该这样做:

import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

public class MyPatternLayout extends PatternLayout
{
  @Override
  public String format(LoggingEvent event)
  {
    String msg = super.format(event);
    msg = msg.replace("com.mycompany.projectname", "");

    return msg;
  }
}

Good luck! 祝好运!

Use %c{2} or %C{2}. 使用%c {2}或%C {2}。 The number specifies the number of right-most components to keep. 该数字指定要保留的最右侧组件的数量。

Refer to http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html . 请参阅http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

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

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