Artikel ini memperkenalkan contoh kode Springboot yang mengintegrasikan SLF4J+LOG4J2. Itu dibagikan dengan Anda. Dengan rincian sebagai berikut:
Ketergantungan Maven
<!-Tingkatkan ketergantungan log4j2 ↓-> <dependency> <GroupId> org.springframework.boot </groupid> <ArtifactId> </ArtiF-starter-starter-starter-starter-org.springframework. <Ecplusions> <!-Hapus dependensi log lama-> <scuxcusion> <GroupId> org.springframework.boot </proupid> <RaintifactId> Spring-boot-starter-logging </artifactid> </cexcpringfr. <ArTifactId> Spring-boot-starter-web </stifactid> <celuctions> <!-Hapus dependensi log lama-> <Celuction> <Groupid> org.springframework.boot </groupid> <ArTifactId> Spring-boot-starter-logging </arttifactid> </eksklusif> </eksklusif>
Jika dependensi lain memiliki konflik log log, Anda dapat menambahkan konfigurasi berikut:
<exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>*</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </Exclusion> <Excuplusion> <GroupId> org.apache.logging.log4j </groupid> <ArTifactId>*</artifactid> </eksklusi> </eksklusi>
log4j2.xml
Masukkan ke direktori sumber daya
<? Xml Version = "1.0" encoding = "UTF-8"?> <!-6 tingkat prioritas adalah: off, fatal, kesalahan, peringatkan, info, debug, jejak, semua. Jika prioritas diatur untuk memperingatkan, maka empat tingkat log, off, fatal, kesalahan, dan peringatan, dapat menghasilkan secara normal untuk mati. Ini berarti bahwa log Log4J2 tidak direkam. -> <!-Status: Digunakan untuk menentukan tingkat log pencetakan log4j itu sendiri, MonitorInterval: Tentukan waktu interval pemantauan untuk log4j secara otomatis dikonfigurasi ulang-> <status konfigurasi = "info" monitorInterval = "30"> <!-Atur properti sendiri, dan kemudian aksesnya melalui $ {}-> <!-Properties> <Properties> <Properti name="LOG_HOME">${web:rootDir}/logs</property> </properties>--> <appenders> <!--Appender 1. Output to the Console console, specify the output format and filter level as INFO --> <Console name="Console" target="SYSTEM_OUT"> <!--ThresholdFilter Specifies the lowest level of output of log messages --> <ThresholdFilter level = "semua" onMatch = "terima" onmismatch = "deny"/> <polalayout pola = " %d {hh: mm: ss.sss} %-5 level %kelas {36} %l %m- %msg %xex %n"/> </console> <!-appender 2. output ke scroll %n "/> </console> <!-appender 2. output ke scroll dari scroll %n"/</console> <!-appender 2. output ke scroll %n "/</console> <!-appender 2. output ke scroll %n"/</console> <!-appender 2. output ke scroll %n "/</console> <!-appender 2. output ke scroll %n"/</console> <!-appender 2. output ke scroll dari the scroll n " 3KB, dan hanya 10 log terbaru yang disimpan-> <file name = "alllog" filename = "$ {log_home} /all.log"> <thresholdfilter level = "semua" onMatch = "terima" pola = "mm. z} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <!--Appender 3. Export to the scroll saved file, The condition that triggers the save log file is that the log file is greater than 3KB, and only the latest 10 logs are saved --> <RollingFile name="debugLog" FileName = "$ {log_home} /debug.log" filepattern = "$ {log.path} /debug-%i.log"> <thresholdFilter level = "debug" onMatch = "terima" onmismatch = "tolak"/> <patternlayout charset = "devel-mmismatch =" dENY "/<POLALAYOUT charset =" devel-mmismatch = "%-%-%{%-level {%-mmisMatch =" DENTE "/<POLEDLEUDOUT/POLAT-MMISMATCH ="%-mmISMATCH = "DEN"/POLEDED (POLDED "/POLED (POLEDLEVET = POLED (POLE, HH: mm: ss}] [%f:%l]-%m%n "/> <sizedtriggeringpolicy size =" 3kb "/> <!-parameter maks di defaultrolloverstrategy dapat membatasi arsip maks setelah ukuran 4. Ekspor ke file yang disimpan gulir, kondisi yang memicu file log simpan adalah acara log pertama per menit. ERROR logs are generated by minutes --> <RollingFile name="errorLog" fileName="${LOG_HOME}/error.log" filePattern="${log.path}/error-%d{yyyy-MM-dd_HH-mm}.log"> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <PolaLayout charset = "utf-8" pola = "[%-5Level] [%d {yyyyy-mm-dd hh: mm: ss}] [%c:%f:%l]-%m%n"/> <timebasedtriggerpolicy/> </rollingfile> <Rollingfile name = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile = "Rollingfile =" Rollingfile "Rollingfile" FileName = "$ {log_home} /rar.log" filepattern = "$ {log_home}/$$ {date: yyyy-mm}/$ {file_name}-%d {mm-dd-yyyy}-%i.log.gz"> <Patternlayout charset = "{BOBLOD" {{POLA-MM-mm-mm HH: mm: ss z} %-5 level %kelas {36} %l %m- %msg %xex %n "/> <!-Nilai file log maksimum dikompresi pada hari berikutnya-> </10 roadedtriggerpolicy/</rolledtriggerpolicy ukuran </10 mb"/> </rolledtriggerpolice> </rolledpolicy ukuran </10 mb "//</rolledtrigerperfies> </rolledpolicy size =" 10 mb "//> </rolledtrigerpligerpolice> </10 mb"//</rolledrigerple ukuran kolkum = "10 mb"//> </rolledtrigerpligerpolicy> memuat-> <toggers> <root level = "debug"> <appender-ref ref = "konsol"/> <!-<appender-ref ref = "alllog"/>-> <!-<appent-ref ref = "debuglog"/>-> <!-<appender-ref ref = "eraRLOG"/-> <!-<! </toggers> </configuration> Logtest.java
impor org.slf4j.logger; impor org.slf4j.loggerFactory; LogTest kelas publik {Public Final Logger Logger = LoggerFactory.getLogger (getClass ()); public static void main (string [] args) {log.trace ("trace"); log.debug ("debug"); log.warn ("warn"); log.info ("info"); log.error ("error"); }}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.