前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >java SLF4J 使用其他的 log框架

java SLF4J 使用其他的 log框架

作者头像
bear_fish
发布于 2018-09-19 04:39:12
发布于 2018-09-19 04:39:12
77500
代码可运行
举报
运行总次数:0
代码可运行

http://saltnlight5.blogspot.com/2013/08/how-to-configure-slf4j-with-different.html

How to configure SLF4J with different logger implementations

There are many good benefits in using slf4j library as your Java application logging API layer. Here I will show few 

examples on how to use and configure it with different loggers.

You can think of slf4j as an Java interface, and then you would need an implementation (ONLY ONE) at runtime to

 provide the actual logging details, such as writing to STDOUT or to a file etc. Each logging implementation 

(or called binding) would obviously have their own way of configuring the log output, but your application will

 remain agnostic and always use the same org.slf4j.Logger API. Let’s see how this works in practice.

Using slf4j with Simple logger

Create a Maven based project and this in your pom.xml.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>

Now you may use Logger in your Java code like this.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package deng;
import org.slf4j.*;
public class Hello {
    static Logger LOGGER = LoggerFactory.getLogger(Hello.class);
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++)
            if (i % 2 == 0)
                LOGGER.info("Hello {}", i);
            else
                LOGGER.debug("I am on index {}", i);
    }
}

The above will get your program compiled, but when you run it, you will see these output.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
bash> java deng.Hello
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

What it’s saying is that at runtime, you are missing the logging "implementation" (or the logger binding), 

so slf4j simply use a "NOP" implmentation, which does nothing. In order to see the output properly, you 

may try use an simple implementation that does not require any configuration at all! Just go back to your

pom.xml and add the following:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.5</version>
    </dependency>

Now you see logging output on STDOUT with INFO level. This simple logger will default show any

 INFO level message or higher. In order to see DEBUG messages, you would need to pass in this System Property -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG at your Java startup.

Using slf4j with Log4j logger

Now we can experiment and swap different logger implementations, but your application 

code can remain the same. All we need is to replace slf4j-simple with another popular logger implementation, 

such as the Log4j.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>

Again, we must configure logging per implementation that we picked. In this case, we need an src/main/resources/log4j.propertiesfile.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    log4j.rootLogger=DEBUG, STDOUT
    log4j.logger.deng=INFO
    log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
    log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
    log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Re-run your program, and you should see similar output.

Using slf4j with JDK logger

The JDK actually comes with a logger package, and you can replace pom.xml with this logger implementation.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.5</version>
    </dependency>

Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such as src/main/resources/logging.properties, but you would also need to add a System properties -Djava.util.logging.config.file=logging.properties in order to have it pick it up. Here is 

an example to get you started:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
.level=INFO
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
deng.level=FINEST

Using slf4j with Logback logger

The logback logger implementation is a super dupa quality implementation. If you intend to write

 serious code that go into production, you may want to evaluate this option. Again modify your pom.xml to replace with this:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
    </dependency>

Here is a sample of configuration src/main/resources/logback.xml to get things started.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="deng" level="DEBUG"/>

  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Writing your own library with slf4j logger

If you are providing an Java library for large end users consumption, it’s good idea to set your project to depend on slf4j-apionly, and then let your user choose any logger implementation at their development or runtime environment. As end users, they may quickly select one of option above and take advatage of their own favorite logging implementation features.

References

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年01月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
2017 面向游戏玩家的八款最佳 Linux 发行版
十一小长假到了,不知各位有什么安排呢?如果不想去外面添堵,在家玩一些小游戏也是不错的选择。面向 Linux 的游戏在过去几年中发生了很大的变化,现在已经有数十种发行版为游戏玩家进行了重点优化。 接下来,推荐几款最新的,也是经过测试挑选后选择出来的最好的面向游戏玩家的 Linux 发行版。 1、SteamOS SteamOS 似乎总是排在各个 Linux 游戏发行版列表的第一个。它专为游戏设计考虑,预装了 Steam,基于 Debian。 默认情况下,SteamOS 仅安装 Steam,但你也可
小小科
2018/05/02
2.7K0
2017 面向游戏玩家的八款最佳 Linux 发行版
最佳 Linux 发行版汇总
Linux入门 Ubuntu Ubuntu是一款基于Debian发行版,以Unity作为默认桌面环境的Linux操作系统。他是世界上最流行的发行版之一,最新发行版为桌面、移动及其桌面移动混合版的优化。
小小科
2018/05/04
7K0
最佳 Linux 发行版汇总
闲置笔记本变身 Steam OS 游戏主机
这篇文章或许是中文网络第一篇,详细介绍如何在普通 PC 设备上安装 Steam OS 新版本系统,希望能为中文玩家提供一个实用的参考指南。
soulteary
2025/02/05
6530
闲置笔记本变身 Steam OS 游戏主机
2017 linux版本排行榜,最好用的linux在这里
Linux的阵营日益壮大,每一款发行版都拥有一大批用户,开发者自愿为相关项目投入精力。Linux发行版可谓是形形色色,它们旨在满足每一种能想得到的需求。 本文就是为了简述某一款发行版为何存在、该发行版的目标用户是哪些,以及它与其他发行版相比有什么样的特殊功能。 最好的回归发行版:openSUSE openSUSE 背后的 SUSE 公司是最老的 Linux 企业,它成立于 Linus Torvalds 宣布放出 Linux 的一年后。它其实早于 Red Hat 的诞生,它也是社区主导的发行版 openS
小小科
2018/05/03
4.2K0
2017 linux版本排行榜,最好用的linux在这里
2023 | 10个最佳的Linux发行版本
下载链接:https://ubuntu.com/download/desktop/
Linux兵工厂
2023/03/30
3.2K0
2023 | 10个最佳的Linux发行版本
2023 年 10 个最受欢迎的 Linux 发行版
在本文[1]中,我们将根据 Distrowatch 的使用统计数据和市场份额,按降序排列截至 2023 年 5 月 18 日的前 10 个最受欢迎的 Linux 发行版。
数据科学工厂
2023/08/10
4K0
2023 年 10 个最受欢迎的 Linux 发行版
挑选指南:2020最佳Linux发行版鉴赏
传统上,Linux被认为是面向编码人员和程序员的操作系统,但是多年来,人们一直在进行尝试,使Linux对普通消费者更具吸引力,这不仅仅是由于消费者普遍不满意Windows安全问题、苹果的“围墙花园”。
用户6543014
2019/10/27
4.5K0
树莓派入门(一)—— 树莓派4B介绍
树莓派由注册于英国的慈善组织“Raspberry Pi 基金会”开发,Eben·Upton/埃·厄普顿为项目带头人。2012年3月,英国剑桥大学埃本·阿普顿(Eben Epton)正式发售世界上最小的台式机,又称卡片式电脑,外形只有信用卡大小,却具有电脑的所有基本功能,这就是Raspberry Pi电脑板,中文译名”树莓派”。
全栈程序员站长
2022/08/12
20.8K0
树莓派入门(一)—— 树莓派4B介绍
Linux发行版:CentOS、Ubuntu、RedHat、Android、Tizen、MeeGo
Linux,最早由Linus Benedict Torvalds在1991年开始编写。在这之前,Richard Stallman创建了Free Software Foundation(FSF)组织以及GNU项目,并不断的编写创建GNU程序(此类程序的许可方式均为GPL: General Public License)。在不断的有杰出的程序员和开发者加入到GNU组织中后,便造就了今天我们所看到的Linux,或称GNU/Linux。 Linux的发行版本可以大体分为两类,一类是商业公司维护的发行版本,一类是社区组织维护的发行版本
阳光岛主
2019/02/19
19.2K0
Linux发行版:CentOS、Ubuntu、RedHat、Android、Tizen、MeeGo
11个用于编程和开发的最佳Linux发行版[2021版]
基于Linux的操作系统是最受欢迎的,并且最适合开发人员和程序员。大多数开发人员使用不同的Linux发行版来迅速完成工作并创建新的东西,但是作为我自己的前开发人员,我主要担心的一个问题是“我应该选择哪一个Linux发行版 ?” 同样,选择Linux发行版进行编程或软件开发时要考虑的其他一些问题是兼容性,功能,稳定性和灵活性。
生信交流平台
2022/09/21
4.7K0
11个用于编程和开发的最佳Linux发行版[2021版]
2023 年的十款最佳 Linux 发行版
Linux 是一个开源操作系统内核,它被广泛用于各种设备和系统上。随着时间的推移,许多基于 Linux 内核的发行版也应运而生。在本文中,我们将详细介绍 2023 年的十款最佳 Linux 发行版。这些发行版在功能、易用性和社区支持等方面都有出色的表现。让我们一起来了解这些发行版。
网络技术联盟站
2023/07/13
6.4K0
2023 年的十款最佳 Linux 发行版
最全Linux的发行版简介,一文读懂各发行版之间的联系和区别
发行版及版本比较 三大家族: Fedora是基于RHEL,CentOS,Scientific Linux, 和Oracle Linux的社区版本。相比RHEL,Fedora打包了显著的更多的软件包。其中一个原因是,多样化的社区参与Fedora的建设;它不只是一家公司。在这个过程中,CentOS用于活动,演示和实验,因为它是对最终用户免费提供的,并具有比Fedora的一个更长的发布周期(通常每隔半年左右发布一个新版本)。 SUSE, SUSE Linux Enterpri
小小科
2018/05/02
8.2K0
最全Linux的发行版简介,一文读懂各发行版之间的联系和区别
各发行版Linux介绍
可能这是最著名的Linux版本了,Red Hat Linux已经创造了自己的品牌,越来越多的人听说过它。Red Hat在1994年创业,当时聘用了全世界500多名员工,他们都致力于开放的源代码体系。
星哥玩云
2022/09/15
3.3K0
各发行版Linux介绍
常见Linux系统下载【发行版】
Linux Mint 由Linux Mint Team团队于2006年开始发行,是一份基于Debian和Ubuntu的Linux发行版。其目标是提供一种更完整的即刻可用体验,这包括提供浏览器插件、多媒体编解码器、对DVD播放的支持、Java和其他组件,它也增加了一套定制桌面及各种菜单,一些独特的配置工具,以及一份基于web的软件包安装界面。Linux Mint是对用户友好而功能强大的操作系统。它诞生的目的是为家庭用户和企业提供一个免费的,易用的,舒适而优雅的桌面操作系统。
Lcry
2022/11/29
10.9K0
这 5 个 Linux 发行版,2021 年再错过就是罪过了
综合自:https://medium.com/dev-genius/top-5-linux-distros-for-developers-in-2021-598311af201a
逆锋起笔
2021/03/17
1K0
Oreon项目是一个更友善的企业Linux发行版
Oreon 项目是一个基于 AlmaLinux 的 Linux 发行版,它使 SELinux 更易于使用。
云云众生s
2025/02/17
790
Oreon项目是一个更友善的企业Linux发行版
这8款 Linux 发行版绝对适合学生群体,看看有没有你喜欢的!
学生在寻找适合自己的 Linux 发行版时,会考虑很多因素,其中包括用户友好性、稳定性、定制化以及预装应用程序的可用性,这些因素会有助于他们的使用。
网络技术联盟站
2023/03/12
1.2K0
这8款 Linux 发行版绝对适合学生群体,看看有没有你喜欢的!
2020年linux桌面系统盘点
MX Linux是基于Debian的“稳定”分支的面向桌面的Linux发行版,是antiX与以前的MEPIS Linux社区之间的合作企业。它使用Xfce作为默认桌面,是一种中等重量的操作系统,旨在将优雅高效的桌面与简单的配置,高稳定性,稳定的性能和中等大小的占地面积相结合。
程序那些事儿
2023/03/07
12.6K0
2020年linux桌面系统盘点
Linux ,越折腾越喜欢
今天这波分享可以说是自己热血来潮。起因是自己收到某个问答社区小秘的问题邀请。问题是《谁能给我推荐几本linux的书?从基础到进阶提高的linux书?》。自己挺怀念大学那段折腾 Linux 的时光,所以就忍不住评论一波。可曾没有想到,这一评论的阅读量有 1.5 W,有些网友想要鸟哥 Linux 的学习视频,私信我以及到公众号后台留言。
猴哥yuri
2018/08/16
1.2K0
Linux 10个主流发行版本
这里只表示做个记录。其实相关的介绍已经很多了。但是还是想怀缅一下青春。这10个Linux发行版来源于国外网站(最后给出链接)。它列出了10个Linux发行版(包含一个FreeBSD,Linux的胞兄弟),通常被认为是全球Linux用户最广泛使用的。当然并没有经过详细的数字统计,大家仍然可以选择其他的发行版。但是一般来讲,这几个都有非常活跃的论坛或邮件列表,遇到困难,也能比较迅速的解决。
狼啸风云
2019/10/24
18.7K0
推荐阅读
相关推荐
2017 面向游戏玩家的八款最佳 Linux 发行版
更多 >
LV.1
中南林业科技大学学生
目录
  • How to configure SLF4J with different logger implementations
  • Using slf4j with Simple logger
  • Using slf4j with Log4j logger
  • Using slf4j with JDK logger
  • Using slf4j with Logback logger
  • Writing your own library with slf4j logger
  • References
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档