View Javadoc
1   package org.slf4j.event;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   
7   import org.slf4j.Logger;
8   import org.slf4j.Marker;
9   
10  /**
11   * A default implementation of {@link LoggingEvent}.
12   * 
13   * @author Ceki Gülcü
14   *
15   * @since 2.0.0
16   */
17  public class DefaultLoggingEvent implements LoggingEvent {
18  
19      Logger logger;
20      Level level;
21  
22      String message;
23      List<Marker> markers;
24      List<Object> arguments;
25      List<KeyValuePair> keyValuePairs;
26  
27      Throwable throwable;
28      String threadName;
29      long timeStamp;
30      
31      String callerBoundary;
32  
33      public DefaultLoggingEvent(Level level, Logger logger) {
34          this.logger = logger;
35          this.level = level;
36      }
37  
38      public void addMarker(Marker marker) {
39          if (markers == null) {
40              markers = new ArrayList<>(2);
41          }
42          markers.add(marker);
43      }
44  
45      @Override
46      public List<Marker> getMarkers() {
47          return markers;
48      }
49  
50      public void addArgument(Object p) {
51          getNonNullArguments().add(p);
52      }
53  
54      public void addArguments(Object... args) {
55          getNonNullArguments().addAll(Arrays.asList(args));
56      }
57  
58      private List<Object> getNonNullArguments() {
59          if (arguments == null) {
60              arguments = new ArrayList<>(3);
61          }
62          return arguments;
63      }
64  
65      @Override
66      public List<Object> getArguments() {
67          return arguments;
68      }
69  
70      @Override
71      public Object[] getArgumentArray() {
72          if (arguments == null)
73              return null;
74          return arguments.toArray();
75      }
76  
77      public void addKeyValue(String key, Object value) {
78          getNonnullKeyValuePairs().add(new KeyValuePair(key, value));
79      }
80  
81      private List<KeyValuePair> getNonnullKeyValuePairs() {
82          if (keyValuePairs == null) {
83              keyValuePairs = new ArrayList<>(4);
84          }
85          return keyValuePairs;
86      }
87  
88      @Override
89      public List<KeyValuePair> getKeyValuePairs() {
90          return keyValuePairs;
91      }
92  
93      public void setThrowable(Throwable cause) {
94          this.throwable = cause;
95      }
96  
97      @Override
98      public Level getLevel() {
99          return level;
100     }
101 
102     @Override
103     public String getLoggerName() {
104         return logger.getName();
105     }
106 
107     @Override
108     public String getMessage() {
109         return message;
110     }
111 
112     public void setMessage(String message) {
113         this.message = message;
114     }
115 
116     @Override
117     public Throwable getThrowable() {
118         return throwable;
119     }
120 
121     public String getThreadName() {
122         return threadName;
123     }
124 
125     public long getTimeStamp() {
126         return timeStamp;
127     }
128 
129     public void setCallerBoundary(String fqcn) {
130         this.callerBoundary = fqcn;
131     }
132     
133     public String getCallerBoundary() {
134         return callerBoundary;
135     }
136 }