如何创建 log4j 换行并获取正确的日志

发布于 2024-12-03 12:13:17 字数 1400 浏览 0 评论 0原文

我有多线程应用程序,我想在每条日志消息中添加一些文本信息 我创建工厂并扩展类,它工作正常

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

但是!在日志中我看到包装类

所有日志打印为

2011-09-08 10:45:49,359 DEBUG MyLogger (35) - Test1 我的文本

我应该如何处理日志文件显示类(带行号)调用我的记录器?

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

but! in the logs I see the wrapper class

all logs print as

2011-09-08 10:45:49,359 DEBUG MyLogger (35) - Test1 My text

What should I do to the log file shows a classes (with line number) calls my logger?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2024-12-10 12:13:17

我知道这是一个老问题,但我想分享我发现的一个很好的解决方案:

将 FQCN 添加到您的班级中,
调用 Logger.log 方法并将您的 FQCN 传递给它
像这样:(

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

同样适用于创建包装器......)

I know this is an old question, but I would like to share a nice solution I found:

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

(same will work for creating a wrapper...)

清欢 2024-12-10 12:13:17

在 log4j.properties 文件中,从如下所示的行中删除 %l 说明符:

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

ConversionPattern 可以是 PatternLayout 或其他内容。这将阻止显示完全限定的调用类名称和行号。

有关说明符的更多信息可以在此处找到。

In your log4j.properties file, remove the %l specifier from the line that looks something like this:

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

ConversionPattern could be PatternLayout or something else. This will prevent the fully qualified calling class name and line number from being displayed.

More information on the specifiers can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文