English 中文(简体)
• 如何建立标志4j 包装和正确记录
原标题:How create log4j wrap and get correct logs

I have multithreaded application and i want add some text information in every log message I create factory and extend class, it works fine

...
protected Logger logger = Logger.getLogger("Test", new MyLog4JFactory());
...

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

public class MyLog4JFactory implements LoggerFactory{
    @Override
    public Logger makeNewLoggerInstance(String arg0) {
        return new MyLogger(arg0);
    }

}


import org.apache.log4j.Logger;
public class MyLogger extends Logger{
    protected MyLogger(String name) {
        super(name);
    }
    private String getMessage(Object msg){
        StringBuffer sb = new StringBuffer();
        return sb.append(msg).append(" ").append("My text").toString();
    }
    @Override
    public void debug(Object message) {
        super.debug(getMessage(message));
    }
    @Override
    public void error(Object message) {
        super.error(getMessage(message));
    }
    @Override
    public void fatal(Object message) {
        super.fatal(getMessage(message));
    }
    @Override
    public void info(Object message) {
        super.info(getMessage(message));
    }
    @Override
    public void warn(Object message) {
        super.warn(getMessage(message));
    }
}

但愿 标识一见包装类别

a. 所有日志

2011-09-08 10:45:49,359德国马克 (35) - 测试1 我的案文

我对记录档案应该做些什么来显示(有线号)有人叫我的记录。

问题回答

我知道这是一个老问题,但我愿赞同我所发现的一点解决办法:

Add FQCN to your class, call Logger.log method and pass it your FQCN like so:

public class MyLogger extends Logger{
    private static String FQCN = MyLogger.class.getName();
    ...

    @Override
    public void debug(Object message) {
        super.log(FQCN, org.apache.log4j.Level.DEBUG,message, null);
    }


    @Override
    public void debug(Object message,Throwable t) {
        super.log(FQCN, org.apache.log4j.Level.DEBUG,message, t);
    }
}

(同样将努力创造一套包装......)

在您的记录4j.properties中,删除了%l<>>specifier from the行文,该行文认为:

log4j.appender.A1.layout.ConversionPattern=<...>

可登录<代码>PatternLayout或其他内容。 这将防止出现完全合格的打电话类别名称和线号码。

More information on the specifiers can be found here.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签