Di Java Cara Membuat Level Logging Anda sendiri menggunakan Log4j (Mengkonfigurasi Log4j 2)
Diterbitkan: 2017-07-26
Jika Anda perlu menambahkan level logging Anda sendiri di Log4j, maka Anda dapat melakukannya sebagai berikut. Anda harus membuat kelas Anda sendiri yang akan diperluas dari Level
, Level Custom Log Levels
dengan Apache Log4j 2.
Log4j
adalah logging framework
yang sederhana dan fleksibel. Logging melengkapi pengembang dengan konteks rinci untuk kegagalan aplikasi. Dengan log4j dimungkinkan untuk mengaktifkan logging saat runtime tanpa memodifikasi biner aplikasi.
Paket log4j dirancang agar pernyataan ini dapat tetap dalam kode yang dikirimkan tanpa menimbulkan biaya kinerja yang berat.

Log4j memungkinkan permintaan logging untuk mencetak ke beberapa tujuan. Dalam log4j speak, tujuan keluaran disebut appender
. Mereka bervariasi dari konsol, file, komponen GUI, server soket jarak jauh hingga JMS.
File Jar yang Anda butuhkan.
Ini adalah Ketergantungan Maven:
1 2 3 4 5 |
< dependency > < groupId > log4j < / groupId > < artifactId > log4j < / artifactId > < version > 2.16.0 < / version > < / dependency > |
Perbarui Log4j ke versi terbaru
CVE-2021-44228: Apache Log4j2 <=2.14.1 Fitur JNDI yang digunakan dalam konfigurasi, pesan log, dan parameter tidak melindungi dari LDAP yang dikendalikan penyerang dan titik akhir terkait JNDI lainnya.
Dari log4j 2.16.0, perilaku ini telah dinonaktifkan secara default.
Anda harus meletakkan file log4j.xml
di bawah folder /resources
:

Berikut ini contoh Kode Java untuk:
- Log4j Logging untuk level Log kustom di Java
- Membuat level logging Anda sendiri di log4j
- contoh logger kustom log4j
CrunchifyLog4jLevel.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package com . crunchify . tutorials ; import org . apache . log4j . Level ; /** * @author Crunchify.com * */ @SuppressWarnings ( "serial" ) public class CrunchifyLog4jLevel extends Level { /** * Value of CrunchifyLog4jLevel level. This value is lesser than DEBUG_INT and higher * than TRACE_INT} */ public static final int CRUNCHIFY_INT = DEBUG_INT - 10 ; /** * Level representing my log level */ public static final Level CRUNCHIFY = new CrunchifyLog4jLevel ( CRUNCHIFY_INT , "CRUNCHIFY" , 10 ) ; /** * Constructor */ protected CrunchifyLog4jLevel ( int arg0 , String arg1 , int arg2 ) { super ( arg0 , arg1 , arg2 ) ; } /** * Checks whether logArgument is "CRUNCHIFY" level. If yes then returns * CRUNCHIFY}, else calls CrunchifyLog4jLevel#toLevel(String, Level) passing * it Level#DEBUG as the defaultLevel. */ public static Level toLevel ( String logArgument ) { if ( logArgument ! = null && logArgument.toUpperCase().equals("CRUNCHIFY")) { return CRUNCHIFY; } return ( Level ) toLevel ( logArgument , Level . DEBUG ) ; } /** * Checks whether val is CrunchifyLog4jLevel#CRUNCHIFY_INT. If yes then * returns CrunchifyLog4jLevel#CRUNCHIFY, else calls * CrunchifyLog4jLevel#toLevel(int, Level) passing it Level#DEBUG as the * defaultLevel * */ public static Level toLevel ( int val ) { if ( val == CRUNCHIFY_INT ) { return CRUNCHIFY ; } return ( Level ) toLevel ( val , Level . DEBUG ) ; } /** * Checks whether val is CrunchifyLog4jLevel#CRUNCHIFY_INT. If yes * then returns CrunchifyLog4jLevel#CRUNCHIFY, else calls Level#toLevel(int, org.apache.log4j.Level) * */ public static Level toLevel ( int val , Level defaultLevel ) { if ( val == CRUNCHIFY_INT ) { return CRUNCHIFY ; } return Level . toLevel ( val , defaultLevel ) ; } /** * Checks whether logArgument is "CRUNCHIFY" level. If yes then returns * CrunchifyLog4jLevel#CRUNCHIFY, else calls * Level#toLevel(java.lang.String, org.apache.log4j.Level) * */ public static Level toLevel ( String logArgument , Level defaultLevel ) { if ( logArgument ! = null && logArgument.toUpperCase().equals("CRUNCHIFY")) { return CRUNCHIFY; } return Level . toLevel ( logArgument , defaultLevel ) ; } } |
Yang lain harus membaca:

- Bagaimana Memulai Stop Apache Tomcat Server melalui Command Line? (Pengaturan sebagai Layanan Windows)
- Buat dan Deploy Layanan Web dan Klien Layanan Web sederhana di Eclipse
Berikut adalah isi dari File log4j.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<? xml version = "1.0" encoding = "UTF-8" ?> < ! DOCTYPE log4j : configuration SYSTEM "log4j.dtd" > < log4j : configuration xmlns : log4j = "http://jakarta.apache.org/log4j/" debug = "false" > < ! -- FILE Appender -- > < appender name = "FILE" class = "org.apache.log4j.FileAppender" > < param name = "File" value = "c:/crunchify.log" / > < param name = "Append" value = "false" / > < layout class = "org.apache.log4j.PatternLayout" > < param name = "ConversionPattern" value = "%t %-5p %c - %m%n" / > < / layout > < / appender > < ! -- CONSOLE Appender -- > < appender name = "CONSOLE" class = "org.apache.log4j.ConsoleAppender" > < layout class = "org.apache.log4j.PatternLayout" > < param name = "ConversionPattern" value = "%d{ISO8601} %-5p [%c{1}] %m%n" / > < / layout > < / appender > < ! -- Limit Category and Specify Priority -- > < category name = "com.crunchify" > < priority value = "CRUNCHIFY" class = "com.crunchify.tutorials.CrunchifyLog4jLevel" / > < appender - ref ref = "CONSOLE" / > < / category > < ! -- Setup the Root category -- > < root > < appender - ref ref = "CONSOLE" / > < / root > < / log4j : configuration > |
CrunchifyLog4jLevelTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com . crunchify . tutorials ; import org . apache . log4j . Level ; import org . apache . log4j . Logger ; import com . crunchify . tutorials . CrunchifyLog4jLevel ; /** * Tests whether the new log level * com.crunchify.tutorials.CrunchifyLog4jLevel#CRUNCHIFY} is working * * @author Crunchify.com * */ public class CrunchifyLog4jLevelTest { public static void main ( String [ ] args ) { Logger logger = Logger . getLogger ( CrunchifyLog4jLevelTest . class ) ; logger . log ( CrunchifyLog4jLevel . CRUNCHIFY , "I am CrunchifyLog4jLevelTest log" ) ; logger . log ( Level . DEBUG , "I am a DEBUG message" ) ; } } |
Jalankan program pengujian dan Anda akan melihat jenis hasil di bawah ini di Eclipse's Console
Anda.
1 2 |
2013 - 08 - 01 15 : 22 : 36 , 758 CRUNCHIFY [ CrunchifyLog4jLevelTest ] I am CrunchifyLog4jLevelTest log 2013 - 08 - 01 15 : 22 : 36 , 758 DEBUG [ CrunchifyLog4jLevelTest ] I am a DEBUG message |
Nilai int yang Anda tentukan untuk level log Anda adalah penting. Di sini saya telah mendefinisikan level log " CRUNCHIFY
" lebih tinggi dari level DEBUG
tetapi lebih rendah dari level TRACE
yang disediakan oleh log4j.
Jadi, setiap kali Anda telah menetapkan tingkat prioritas ke DEBUG pada kategori (dalam file log4j.xml
Anda), log tingkat CRUNCHIFY
TIDAK akan sampai ke file log.