如何创建 log4j 换行并获取正确的日志
我有多线程应用程序,我想在每条日志消息中添加一些文本信息 我创建工厂并扩展类,它工作正常
...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这是一个老问题,但我想分享我发现的一个很好的解决方案:
将 FQCN 添加到您的班级中,
调用 Logger.log 方法并将您的 FQCN 传递给它
像这样:(
同样适用于创建包装器......)
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:
(same will work for creating a wrapper...)
在 log4j.properties 文件中,从如下所示的行中删除
%l
说明符:ConversionPattern
可以是PatternLayout
或其他内容。这将阻止显示完全限定的调用类名称和行号。有关说明符的更多信息可以在此处找到。
In your log4j.properties file, remove the
%l
specifier from the line that looks something like this:ConversionPattern
could bePatternLayout
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.