使用 log4j 从基类和扩展类进行日志记录?
我将 slf4j 与 log4j 一起使用,并且遇到以下情况:
package x.y.z.base;
public class FTPOperationsBase {
private final Logger log = LoggerFactory.getLogger(FTPOperationsBase.class);
protected FTPClient ftp;
// some more fields
public void connect() {
...
// log all connect exceptions
}
public void disconnect() {
...
// log all disconnect exceptions
};
}
可以看出,包是xyzbase。现在我有几个类扩展了这个基类(这些类当前位于包 xyzclassA、xyzclasB 等中)。
我想要每个类都有单独的日志文件,因为它们充当单独的模块。但我还想要的是以某种方式将日志从基类放入这些单独的日志中,具体取决于正在使用哪个类,即如果只有一个子类正在执行,我不希望在所有日志中记录形式基类< /强>。例如,如果我扩展了 class:
public class FTPExtendedClass1
extends FTPOperationsBase {
public void doSomething() {...}
}
并制作以下代码序列:
FTPExtendedClass1 obj1 = new FTPExtendedClass1();
obj1.connect();
obj1.doSomething();
obj1.disconnect();
我希望这种情况下的日志文件如下:
- connect() 中可能的信息/警告/错误
- doSomething( 中可能的信息/警告/错误)
- 来自disconnect()的可能的信息/警告/错误
这应该是所有扩展类的情况,其中doSomething()是特定于类的方法。有什么方法可以为这种行为组织包/配置 log4j 吗?
谢谢。
I'm using slf4j with log4j and I have the following situation:
package x.y.z.base;
public class FTPOperationsBase {
private final Logger log = LoggerFactory.getLogger(FTPOperationsBase.class);
protected FTPClient ftp;
// some more fields
public void connect() {
...
// log all connect exceptions
}
public void disconnect() {
...
// log all disconnect exceptions
};
}
As it can be seen, package is x.y.z.base. Now I have several classes which extend this base class (these classes are currently in packages x.y.z.classA, x.y.z.clasB,... etc).
I want separate log file for each of these classes, as they act as separate modules. But what I also want is to somehow put log from base class to these separate logs, depending on which class is being used, that is I don't want log form base class in all logs if only one subclass is executing. For example, if I have extended class:
public class FTPExtendedClass1
extends FTPOperationsBase {
public void doSomething() {...}
}
and make the following code sequence:
FTPExtendedClass1 obj1 = new FTPExtendedClass1();
obj1.connect();
obj1.doSomething();
obj1.disconnect();
I'd like to have the log file for this case as follows:
- possible info/warnings/errors from connect()
- possible info/warnings/errors from doSomething()
- possible info/warnings/errors from disconnect()
This should be the case for all extended classes, where doSomething() is class-specific method. Any way to organize packages / configure log4j for this behavior ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在 log4j.xml 中创建不同的 Appender。
每个不同的班级一个。
然后在您的每个类(基础类和扩展类)中,您都会这样做:
我认为它不能满足您的第二个要求(但我还想要以某种方式将日志从基类放入这些单独的日志中,具体取决于所使用的类)
但它是一个开始......;-)有了这个你应该能够根据类的名称登录不同的文件。
希望有帮助
You have to create different Appender in your log4j.xml.
One for each different class.
then in each of your class (Base and extended) you do:
I don't think it cover your second requirement (But what I also want is to somehow put log from base class to these separate logs, depending on which class is being used)
but its a start... ;-) with this you should be able to log in different file based on the name of the class.
Hope it help