În Java Cum să vă creați propriul nivel de înregistrare folosind Log4j (Configurarea Log4j 2)
Publicat: 2017-07-26
Dacă trebuie să adăugați propriul nivel de înregistrare în Log4j, atunci puteți face acest lucru după cum urmează. Va trebui să vă creați propria clasă care se va extinde de la Level
, Custom Log Levels
cu Apache Log4j 2.
Log4j
este un logging framework
simplu și flexibil. Logging-ul oferă dezvoltatorului un context detaliat pentru eșecurile aplicației. Cu log4j este posibil să activați înregistrarea în timp de execuție fără a modifica binarul aplicației.
Pachetul log4j este conceput astfel încât aceste declarații să poată rămâne în codul livrat fără a suporta un cost mare de performanță.

Log4j permite ca cererile de înregistrare să fie imprimate către mai multe destinații. În log4j speak, o destinație de ieșire se numește un appender
. Acestea variază de la consolă, fișiere, componente GUI, servere socket la distanță la JMS.
Fișierul Jar de care aveți nevoie.
Iată o dependență de Maven:
1 2 3 4 5 |
< dependency > < groupId > log4j < / groupId > < artifactId > log4j < / artifactId > < version > 2.16.0 < / version > < / dependency > |
Actualizați Log4j la cea mai recentă versiune
CVE-2021-44228: Apache Log4j2 <=2.14.1 Caracteristicile JNDI utilizate în configurare, mesajele de jurnal și parametrii nu protejează împotriva LDAP controlat de atacator și a altor puncte finale legate de JNDI.
Din log4j 2.16.0, acest comportament a fost dezactivat implicit.
Trebuie să puneți fișierul log4j.xml
în folderul /resources
:

Iată un exemplu de cod Java pentru:
- Log4j Logare pentru niveluri de jurnal personalizate în Java
- Crearea propriului nivel de înregistrare în log4j
- exemplu log4j log4j personalizat
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 ) ; } } |
Altul trebuie să citească:

- Cum se pornește oprirea serverului Apache Tomcat prin linia de comandă? (Configurați ca serviciu Windows)
- Creați și implementați un serviciu web simplu și un client pentru servicii web în Eclipse
Aici este conținutul fișierului 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" ) ; } } |
Rulați programul de testare și ar trebui să vedeți mai jos tipul de rezultat în 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 |
Valoarea int pe care o specificați pentru nivelul de jurnal este importantă. Aici am definit că nivelul de jurnal „ CRUNCHIFY
” trebuie să fie mai mare decât nivelul DEBUG
, dar mai mic decât nivelul TRACE
furnizat de log4j.
Deci, ori de câte ori ați setat un nivel de prioritate pentru DEBUG pe categorie (în fișierul dvs. log4j.xml
), jurnalele de nivel CRUNCHIFY
NU vor ajunge în fișierul jurnal.