如何让OpenEJB使用slf4j?

发布于 2024-10-01 20:45:23 字数 190 浏览 1 评论 0原文

任何人都可以给出一个 pom.xml 依赖项配置的示例,该配置将使 OpenEJB 使用 slf4j 日志记录,而不是 JCL (据我所知,这就是它现在使用的)。

另请参阅如何配置 OpenEJB 日志记录?

Can anyone give an example of pom.xml dependencies configuration that will make OpenEJB to use slf4j logging, instead of JCL (this is what it uses now, as I understand).

see also How to configure OpenEJB logging?

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

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

发布评论

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

评论(6

倾城花音 2024-10-08 20:45:24

我们支持:

LogStreamFactory 可以通过将类名设置为 openejb.log.factory 系统属性的值来插入。

请随意复制现有的实现之一并更新 JCL 或 SLF4J。如果您也有尝试其他事情的冲动,该项目总是接受新的提交者!

We have support for:

New implementations of LogStreamFactory can be plugged in by setting the class name as the value of openejb.log.factory system property.

Feel free to copy one of the existing impls and update for either JCL or SLF4J. The project is always accepting new committers if you get the urge to hack on other things as well!

脱离于你 2024-10-08 20:45:24

感谢大卫的指点,这实际上很容易。您只需要实现两个类:
- Slf4jLogStreamFactory
- Slf4jLogStream

然后设置你的工厂:
System.setProperty("openejb.log.factory", "de.glauche.Slf4jLogStreamFactory");

package de.glauche;

import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.LogStream;
import org.apache.openejb.util.LogStreamFactory;

public class Slf4jLogStreamFactory implements LogStreamFactory {

    @Override
    public LogStream createLogStream(LogCategory logCategory) {
        return new Slf4jLogStream(logCategory);
    }

}

和 Slf4jLogStream:

package de.glauche;

import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.LogStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Slf4jLogStream implements LogStream {
    private Logger log;

    public Slf4jLogStream(LogCategory logCategory) {
        log = LoggerFactory.getLogger(logCategory.getName());
    }

    @Override
    public boolean isFatalEnabled() {
        return log.isErrorEnabled();
    }

    @Override
    public void fatal(String message) {
        log.error(message);
    }

    @Override
    public void fatal(String message, Throwable t) {
        log.error(message,t);
    }
        ... (overwrite the remaining methods like this)

有了这个,我可以通过 slf4j 在我的 logback 记录器中获得格式良好的所有输出:)

Thanks to David's pointers, this is actually quite easy. You only need to implement two clases:
- Slf4jLogStreamFactory
- Slf4jLogStream

then set your factory :
System.setProperty("openejb.log.factory", "de.glauche.Slf4jLogStreamFactory");

package de.glauche;

import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.LogStream;
import org.apache.openejb.util.LogStreamFactory;

public class Slf4jLogStreamFactory implements LogStreamFactory {

    @Override
    public LogStream createLogStream(LogCategory logCategory) {
        return new Slf4jLogStream(logCategory);
    }

}

and the Slf4jLogStream:

package de.glauche;

import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.LogStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Slf4jLogStream implements LogStream {
    private Logger log;

    public Slf4jLogStream(LogCategory logCategory) {
        log = LoggerFactory.getLogger(logCategory.getName());
    }

    @Override
    public boolean isFatalEnabled() {
        return log.isErrorEnabled();
    }

    @Override
    public void fatal(String message) {
        log.error(message);
    }

    @Override
    public void fatal(String message, Throwable t) {
        log.error(message,t);
    }
        ... (overwrite the remaining methods like this)

With this i'm getting all output nicely formated in my logback logger via slf4j :)

﹂绝世的画 2024-10-08 20:45:24

这就是我使 OpenEJB 使用外部日志记录的方法:

[...]
@Before
public void before() throws Exception {
  System.setProperty("openejb.logger.external", "true");
  InitialContext ctx = new InitialContext();
}
[...]

也许可以将此系统属性移动到某些全局资源,例如 pom.xml

This is how I made OpenEJB to use external logging:

[...]
@Before
public void before() throws Exception {
  System.setProperty("openejb.logger.external", "true");
  InitialContext ctx = new InitialContext();
}
[...]

Maybe it's possible to move this system property to some global resource, like pom.xml?

抚你发端 2024-10-08 20:45:24

如果您添加 API 和 JCL 依赖项,那么使用 SLF4J API 的日志记录将定向到默认的 JCL 日志。

这就是你想要的吗?或者您想使用其他后端进行日志记录吗?

 <dependencies>
   <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jcl</artifactId>
        <version>1.6.1</version>
    </dependency>
</dependencies>

If you add the API and JCL dependencies then your logging using the SLF4J API will get directed to the default JCL logs.

Is that what you want? Or do you want to use some other back end for the logging?

 <dependencies>
   <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jcl</artifactId>
        <version>1.6.1</version>
    </dependency>
</dependencies>
〃安静 2024-10-08 20:45:24

不是 Maven 专家,但根据我的理解,您想要这样:

<dependency>
    <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
    <version>1.6.1</version>
</dependency>

如果 OpenEJB 不支持 JCL,那么添加该依赖项将毫无意义,因此请使用 JUL 或 Log4j。

您可以在 Maven 存储库 中找到更多详细信息。

如果您确实想使用 JCL 桥,请使用以下命令:

<dependency>
    <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.1</version>
</dependency>

Not a Maven expert, but from my understanding, you want this:

<dependency>
    <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
    <version>1.6.1</version>
</dependency>

If OpenEJB doesn't support JCL, it would be pointless to add that dependency, so go with JUL or Log4j.

You can find more details at Maven repository.

Just in case you really want to use JCL bridge, use this:

<dependency>
    <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.1</version>
</dependency>
请叫√我孤独 2024-10-08 20:45:24

如果您通过 Maven 的 exec 插件执行,请尝试以下操作:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
       <mainClass>your Mainclass here</mainClass>
       <systemProperties>
          <systemProperty>
             <key>openejb.log.factory</key>
             <value>slf4j</value>
          </systemProperty>
       </systemProperties>
    </configuration>
 </plugin>

If you are executing via Maven's exec plugin, try this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
       <mainClass>your Mainclass here</mainClass>
       <systemProperties>
          <systemProperty>
             <key>openejb.log.factory</key>
             <value>slf4j</value>
          </systemProperty>
       </systemProperties>
    </configuration>
 </plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文