SLF4J最佳做法 [英] SLF4J best practices

查看:135
本文介绍了SLF4J最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个库,并使用sfl4j处理日志记录.

I'm writing a library and I use sfl4j to handle logging.

我认为这是一个好主意,只要每个人都可以提供自己的实现,然后我的应用程序提供的日志将得到正确处理.

I think it's good idea as long as every body can provide it's own implementation and then, the log provided by my application will be handled correctly.

但是我不知道我是否必须提供实现作为传递依赖.

But I don't know if I have to provide an implementation as a transitive dependency or not.

示例:

如果我仅提供:

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

我的库的用户可以选择实现,但是如果他只是添加我的库而不读取配置,它将无法正常工作

The user of my lib can choose the implementation, but if he just add my lib without reading configuration, it will not work

否则,如果我提供:

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

仅当向其添加依赖项时,我的库才能正常工作,但是如果用户要使用其他slf4j,则他必须排除我的库.

My lib will work correctly just when adding dependency to it, but If the user want to use an other slf4j, he will have to exclude mine.

您对此有何看法?

推荐答案

从不提供日志实现.客户端应用程序必须这样做.否则,这将违反关注点分离.不要对未知的客户做任何假设.

You never provide a log implementation. The client application has to do so. Otherwhise this would be a violation of separation of concerns. Don't do any assumptions about an unknown client.

这篇关于SLF4J最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆