当前位置:网站首页>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!
边栏推荐
- 【雲計算重點複習】
- Single precision, double precision and precision (Reprint)
- Oracle之trim,ltrim,rtrim三个函数的用法
- 雷达导引头伺服系统的建模与仿真
- 线程池的三大方法
- Single cell literature learning (Part2) -- stplus: a reference based method for the exact enhancement of St
- Frame profiling
- 401 string (344. reverse string, 541. reverse string II, Title: Sword finger offer 05. replace spaces, 151. reverse words in string)
- 什么是JUC
- W800芯片平台进入OpenHarmony主干
猜你喜欢

SQLServer中的子查询

【NAND文件系统】UBI介绍

reduce_ Reduction in sum()_ indices

不务正业系列7:老照片去除斑点手法

SSM整合所需配置文件及常见配置错误引起的报错

Unity development - scene asynchronous loading

Little bear school bearpi HM micro officially integrated into openharmony trunk

Unity app improves device availability
![[NAND file system] UBI introduction](/img/69/7213b8b39cebc1626eb6bb8cc10d16.png)
[NAND file system] UBI introduction

On the definition of jinja2 macro
随机推荐
Array and foreach traversal in C #
为什么我选择 Rust
C#中的泛型
Research on automatic landing control system of carrier aircraft
Callable
ReadWriteLock
W800芯片平台进入OpenHarmony主干
Use of stopwatch
单细胞文献学习(part3)--DSTG: deconvoluting spatial transcriptomics data through graph-based AI
DataBricks从开源到商业化踩过的坑
单细胞文献学习(part2)--stPlus: a reference-based method for the accurate enhancement of ST
Lock锁(重点)
[technical notes]
不务正业系列7:老照片去除斑点手法
文献记录(part106)--GRAPH AUTO-ENCODER VIA NEIGHBORHOOD WASSERSTEIN RECONSTRUCTION
CMake 入门级别语法
虚职、架空、拖后腿,大厂开源办公室到底什么样?
【雲計算重點複習】
Modeling and Simulation of Radar Seeker Servo System
Empirical mode decomposition (EMD) and Hilbert Huang transform (HHT)