In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to achieve log4j log format to add custom field information", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to achieve log4j log format to add custom field information" this article.
Add custom field information to log4j log format
When using log4j logs, sometimes you need to add custom field information to the log, such as the information of logged-in users, etc. You can use org.apache.log4j.MDC to implement this feature:
1. In the Action method, directly use the
MDC.put ("username", getUserInfo (). GetName ()); / / getUserInfo () to get login user information
2. In the configuration file of log4j
% d {yyyy/MM/dd HH:mm:ss.sss} [% X {username}] [%-5p]% c -% m% n
Log file results:
2013-11-29 1800 com.test.TestAction 25.025 [test] [ERROR] com.test.TestAction / / Red test is the user information to join.
Note: the red parts must be consistent.
Custom field types for log4j2 storage
The database log table in the project contains fields of type number, and it is found that only string types can be stored by default. No relevant help information can be found on the Internet.
Only a few attributes are provided in the official API document
Set isNumber= "true"
The attempt to set the property isNumber= "true" turned out to be successful, with the following code:
After testing the login interface, take a look at the database and find that the fields of number type are stored.
The above is all the contents of the article "how to add custom field information to the log4j log format". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.