View Javadoc
1   package org.slf4j.spi;
2   
3   import java.util.function.Supplier;
4   
5   import org.slf4j.Logger;
6   import org.slf4j.Marker;
7   import org.slf4j.event.DefaultLoggingEvent;
8   import org.slf4j.event.KeyValuePair;
9   import org.slf4j.event.Level;
10  import org.slf4j.event.LoggingEvent;
11  
12  public class DefaultLoggingEventBuilder implements LoggingEventBuilder {
13  
14      DefaultLoggingEvent loggingEvent;
15      Logger logger;
16  
17      public DefaultLoggingEventBuilder(Logger logger, Level level) {
18          this.logger = logger;
19          loggingEvent = new DefaultLoggingEvent(level, logger);
20      }
21  
22      /**
23       * Add a marker to the current logging event being built.
24       * 
25       * It is possible to add multiple markers to the same logging event.
26       *
27       * @param marker the marker to add
28       */
29      @Override
30      public LoggingEventBuilder addMarker(Marker marker) {
31          loggingEvent.addMarker(marker);
32          return this;
33      }
34  
35      @Override
36      public LoggingEventBuilder setCause(Throwable t) {
37          loggingEvent.setThrowable(t);
38          return this;
39      }
40  
41      @Override
42      public LoggingEventBuilder addArgument(Object p) {
43          loggingEvent.addArgument(p);
44          return this;
45      }
46  
47      @Override
48      public LoggingEventBuilder addArgument(Supplier<?> objectSupplier) {
49          loggingEvent.addArgument(objectSupplier.get());
50          return this;
51      }
52  
53      @Override
54      public void log(String message) {
55          loggingEvent.setMessage(message);
56          innerLog(loggingEvent);
57      }
58  
59      @Override
60      public void log(String message, Object arg) {
61          loggingEvent.setMessage(message);
62          loggingEvent.addArgument(arg);
63          innerLog(loggingEvent);
64      }
65  
66      @Override
67      public void log(String message, Object arg0, Object arg1) {
68          loggingEvent.setMessage(message);
69          loggingEvent.addArgument(arg0);
70          loggingEvent.addArgument(arg1);
71          innerLog(loggingEvent);
72      }
73  
74      @Override
75      public void log(String message, Object... args) {
76          loggingEvent.setMessage(message);
77          loggingEvent.addArguments(args);
78  
79          innerLog(loggingEvent);
80      }
81  
82      private void innerLog(LoggingEvent logggingEvent) {
83          if (logger instanceof LoggingEventAware) {
84              ((LoggingEventAware) logger).log(logggingEvent);
85          } else {
86              logViaPublicSLF4JLoggerAPI(logggingEvent);
87          }
88      }
89  
90      private void logViaPublicSLF4JLoggerAPI(LoggingEvent logggingEvent) {
91          Object[] argArray = logggingEvent.getArgumentArray();
92          int argLen = argArray == null ? 0 : argArray.length;
93  
94          Throwable t = logggingEvent.getThrowable();
95          int tLen = t == null ? 0 : 1;
96  
97          String msg = logggingEvent.getMessage();
98  
99          Object[] combinedArguments = new Object[argLen + tLen];
100 
101         if (argArray != null) {
102             System.arraycopy(argArray, 0, combinedArguments, 0, argLen);
103         }
104         if (t != null) {
105             combinedArguments[argLen] = t;
106         }
107 
108         msg = mergeMarkersAndKeyValuePairs(logggingEvent, msg);
109 
110         switch (logggingEvent.getLevel()) {
111         case TRACE:
112             logger.trace(msg, combinedArguments);
113             break;
114         case DEBUG:
115             logger.debug(msg, combinedArguments);
116             break;
117         case INFO:
118             logger.info(msg, combinedArguments);
119             break;
120         case WARN:
121             logger.warn(msg, combinedArguments);
122             break;
123         case ERROR:
124             logger.error(msg, combinedArguments);
125             break;
126         }
127 
128     }
129 
130     /**
131      * Prepend markers and key-value pairs to the message.
132      * 
133      * @param logggingEvent
134      * @param msg
135      * @return
136      */
137     private String mergeMarkersAndKeyValuePairs(LoggingEvent logggingEvent, String msg) {
138 
139         StringBuilder sb = null;
140 
141         if (loggingEvent.getMarkers() != null) {
142             sb = new StringBuilder();
143             for (Marker marker : logggingEvent.getMarkers()) {
144                 sb.append(marker);
145                 sb.append(' ');
146             }
147         }
148 
149         if (logggingEvent.getKeyValuePairs() != null) {
150             if (sb == null) {
151                 sb = new StringBuilder();
152             }
153             for (KeyValuePair kvp : logggingEvent.getKeyValuePairs()) {
154                 sb.append(kvp.key);
155                 sb.append('=');
156                 sb.append(kvp.value);
157                 sb.append(' ');
158             }
159         }
160 
161         if (sb != null) {
162             sb.append(msg);
163             return sb.toString();
164         } else {
165             return msg;
166         }
167     }
168 
169     @Override
170     public void log(Supplier<String> messageSupplier) {
171         if (messageSupplier == null) {
172             log((String) null);
173         } else {
174             log(messageSupplier.get());
175         }
176     }
177 
178     @Override
179     public LoggingEventBuilder addKeyValue(String key, Object value) {
180         loggingEvent.addKeyValue(key, value);
181         return this;
182     }
183 
184     @Override
185     public LoggingEventBuilder addKeyValue(String key, Supplier<Object> value) {
186         loggingEvent.addKeyValue(key, value.get());
187         return this;
188     }
189 
190 }