日志调整

This commit is contained in:
Shikong 2023-04-26 13:59:04 +08:00
parent 9b175c581e
commit 7d5859fa4b
2 changed files with 58 additions and 6 deletions

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
<!--<property name="log.path" value="./log/business_Log" />-->
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<!-- <level>INFO</level>-->
<!-- </filter>-->
<!-- <withJansi>true</withJansi>-->
<encoder>
<!--<pattern>%d %p (%file:%line\)- %m%n</pattern>-->
<!--格式化输出:%d:表示日期 %thread:表示线程名 %-5level:级别从左显示5个字符宽度 %msg:日志消息 %n:是换行符-->
<pattern>%red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %green([%thread]) %highlight(%-5level) %yellow(at %class.%method) (%file:%line\) - %cyan(%msg%n)</pattern>
<!--<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %magenta(%-5level) %green([%-50.50class]) >>> %cyan(%msg) %n</pattern>-->
<charset>UTF-8</charset>
</encoder>
</appender>
<!--&lt;!&ndash;输出到文件&ndash;&gt;-->
<!--<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<!-- <level>INFO</level>-->
<!-- </filter>-->
<!-- <file>${log.path}/logback.log</file>-->
<!-- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
<!-- <fileNamePattern>${log.path}/logback-%d{yyyy-MM-dd-HH-mm}.log</fileNamePattern>-->
<!-- <maxHistory>365</maxHistory>-->
<!-- &lt;!&ndash; <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">&ndash;&gt;-->
<!-- &lt;!&ndash; <maxFileSize>100kB</maxFileSize>&ndash;&gt;-->
<!-- &lt;!&ndash; </timeBasedFileNamingAndTriggeringPolicy>&ndash;&gt;-->
<!-- </rollingPolicy>-->
<!-- <encoder>-->
<!-- &lt;!&ndash;格式化输出:%d:表示日期 %thread:表示线程名 %-5level:级别从左显示5个字符宽度 %msg:日志消息 %n:是换行符&ndash;&gt;-->
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>-->
<!-- <charset>UTF-8</charset>-->
<!-- </encoder>-->
<!--</appender>-->
<!-- 如果appender里没有限定日志级别那么root可以统一设置如果没有配置那么控制台和文件不会输出任何日志这里root的level不做限制-->
<root level="INFO">
<!-- 允许控制台输出-->
<appender-ref ref="console" />
<!--&lt;!&ndash; 允许文件输出&ndash;&gt;-->
<!--<appender-ref ref="file" />-->
</root>
<logger name="cn.skcks.study.springcloud.kafka.producer.controller" level="DEBUG" />
</configuration>

View File

@ -2,14 +2,14 @@ package cn.skcks.study.springcloud.kafka.producer.controller;
import cn.skcks.study.springcloud.kafka.common.model.dto.Message;
import cn.skcks.study.springcloud.utils.JsonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@AllArgsConstructor
public class SimpleController {
@ -17,16 +17,15 @@ public class SimpleController {
final static String TOPIC_NAME = "test";
@GetMapping("/api/v2/{num}")
public void sendMessage1(@PathVariable("num") String num) throws JsonProcessingException {
public void sendMessage1(@PathVariable("num") String num) {
Message message = new Message();
message.setData("这是一个消息,num="+num);
kafkaTemplate.send(TOPIC_NAME,num, JsonUtils.toJson(message)).addCallback(success->{
System.out.println("发送成功");
log.debug("消息 {} 发送成功",message);
},failure->{
System.out.println("发送消息失败:"+failure.getMessage());
log.info("发送消息失败 {}",failure.getMessage());
});
}
}