이 기사에서는 SLF4J+LOG4J2를 통합하는 SpringBoot의 예제 코드를 소개합니다. 그것은 당신과 공유됩니다. 세부 사항은 다음과 같습니다.
Maven 의존성
<!-Log4J2 의존성 증가 ↓-> <pectionency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-log4j2 </artifactid> </fexendency> <pectionency> <groupid> org.springframework.boot </groupId> <artifactid> spring-sporat-staratiD> <Exclusions> <!-기존 로그 종속성 제거-> <pexclusion> <groupId> org.springframework.boot </groupId> <artifactid> Spring-Boot-Starter-Logging </artifactid> </exclusion> </fexclusions> </fexendency> <groupid> <groupid> org.springframework.springframework. <아티 팩트> 스프링 부트 스타터-위버 </artifactid> <exclusions> <!-오래된 로그 종속성 제거-> <exclusion> <groupid> org.springframework.boot </groupId> <artifactID> 스프링 부트 스타터-로깅 </artifactid> </explusions>
다른 종속성에 로그 로그 충돌이있는 경우 다음 구성을 추가 할 수 있습니다.
<제외> <제외> <groupId> log4J </groupId> <artifactid>*</artifactid> </arevericid> </exclusion> <groupid> org.slf4j </groupid> <artifactid>*</artifactid> </exclusion> <groupid> <groupid> </artifact> </groupid> </groupid> </제외> <제외> <groupid> org.apache.logging.log4j </groupid> <artifactid>*</artifactid> </exclusion> </exclusions>
log4j2.xml
리소스 디렉토리에 넣으십시오
<? xml version = "1.0"encoding = "utf-8"?> <!-6 가지 우선 순위 레벨은 다음과 같습니다. OFF, 치명적, 오류, 경고, 정보, 디버그, 트레이스, 모두입니다. 우선 순위가 경고되도록 설정되면 4 단계의 로그, OFF, 치명적, 오류 및 경고는 정상적으로 출력 할 수 있습니다. 이것은 log4J2의 로그가 기록되지 않았 음을 의미합니다. -> <!-상태 : Log4J 자체의 인쇄 로그 레벨을 지정하는 데 사용됩니다. MonitorInterval : Log4J의 모니터링 간격 시간을 자동으로 재구성했습니다-> <configuration status = "info"monitorInterVal = "30"> <!-속성을 직접 설정 한 다음 $ {}->-<속성을 통해 액세스하십시오. 이름 = "log_home"> $ {web : rootdir}/logs </property> </properties>-> <pactenders> <!-Appender 1. 콘솔 콘솔에 출력, 출력 형식 및 필터 레벨을 정보로 지정합니다-> <콘솔 이름 = "Console"target = "System_out">-thresholdfilter는 낮은 Log ress ~ who log ressing of Output of Output of Outputs of Outputs of the Outs of Outputs of level = "all"all "onmatch ="accept "onmismatch ="deny "/> <PatternLayout Pattern =" %d {HH : MM : Ss.sss} %-5level %class {36} %l %m- %m- %msg %xex %n "/> </console> <!-Applender 2보다 로그 파일이 더 큽니다. 3KB, 그리고 최신 10 개의 로그 만 저장됩니다-> <file name = "AllLog"filename = "$ {log_home}/all.log"> <thresholdfilter level = "all"onmatch = "encept"onmistatch = "deny"/> <patternlayout = "utf-8"pattern = "hyyyy.mm.mm.mm.mm.mm.mm.mm.mm. z} %-5level %class {36} %l %m- %msg %xex %n "/> </file> <!-Appender 3. 스크롤 저장된 파일로 내보내기, 로그 파일을 트리거하는 조건은 로그 파일이 3kb보다 크고 최신 10 개의 로그 만 저장된다는 것입니다. filename = "$ {log_home} /debug.log"filepattern = "$ {log.path} /debug-%i.log"> <thresholdfilter level = "debug"onmatch = "encept"onmistatch = "deny"/> <patternlayout charset = "utf-8"[%] HH : MM : SS}] [%f :%l]-%m%n "/> <sizebasedtriggeringpolicy size ="3KB "/> <!-defaultrollOverStrategy의 매개 변수 max는 크기가 sizebasedtriggeringpolicy-> defaultrolloverStrategy max에서 크기를 초과 한 후 최대 아카이브를 제한 할 수 있습니다. 스크롤 저장된 파일로 내보내는 것은 저장 로그 파일을 트리거하는 조건이 분당 첫 번째 로그 이벤트입니다. 오류 로그는 몇 분마다 생성됩니다-> <RollingFile name = "ErrorLog"filename = "$ {log_home} /error.log"filepattern = "$ {log.path}/error-%d {yyyy-mm-dd_hh-mm} .Log"> <ThresholdFilter level = "orr. <PatternLayout charset = "utf-8"pattern = "[%-5level] [%d {yyyyy-mm-dd hh : mm : mm : ss}] [%c :%f :%l]-%m%n"/> <TimeBasedTriggeringPolicy/> </rollingfile> <RollingFile Name = "RollingFile" filename = "$ {log_home} /rar.log"filepattern = "$ {log_home}/$$ {date : yyyy-mm}/$ {file_name}-%d {mm-dd-yyyy}-%i.log.gz "> <utf-8"%d {yyyyy charset = "utf-8" HH : MM : SS Z} %-5level %class {36} %l %m- %msg %xex %n "/> <!-다음 날 최대 로그 파일 값은 압축됩니다-> <policies> <timebasedtriggeringpolicy/> <sizebasedtriggeringpolicy size ="10 mb "/> </rollingfile> </rollingfile> 로드-> <gogers> <root level = "debug"> <Appender-Ref ref = "console"/> <!-<Appender-Ref ref = "AllLog"/>-> <!-<Appender-Ref = "Debuglog"/>-> <!-<appender-ref = "errorlog"/>->-<applender-ref ref = "rollingfile">-< </loggers> </configuration> logtest.java
import org.slf4j.logger; import org.slf4j.loggerfactory; public class logtest {public final logger = loggerfactory.getLogger (getClass ()); public static void main (String [] args) {log.trace ( "Trace"); log.debug ( "디버그"); log.warn ( "warn"); log.info ( "정보"); log.error ( "오류"); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.