Best way to write custom json messages using log4j2

后端 未结 3 1380
不思量自难忘°
不思量自难忘° 2020-12-30 13:54

I have been using log4j for different kind of projects and have some experience with log4j2. All implementations used the default appender and layout. Currently i need to wr

3条回答
  •  清酒与你
    2020-12-30 14:22

    I found a solution which works for me; slf4j-json-logger. It is a slf4j framework, so should be included in the pom.xml. Sample project files;

    pom.xml

      
      
         4.0.0
         com.reddipped
         JSONLogger_2
         1.0-SNAPSHOT
         jar
         
    
            UTF-8
            1.6
            1.6
    
            com.reddipped.jsonlogger_2.Test
    
            1.7.21
            
            2.6.2 
    
         
    
         
            
               org.slf4j
               slf4j-api
               ${slf4j.version}
            
            
            
               org.apache.logging.log4j
               log4j-slf4j-impl
               ${log4j.version}
            
            
            
               org.apache.logging.log4j
               log4j-api
               ${log4j.version}
            
            
               org.apache.logging.log4j
               log4j-core
               ${log4j.version}
            
            
            
               com.savoirtech.logging
               slf4j-json-logger
               2.0.2
            
          
      
    

    log4j2.xml

      
    
      
      
         
            /Users/petervannes/NetBeansProjects/JSONLogger_2/logfiles
         
         
            
               
                  %m%n
                
               
                  
                  
               
               
            
         
         
            
               
            
            
               
            
         
      
    

    Example Java code

      package com.reddipped.jsonlogger_2;
    
      import com.savoirtech.logging.slf4j.json.LoggerFactory;
      import java.util.HashMap;
      import java.util.Map;
    
      /**
       *
       * @author petervannes
       */
      public class Test {
          public static void main(String[] args) {   
            Test t = new Test() ;
         }
    
    
         public Test() {
    
            LoggerFactory.setIncludeLoggerName(false);
            LoggerFactory.setDateFormatString("yyyy-MM-dd HH:mm:ss.SSS");
    
             com.savoirtech.logging.slf4j.json.logger.Logger LOGGER =  LoggerFactory.getLogger("JSONLogger");
    
         Map optionalFields = new HashMap();
         optionalFields.put("CaseNumber", "C12.12343");
         optionalFields.put("Step","Assignment") ;
         optionalFields.put("Department","BPM") ;
    
         String LOB = "Business Administration" ;
         String Service = "DocumentService" ;
         String Process = "AddAttachements" ;
         String Reason = "Technical" ; 
    
         LOGGER.error().message("Conversion error 'incompatible PDF document'")
               .field("LOB",LOB)
               .field("Service", Service)
               .field("Process",Process)
               .field("Reason", Reason)
               .map("OptionalFields", optionalFields).log() ;
         }
    
      }
    

    JSON Log Entry

      {
        "message": "Conversion error  'incompatible PDF document'",
        "LOB": "Business Administration",
        "Service": "DocumentService",
        "Process": "AddAttachements",
        "Reason": "Technical",
        "OptionalFields": {
         "Step": "Assignment",
         "Department": "BPM",
         "CaseNumber": "C12.12343"
        },
        "level": "ERROR",
        "thread_name": "main",
        "class": "com.reddipped.jsonlogger_2.Test",
        "@timestamp": "2016-09-23 10:18:06.623"
      }
    

提交回复
热议问题