تقدم هذه المقالة مثال رمز Springboot دمج SLF4J+Log4J2. تتم مشاركتها معك. التفاصيل كما يلي:
تبعيات مافن
<!-زيادة log4j2 التبعية ↓-> <redency> <roupiD> org.springframework.boot </groupId> <StifactId> Spring-boot-starter-log4j2 </suntifactid> </preminency> <sperence> <StifactId> Spring-boot-starter-actuator </artifactid> <issistions> <!-إزالة تبعيات السجل القديم-> <Section> <rouckId> org.springframework.boot </rougiD> </atfactid> spring-boot-starter-loggter </artifactid> <roughid> org.springframework.boot </groupId> <StifactId> Spring-Boot-Starter-Web </shintifactid> <quartipions> <!-إزالة التبعيات القديمة السجل-> <Sective> </requortions> </premency>
إذا كانت التبعيات الأخرى تحتوي على تعارضات سجل ، فيمكنك إضافة التكوين التالي:
<Sextasions> <Sective> <rougiD> log4j </rougiD> <SnifactId>*</artifactId> </section> <section> <rouckId> org.slf4j </rougiD> <StifactId> </artifactid> </section> <Section> <roupiD> org.apache.logging.log4j </rougiD> <StifactId>*</shintifactid> </assclusion> </exsdarusions>
log4j2.xml
ضعها في دليل الموارد
<؟ إذا تم تعيين الأولوية لتحذيرها ، فإن المستويات الأربعة من السجلات ، والخروج ، والخطأ ، والخطأ ، والتحذير ، يمكن أن تنتج بشكل طبيعي إلى إيقاف التشغيل. هذا يعني أن سجلات log4j2 لم يتم تسجيلها. -> <!-الحالة: تستخدم لتحديد مستوى سجل الطباعة من log4j نفسه ، مراقبة: حدد وقت المراقبة وقت المراقبة لـ Log4J إعادة تلقائي-> <status configuration = "info" monitorInterVal = "30"> <!-اضبط الخصائص بنفسك ، ثم الوصول إليها من خلال $ {}- name = "log_home"> $ {web: rootdir}/سجلات </property> </sprenties>-> <appenders> <!-Appender 1. الإخراج إلى وحدة التحكم ، حدد تنسيق الإخراج ومستوى المرشح كأقل من الرسائل- Level = "all" onMatch = "قبول" onMismatch = "deny"/> <patternayout pattern = "٪ d {hh: mm: ss.sss} ٪ -5Level ٪ class {36} ٪ l ٪ m-٪ msg ٪ xex n"/> </console> <! ولا يتم حفظ أحدث 10 سجلات-> <file name = "alllog" fileName = "$ {log_home}/all.log"> <thresholdfilter level = "all" onMatch = "Accept" onMismatch = "deny"/> <patternlayout charset = "utf-8" pattern = "٪ d {yyyyy. ٪ -5LEVEL ٪ class {36} ٪ l ٪ m-٪ msg ٪ xex ٪ n "/> </file> <!-Appender 3. التصدير إلى الملف المحفوظ للتمرير ، والشرط الذي يؤدي إلى ملف السجل حفظ هو أن ملف السجل أكبر من 3 كيلو بايت ، وآخر 10 سجلات يتم حفظها-> <rollingfile name =" debuglog " filename = "$ {log_home} /debug.log" filepattern = "$ {log.path} /debug-٪i.log"> <thresholdfilter lister = "debug" onMatch = "onmismatch =" deny "/> <patternayout charset =" utf-8 " HH: mm: ss}] [٪ f: ٪ l]-٪ m ٪ n "/> <SiverseBatedTriggeringPolicy size =" 3kb "/> <!-المعلمة MAX في defaultrolloverstrategy يمكن أن تحد من أرشيف max بعد أن يتجاوز الحجم الحجم في sizebasedtriggeringpolicy- <!-Addender 4. التصدير إلى الملف المحفوظ للتمرير ، فإن الشرط الذي يؤدي إلى تشغيل ملف السجل Save هو أول حدث سجل في الدقيقة. يتم إنشاء سجلات الخطأ بالدقائق-> <rollingfile name = "errorlog" filename = "$ {log_home} /error.log" filepattern = "$ {log.path}/error- ٪ d {yyyy-mm-dd_hh-mm} .log charset = "utf-8" pattern = "[٪-5Level] [٪ d {yyyyy-mm-dd hh: mm: ss}] filepattern = "$ {log_home}/$$ {date: yyyy-mm}/$ {file_name}-٪ d {mm-dd-yyyy}-٪ ٪ class {36} ٪ l ٪ m-٪ msg ٪ xex ٪ n "/> <!-يتم ضغط أقصى قيمة ملف السجل في اليوم التالي-> <السياسات> <TimeBasedTriggeringPolicy/> <SiverseBatedTriggeringPolicy size =" 10 mb "/> </solicies> </rollingfile> level = "debug"> <appender-ref ref = "console"/> <!-<appender-ref Ref = "alllog"/>-> <!-<appender-ref ref = "debuglog"/>-> <! logtest.java
استيراد org.slf4j.logger ؛ استيراد org.slf4j.loggerfactory ؛ سجل الفئة العامة {public 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 ("خطأ") ؛ }}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.