使用 log4j 从基类和扩展类进行日志记录?

发布于 2024-11-28 22:21:50 字数 1193 浏览 1 评论 0原文

我将 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 技术交流群。

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

发布评论

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

评论(1

柠檬 2024-12-05 22:21:50

您必须在 log4j.xml 中创建不同的 Appender。

每个不同的班级一个。

<logger name="yourBaseClassName" additivity="false">
    <level value="INFO" />
    <appender-ref ref="BasefileAppender" />
</logger>

<appender name="BasefileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="/path/yourfile.log"/>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd MM yyyy HH:mm:ss,SSS} %m%n"/>
    </layout>
</appender>

<logger name="yourExtendedClassName" additivity="false">
    <level value="INFO" />
    <appender-ref ref="ExtendOnefileAppender" />
</logger>

<appender name="ExtendOnefileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="/path/ExtendOnefileAppender.log"/>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd MM yyyy HH:mm:ss,SSS} %m%n"/>
    </layout>
</appender>

然后在您的每个类(基础类和扩展类)中,您都会这样做:

Logger mylogger = Logger.getLogger(MyClass.class.getName())

我认为它不能满足您的第二个要求(但我还想要以某种方式将日志从基类放入这些单独的日志中,具体取决于所使用的类)

但它是一个开始......;-)有了这个你应该能够根据类的名称登录不同的文件。

希望有帮助

You have to create different Appender in your log4j.xml.

One for each different class.

<logger name="yourBaseClassName" additivity="false">
    <level value="INFO" />
    <appender-ref ref="BasefileAppender" />
</logger>

<appender name="BasefileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="/path/yourfile.log"/>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd MM yyyy HH:mm:ss,SSS} %m%n"/>
    </layout>
</appender>

<logger name="yourExtendedClassName" additivity="false">
    <level value="INFO" />
    <appender-ref ref="ExtendOnefileAppender" />
</logger>

<appender name="ExtendOnefileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value="/path/ExtendOnefileAppender.log"/>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd MM yyyy HH:mm:ss,SSS} %m%n"/>
    </layout>
</appender>

then in each of your class (Base and extended) you do:

Logger mylogger = Logger.getLogger(MyClass.class.getName())

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

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