当前位置:网站首页>Logback custom pattern parameter resolution
Logback custom pattern parameter resolution
2022-06-22 06:14:00 【Ruoyu 1919】
demand
The logged in user name should be included in the printed log .
- The most stupid way : Every time the log is printed , Add the user name manually
- workable proposition : Intercept request , Write the user name to MDC, And then in Pattern Pass through %X{username} Print
- loading X The way to : Intercept request , Write the user name to ThreadLocal, And then in Pattern Pass through %u Customized in this Pattern Print
Customize Pattern
append Configuration of
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.example.demo.log.UserLogbackLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-10u] [%thread] %-5level %logger{36} -%msg%n</Pattern>
</layout>
</encoder>
</appender>
Be careful Pattern Inside [%-10u], We just want to use %u To output the user name
UserLogbackLayout
public class UserLogbackLayout extends PatternLayout {
// Nothing , Is to register a pair of parameters u analytically converter
static {
DEFAULT_CONVERTER_MAP.put("u", LogbackUserPatternConverter.class.getName());
}
}
LogbackUserPatternConverter
public class LogbackUserPatternConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent iLoggingEvent) {
// Nothing , Just from ThreadLocal Fetching users from
return UserHolder.get();
}
}
Test it

so easy!
边栏推荐
- 【自己动手写CPU】异常相关指令的实现
- pip升级难题(已解决)You are using pip version 19.0.3, however version 22.1.2 is available.
- Single precision, double precision and precision (Reprint)
- Machine learning concept sorting (no formula)
- 虚职、架空、拖后腿,大厂开源办公室到底什么样?
- ReadWriteLock
- Vulkan pre rotation processing equipment direction
- Research on dynamics and control of single ball robot
- 富设备平台突破:基于RK3568的DAYU200进入OpenHarmony 3.1 Release主干
- 常用的辅助类—(重点)
猜你喜欢

从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?

Pyg tutorial (7): dissecting neighborhood aggregation
![[NAND file system] UBI introduction](/img/69/7213b8b39cebc1626eb6bb8cc10d16.png)
[NAND file system] UBI introduction

401 string (344. reverse string, 541. reverse string II, Title: Sword finger offer 05. replace spaces, 151. reverse words in string)

基于卫星测深的牙买加沿岸水深测量

Creating GLSL Shaders at Runtime in Unity3D

idea本地运行scope

W800 chip platform enters openharmony backbone

Single cell paper records (Part8) -- cell2location maps fine grained cell types in spatial transcriptomics

反射操作注解
随机推荐
不务正业系列7:老照片去除斑点手法
Little bear school bearpi HM micro officially integrated into openharmony trunk
生信文献学习(part1)--PRECISE: a ... approach to transfer predictors of drug response from pre-clinical ...
集合类并发不安全问题
线程池的七大参数及自定义线程池
Callable
Mail sending function is realized through SMTP protocol and exchange
Logback自定义Pattern参数解析
Modeling and Simulation of Radar Seeker Servo System
常用的辅助类—(重点)
Surfer格网文件裁剪
Detailed interpretation of tab[i = (n - 1) & hash]
No business series 7: removing spots from old photos
【Rust笔记】03-引用
OSG compiling osgqt
单细胞文献学习(part2)--stPlus: a reference-based method for the accurate enhancement of ST
Shengxin visualization (Part2) -- box diagram
五大常考SQL面试题
Use of idea plug-in EASYCODE
类加载内存分析