View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.jsonTest;
16  
17  import ch.qos.logback.classic.Level;
18  import ch.qos.logback.classic.encoder.JsonEncoder;
19  import ch.qos.logback.classic.spi.LoggerContextVO;
20  import ch.qos.logback.classic.spi.PubThrowableProxy;
21  import ch.qos.logback.classic.spi.StackTraceElementProxy;
22  import com.fasterxml.jackson.core.JsonProcessingException;
23  import com.fasterxml.jackson.databind.JsonNode;
24  import com.fasterxml.jackson.databind.ObjectMapper;
25  import com.fasterxml.jackson.databind.module.SimpleModule;
26  import org.slf4j.IMarkerFactory;
27  import org.slf4j.Marker;
28  import org.slf4j.event.KeyValuePair;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  
35  public class JsonStringToLoggingEventMapper {
36      IMarkerFactory markerFactory;
37  
38  
39      public JsonStringToLoggingEventMapper(IMarkerFactory markerFactory) {
40          this.markerFactory = markerFactory;
41      }
42  
43      public JsonLoggingEvent mapStringToLoggingEvent(String resultString) throws JsonProcessingException {
44          ObjectMapper objectMapper = new ObjectMapper();
45          SimpleModule module = new SimpleModule();
46          module.addDeserializer(StackTraceElementProxy.class, new STEPDeserializer());
47          module.addDeserializer(Level.class, new LevelDeserializer());
48          module.addDeserializer(Marker.class, new MarkerDeserializer(markerFactory));
49          module.addDeserializer(KeyValuePair.class, new KeyValuePairDeserializer());
50          module.addDeserializer(LoggerContextVO.class, new LoggerContextVODeserializer());
51          module.addDeserializer(PubThrowableProxy.class, new PubThrowableProxyDeserializer());
52  
53          objectMapper.registerModule(module);
54  
55          JsonNode jsonNode = objectMapper.readTree(resultString);
56          JsonLoggingEvent resultEvent = objectMapper.treeToValue(jsonNode, JsonLoggingEvent.class);
57          //buildLevel(jsonNode, resultEvent);
58  
59          //xbuildMarkersList(jsonNode, resultEvent);
60          //xbuildKVPList(jsonNode, resultEvent);
61          //buildThrowableProxy(jsonNode, resultEvent);
62          return resultEvent;
63      }
64  
65      private static void UNUSED_buildLevel(JsonNode jsonNode, JsonLoggingEvent resultEvent) {
66          String levelStr = jsonNode.at("/"+ JsonEncoder.LEVEL_ATTR_NAME).asText();
67          Level level = Level.toLevel(levelStr);
68          resultEvent.level = level;
69      }
70  
71      private void UNUSED_buildMarkersList(JsonNode jsonNode, JsonLoggingEvent resultEvent) {
72          JsonNode markersNode = jsonNode.at("/"+JsonEncoder.MARKERS_ATTR_NAME);
73          if(markersNode!=null && markersNode.isArray()) {
74              List<Marker> markerList = new ArrayList<>();
75              Iterator<JsonNode> itr = markersNode.iterator();
76              while (itr.hasNext()) {
77                  JsonNode item=itr.next();
78                  String markerStr = item.asText();
79                  Marker marker = markerFactory.getMarker(markerStr);
80                  markerList.add(marker);
81              }
82              resultEvent.markerList = markerList;
83          }
84      }
85  
86  
87      private void UNUSED_buildKVPList(JsonNode jsonNode, JsonLoggingEvent resultEvent) {
88          JsonNode kvpNode = jsonNode.at("/"+JsonEncoder.KEY_VALUE_PAIRS_ATTR_NAME);
89          if(kvpNode!=null && kvpNode.isArray()) {
90              System.out.println("in buildKVPList");
91              List<KeyValuePair> kvpList = new ArrayList<>();
92              Iterator<JsonNode> itr = kvpNode.iterator();
93              while (itr.hasNext()) {
94                  JsonNode item=itr.next();
95  
96                  Map.Entry<String, JsonNode> entry = item.fields().next();
97                  String key = entry.getKey();
98                  String val = entry.getValue().asText();
99                  kvpList.add(new KeyValuePair(key, val));
100 
101              }
102             resultEvent.kvpList =kvpList;
103         }
104     }
105 
106 }