logback日志输出格式设置方式

 更新时间:2023年04月06日 09:06:34   作者:__WanG  
这篇文章主要介绍了logback日志输出格式设置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

部分标签解释

withJansi: 是否配合jansi使用

filter: 日志过滤器

layout: 布局, 配合内置模板使用

pattern: 日志模板, 有内置的日志模板可以直接使用, 例如: ${FILE_LOG_PATTERN}

encoder: 编码器, 支付转码

charset: 支付编码, 一般为utf-8

内置转换器

可以直接通过调用内置的转换器将日志取出来, 打印出来

部分特殊字符串解释

%d: 表示日期

%thread: 表示线程名

%-5level: 级别从左显示5个字符宽度

%msg: 日志消息

%n: 是换行符

更多内置特殊字符

defaultConverterMap.put("d", DateConverter.class.getName());
defaultConverterMap.put("date", DateConverter.class.getName());
defaultConverterMap.put("r", RelativeTimeConverter.class.getName());
defaultConverterMap.put("relative", RelativeTimeConverter.class.getName());
defaultConverterMap.put("level", LevelConverter.class.getName());
defaultConverterMap.put("le", LevelConverter.class.getName());
defaultConverterMap.put("p", LevelConverter.class.getName());
defaultConverterMap.put("t", ThreadConverter.class.getName());
defaultConverterMap.put("thread", ThreadConverter.class.getName());
defaultConverterMap.put("lo", LoggerConverter.class.getName());
defaultConverterMap.put("logger", LoggerConverter.class.getName());
defaultConverterMap.put("c", LoggerConverter.class.getName());
defaultConverterMap.put("m", MessageConverter.class.getName());
defaultConverterMap.put("msg", MessageConverter.class.getName());
defaultConverterMap.put("message", MessageConverter.class.getName());
defaultConverterMap.put("C", ClassOfCallerConverter.class.getName());
defaultConverterMap.put("class", ClassOfCallerConverter.class.getName());
defaultConverterMap.put("M", MethodOfCallerConverter.class.getName());
defaultConverterMap.put("method", MethodOfCallerConverter.class.getName());
defaultConverterMap.put("L", LineOfCallerConverter.class.getName());
defaultConverterMap.put("line", LineOfCallerConverter.class.getName());
defaultConverterMap.put("F", FileOfCallerConverter.class.getName());
defaultConverterMap.put("file", FileOfCallerConverter.class.getName());
defaultConverterMap.put("X", MDCConverter.class.getName());
defaultConverterMap.put("mdc", MDCConverter.class.getName());
defaultConverterMap.put("ex", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("exception", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("rEx", RootCauseFirstThrowableProxyConverter.class.getName());
defaultConverterMap.put("rootException", RootCauseFirstThrowableProxyConverter.class.getName());
defaultConverterMap.put("throwable", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("xEx", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("xException", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("xThrowable", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("nopex", NopThrowableInformationConverter.class.getName());
defaultConverterMap.put("nopexception", NopThrowableInformationConverter.class.getName());
defaultConverterMap.put("cn", ContextNameConverter.class.getName());
defaultConverterMap.put("contextName", ContextNameConverter.class.getName());
defaultConverterMap.put("caller", CallerDataConverter.class.getName());
defaultConverterMap.put("marker", MarkerConverter.class.getName());
defaultConverterMap.put("property", PropertyConverter.class.getName());
defaultConverterMap.put("n", LineSeparatorConverter.class.getName());

如何自定义输出样式

字符颜色定义

使用%加上颜色代号, 然后内容放在后面的括号()里面

例如:

  • %white(这里放输出内容)
  • 配合特殊字符串, 可以输入时间, 线程名等

内置的一些样式

代码示例

使用内置模板

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <filter class="com.wang.cloud.store.common.LogFilter"/>
     <layout class="ch.qos.logback.classic.PatternLayout">
         <pattern>${FILE_LOG_PATTERN}</pattern>
     </layout>
 </appender>

使用自定义模板

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <filter class="com.wang.cloud.store.common.LogFilter"/>
     <withJansi>false</withJansi>
     <encoder>
         <pattern>%white(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%lsn) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{10}) - %cyan(%msg%n)</pattern>
         <charset>utf8</charset>
     </encoder>
 </appender>

总结

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

相关文章

  • ApplicationListenerDetector监听器判断demo

    ApplicationListenerDetector监听器判断demo

    这篇文章主要为大家介绍了ApplicationListenerDetector监听器判断demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Java中String类常用方法详细汇总

    Java中String类常用方法详细汇总

    Java中String类在所有项目开发里面一定会用到,因此String类提供了一系列的功能操作方法,下面这篇文章主要给大家介绍了关于Java中String类常用方法的相关资料,需要的朋友可以参考下
    2023-05-05
  • Java各种内存溢出的问题剖析

    Java各种内存溢出的问题剖析

    本文主要介绍了Java各种内存溢出的问题剖析,了解其根源、排查方法以及有效的修改策略,具有一定的参考价值,感兴趣的可以了解一下
    2025-03-03
  • Session过期后自动跳转到登录页面的实例代码

    Session过期后自动跳转到登录页面的实例代码

    这篇文章主要介绍了Session过期后自动跳转到登录页面实例代码,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Java String对象使用方法详解

    Java String对象使用方法详解

    这篇文章主要介绍了Java String对象使用方法详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • 使用Spirng Boot Admin监控Spring Cloud应用项目

    使用Spirng Boot Admin监控Spring Cloud应用项目

    这篇文章主要介绍了使用Spirng Boot Admin监控Spring Cloud应用项目,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • SpringBoot3整合Swagger3时出现Type javax.servlet.http.H的ttpServletRequest not present错误解决方法

    SpringBoot3整合Swagger3时出现Type javax.servlet.http.H的ttpSe

    这篇文章主要介绍了SpringBoot3整合Swagger3时出现Type javax.servlet.http.H的ttpServletRequest not present错误解决方法,文中有详细的解决方法,需要的朋友可以参考下
    2025-01-01
  • Spring Core核心类库的功能与应用实践分析

    Spring Core核心类库的功能与应用实践分析

    本文详细介绍了SpringCore核心类库的功能、应用实践和底层原理,SpringCore提供了控制反转(IOC)、依赖注入(DI)、Bean管理以及JNDI、定时任务等企业级功能,文章通过多个Java示例展示了SpringCore的应用,感兴趣的朋友跟随小编一起看看吧
    2024-12-12
  • maven打包所有依赖对外提供sdk.jar

    maven打包所有依赖对外提供sdk.jar

    这篇文章主要介绍了maven打包所有依赖,对外提供sdk.jar,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • 虚拟机linux中jdk安装配置方法

    虚拟机linux中jdk安装配置方法

    这篇文章主要为大家详细介绍了虚拟机linux中jdk安装配置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08

最新评论