General Category Prefix

I have a project that uses Apache Commons Logging and log4j with a lot of logging classes. 95% of my logs are displayed with the same prefix

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 (that is, the class name), but is there a way to trim the common part of "com.mycompany.projectname" from each journal under this package, given how much space each one takes line?

+3
source share
2 answers

If you use Log4j 1.2.16, you can change your layout to EnhancedPatternLayout , which allows you to specify a negative value for the category parameter. From the docs:

For example, for the category name "alpha.beta.gamma" ...% c {-2} will remove two elements [in front], leaving "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 disable com.mycompany.projectname.

every, , . , org.hibernate.engine.query.HQLQueryPlan query.HQLQueryPlan, , .

(.. "com.mycompany.projectname" ), Layout. - :

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

!

+3

Source: https://habr.com/ru/post/1758277/


All Articles