W Javie Jak stworzyć własny poziom rejestrowania za pomocą Log4j (Konfiguracja Log4j 2)
Opublikowany: 2017-07-26
Jeśli potrzebujesz dodać własny poziom rejestrowania w Log4j, możesz to zrobić w następujący sposób. Będziesz musiał stworzyć własną klasę, która rozszerzy się z Level
, Custom Log Levels
z Apache Log4j 2.
Log4j
to prosta i elastyczna logging framework
. Rejestrowanie zapewnia programiście szczegółowy kontekst awarii aplikacji. Dzięki log4j możliwe jest włączenie rejestrowania w czasie wykonywania bez modyfikowania pliku binarnego aplikacji.
Pakiet log4j został zaprojektowany tak, aby te instrukcje mogły pozostać w dostarczonym kodzie bez ponoszenia dużych kosztów wydajności.

Log4j umożliwia logowanie żądań drukowania do wielu miejsc docelowych. W mowie log4j docelowe miejsce docelowe jest nazywane appender
. Różnią się one od konsoli, plików, komponentów GUI, zdalnych serwerów gniazd po JMS.
Plik Jar, którego potrzebujesz.
Oto zależność od Mavena:
1 2 3 4 5 |
< dependency > < groupId > log4j < / groupId > < artifactId > log4j < / artifactId > < version > 2.16.0 < / version > < / dependency > |
Zaktualizuj Log4j do najnowszej wersji
CVE-2021-44228: Apache Log4j2 <=2.14.1 Funkcje JNDI używane w konfiguracji, komunikaty dziennika i parametry nie chronią przed kontrolowanym przez atakującego LDAP i innymi punktami końcowymi związanymi z JNDI.
Od log4j 2.16.0 to zachowanie zostało domyślnie wyłączone.
Musisz umieścić plik log4j.xml
w folderze /resources
:

Oto przykładowy kod Java dla:
- Log4j Logowanie dla niestandardowych poziomów logów w Javie
- Tworzenie własnego poziomu logowania w log4j
- Przykład niestandardowego rejestratora 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 ) ; } } |
Inny musi przeczytać:

- Jak uruchomić Stop Apache Tomcat Server za pomocą wiersza poleceń? (Konfiguracja jako usługa Windows)
- Twórz i wdrażaj prostą usługę sieciową i klienta usługi sieciowej w środowisku Eclipse
Oto zawartość pliku 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" ) ; } } |
Uruchom program testowy i powinieneś zobaczyć poniżej typ wyniku w Eclipse's Console
.
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 |
Ważna jest wartość int określona dla poziomu rejestrowania. Tutaj zdefiniowałem poziom dziennika „ CRUNCHIFY
” jako wyższy niż poziom DEBUG
, ale niższy niż poziom TRACE
dostarczany przez log4j.
Tak więc za każdym razem, gdy ustawisz poziom priorytetu na DEBUG w kategorii (w pliku log4j.xml
), logi poziomu CRUNCHIFY
NIE trafią do pliku dziennika.