温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么解决引用slf4j中Logger.info没有数据的问题

发布时间:2021-12-27 16:59:16 来源:亿速云 阅读:147 作者:iii 栏目:开发技术

这篇文章主要介绍“怎么解决引用slf4j中Logger.info没有数据的问题”,在日常操作中,相信很多人在怎么解决引用slf4j中Logger.info没有数据的问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么解决引用slf4j中Logger.info没有数据的问题”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

slf4j Logger.info只打印出文字没有数据

引的是 slf4j 包

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static Logger logger = LoggerFactory.getLogger(TsfTest.class);
logger.info("打印参数:",map);

只能打印出:

2019-06-14 17:52:07.246 [http-apr-8080-exec-10] INFO c.q.m.p.b.rest.test - 打印参数:

解决方案

在第一个参数中加入花括号{ }即可。

logger.info("打印参数:{}",map);

解决!

启用设置org.slf4j.Logger打印并输出日志

在resouces目录下面新建logback.xml(此为Logback推荐目录)

内容配置如下

logback 分为两种设置:

1. 输出到控制台 STDOUT

2. 输出到文件 FILE

pom.xml配置

<properties>
    <slf4j.version>1.7.25</slf4j.version>
</properties>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>

logback.xml配置

下面的配置同时配置输出到文件和输出到控制台

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="3 seconds">
    <!--设置日志输出为控制台-->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{userId}] [%X{requestId}] %logger - %msg%n</pattern>
        </encoder>
    </appender>

    <!--设置日志输出为文件-->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>logFile.log</File>
        <rollingPolicy  class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>logFile.%d{yyyy-MM-dd_HH-mm}.log.zip</FileNamePattern>
        </rollingPolicy>

        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss,SSS} [%thread] %-5level %logger{32} - %msg%n</Pattern>
        </layout>
    </appender>

    <root>
        <level value="DEBUG"/>
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

程序调用

1.申明 logger 变量

private Logger logger = LoggerFactory.getLogger(LoginLogDao.class);

2.在程序中调用日志

logger.debug(INSERT_LOGIN_LOG_SQL);

官方介绍网址:https://logback.qos.ch/demo.html

下面为官网介绍

logback-classic with two appenders: a ConsoleAppender and a RollingFileAppender. The RollingFileAppender sends logging events to a file called logFile.log and will rollover the active file every minute. The old file will be renamed and compressed to a zip file. The ConsoleAppender will output the logging requests to the console, and shorten the logger names to gain space on the console window, without loss of legibility. For example, ch.qos.logback.demo.prime.NumberCruncherImpl will be abbreviated as c.q.l.d.prime.NumberCruncherImpl.

输出结果如下

isDebugEnabled true
2017-04-23 23:58:35,502 DEBUG [http-nio-8080-exec-6] (LoginLogDao.java:32) - INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)
2017-04-23 23:58:35,503 DEBUG [http-nio-8080-exec-6] (JdbcTemplate.java:869) - Executing prepared SQL update
2017-04-23 23:58:35,503 DEBUG [http-nio-8080-exec-6] (JdbcTemplate.java:616) - Executing prepared SQL statement [INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)]

到此,关于“怎么解决引用slf4j中Logger.info没有数据的问题”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI