บทความนี้แนะนำรหัสตัวอย่างของ Springboot ที่รวม SLF4J+log4j2 มันถูกแบ่งปันกับคุณ รายละเอียดมีดังนี้:
การพึ่งพา maven
<!-เพิ่ม log4j2 การพึ่งพา↓-> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Log4J2 </artifactid> <ArtIfactId> Spring-Boot-Starter-Actuator </artifactid> <exclusions> <!-ลบการพึ่งพาบันทึกเก่า-> <exclusion> <sclusion> <sclusion> <RoupID> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Web </artifactid> <exclusions> <!-ลบการพึ่งพาบันทึกเก่า-> <exclusion> </scrusions> </dermentency>
หากการพึ่งพาอื่นมีบันทึกความขัดแย้งบันทึกคุณสามารถเพิ่มการกำหนดค่าต่อไปนี้:
<EXCLUSIONS> <EXCLUSION> <SarroupID> log4j </groupId> <ratifactId>*</artifactId> </การยกเว้น> <exclusion> <sclusion> <sclusion> </sclipusion> <exclusion> <sclusion> <roupId> org.apache.logging.log4j </groupId> <ratifactId>*</artifactid>
log4j2.xml
วางไว้ในไดเรกทอรีทรัพยากร
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <!-ระดับความสำคัญ 6 ระดับคือ: ปิด, ร้ายแรง, ข้อผิดพลาด, เตือน, ข้อมูล, การดีบัก, การติดตาม, ทั้งหมด หากมีการตั้งค่าลำดับความสำคัญเป็นคำเตือนการบันทึกสี่ระดับปิดการเสียชีวิตข้อผิดพลาดและเตือนสามารถส่งออกตามปกติ ซึ่งหมายความว่าไม่ได้บันทึกบันทึกของ log4j2 -> <!-สถานะ: ใช้เพื่อระบุระดับบันทึกการพิมพ์ของ log4j ตัวเอง, monitorinterval: ระบุช่วงเวลาการตรวจสอบสำหรับ log4j กำหนดค่าใหม่โดยอัตโนมัติ-> <สถานะการกำหนดค่า = "ข้อมูล" monitorInterval = "30"> <!-ตั้งค่าคุณสมบัติด้วยตัวเองแล้วเข้าถึงผ่าน $ {}-> < name = "log_home"> $ {web: rootdir}/logs </property> </properties>-> <appenders> <!-appender 1. เอาต์พุตไปยังคอนโซลคอนโซลระบุรูปแบบเอาต์พุตและระดับตัวกรองเป็นข้อมูลที่เพิ่มขึ้น Level = "all" onMatch = "ยอมรับ" onMismatch = "deny"/> <patternlayout pattern = " %d {hh: mm: ss.sss} %-5level %คลาส {36} %l %m- %msg %xex %n"/> </console> <! 3KB และเพียง 10 บันทึกล่าสุดเท่านั้นที่บันทึกไว้-> <file name = "alllog" filename = "$ {log_home} /all.log"> <thresholdFilter ระดับ = "all" onMatch = "ยอมรับ" onmismatch = "deny"/> z} %-5Level %คลาส {36} %l %m- %msg %xex %n "/> </file> <!-Appender 3. ส่งออกไปยังไฟล์ที่บันทึกไว้ซึ่งเงื่อนไขที่เรียกไฟล์บันทึกบันทึกคือไฟล์บันทึก" สูงกว่า 3KB filename = "$ {log_home} /debug.log" filepattern = "$ {log.path} /debug -%i.log"> <thresholdFilter ระดับ = "debug" onMatch = "ยอมรับ" onMismatch = "deny"/>> HH: MM: SS}] [%f:%l]-%m%n "/> <sizebasedtriggeringpolicy ขนาด =" 3kb "/> <!-พารามิเตอร์สูงสุดใน defaultrolloversgygy สามารถ จำกัด คลังเก็บสูงสุดหลังจากขนาดของขนาดเกินขนาด <!-Appender 4. ส่งออกไปยังไฟล์ที่บันทึกไว้ซึ่งเงื่อนไขที่ทริกเกอร์ไฟล์บันทึกบันทึกเป็นเหตุการณ์บันทึกแรกต่อนาที บันทึกข้อผิดพลาดถูกสร้างขึ้นโดยนาที-> <rollingfile name = "errorlog" filename = "$ {log_home} /error.log" filepattern = "$ {log.path}/ข้อผิดพลาด-%d {yyyy-mm-dd_hh-mm} .log" <patternLayout charset = "utf-8" pattern = "[%-5level] [%d {yyyyy-mm-dd hh: mm: ss}] [%c:%f:%l]-%m%n"/> filename = "$ {log_home} /rar.log" filepattern = "$ {log_home}/$$ {วันที่: yyyy-mm}/$ {file_name}-%d {mm-dd-yyyy}-%i.log.gz" HH: MM: SS Z} %-5Level %คลาส {36} %l %m- %msg %xex %n "/> <!-ค่าไฟล์บันทึกสูงสุดถูกบีบอัดในวันถัดไป-> <นโยบาย> <timebasedtriggeringPolicy/> การโหลด-> <loggers> <root level = "debug"> <appender-ref ref = "console"/> <!-<appender-ref ref = "alllog"/>-> <!-<appender-ref ref = "debuglog"/>-> <! </loggers> </การกำหนดค่า> logtest.java
นำเข้า org.slf4j.logger; นำเข้า org.slf4j.loggerFactory; คลาสสาธารณะ logtest {สาธารณะ logger สุดท้าย logger = loggerFactory.getLogger (getClass ()); โมฆะคงที่สาธารณะหลัก (สตริง [] args) {log.trace ("trace"); log.debug ("debug"); log.warn ("เตือน"); log.info ("ข้อมูล"); log.error ("ข้อผิดพลาด"); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น