1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.logging.business;
20
21 import com.fasterxml.jackson.core.JsonProcessingException;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import org.apache.logging.log4j.message.Message;
24 import org.apache.logging.log4j.status.StatusLogger;
25
26
27
28
29 public class JsonMessage implements Message {
30
31
32
33
34 private static final long serialVersionUID = 1L;
35
36
37
38 private static final ObjectMapper MAPPER = new ObjectMapper();
39
40
41
42 private final Object object;
43
44
45
46
47
48
49 public JsonMessage(final Object object) {
50 this.object = object;
51 }
52
53
54
55
56 @Override
57 public String getFormattedMessage() {
58 String result;
59 try {
60 result = MAPPER.writeValueAsString(object);
61 } catch (final JsonProcessingException e) {
62 StatusLogger.getLogger().catching(e);
63 result = object.toString();
64 }
65 return result;
66 }
67
68
69
70
71 @Override
72 public String getFormat() {
73 return object.toString();
74 }
75
76
77
78
79 @Override
80 public Object[] getParameters() {
81 return new Object[]{object};
82 }
83
84
85
86
87 @Override
88 public Throwable getThrowable() {
89 return null;
90 }
91 }