欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决引用slf4j中Logger.info只打印出文字没有数据的问题

程序员文章站 2022-06-19 13:28:57
目录slf4j logger.info只打印出文字没有数据解决方案启用设置org.slf4j.logger打印并输出日志pom.xml配置logback.xml配置程序调用slf4j logger.i...

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(?,?,?)]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。