Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use logback to customize deviceId and generate respective log files according to deviceId

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use logback to customize deviceId and generate their own log files according to deviceId". In daily operation, I believe that many people have doubts about how to use logback to customize deviceId and generate their own log files according to deviceId. The editor consulted all kinds of materials and sorted out simple and useful operation methods, hoping to answer "how to use logback to customize deviceId". And generate their own log files according to deviceId "confusion helps!" Next, please follow the editor to study!

1. Screenshot of hierarchical directory

2.pom dependence Note-- idea downloads lombok plug-in org.projectlombok lombok 1.16.16 org.slf4j slf4j-api 1.7.253.logback file% d {HH:mm:ss.SSS} [% thread]% level% logger {35} -% msg%n deviceId unknown True [% d {yyyy-MM-dd HH:mm:ss.SSS}]%-5level% logger {35} -% msg%n D:/mylogs/%d {yyyyMMdd} / deviceId-$ {deviceId} -% i.log 500MB 60 20GB INFO 4. Test class import org.apache.log4j.MDC;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class TestLogback {public static final Logger logger = LoggerFactory.getLogger (TestLogback.class); public void test1 () {MDC.put ("deviceId", "deviceId3"); logger.debug ("deviceId3 connected"); MDC.put ("deviceId", "deviceId4"); logger.error ("deviceId4-connected") MDC.put ("deviceId", "deviceId5"); logger.info ("deviceId5-connected");} public void test2 () {MDC.put ("deviceId", "deviceId2"); logger.trace ("deviceId2 connected");} public static void main (String [] args) {final TestLogback testLogback = new TestLogback (); testLogback.test1 (); testLogback.test2 ();}} 5. Effect screenshot

Note: you need to get a unique identification of the device before printing the log with log. The point is:

MDC.put ("deviceId", "deviceId3"); / / generate a log file based on deviceId3, append data if any, or create the file if not. DeviceId3 is to print the unique identification logger.debug ("deviceId3 connected"); / / MDC.put ("deviceId", "deviceId3"); for the first step, logger.debug ("deviceId3 connected"); print the log for the second bu to this, on "how to use logback custom deviceId, and generate their own log files according to deviceId" study is over, hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report