View Javadoc
1   package org.slf4j.helpers;
2   
3   import org.slf4j.event.LoggingEvent;
4   
5   /**
6    * Holds normalized calling call parameters.
7    * 
8    * Includes utility methods such as {@link #normalize(String, Object[], Throwable)} to help the normalization of parameters.
9    * 
10   * @author ceki
11   * @since 2.0
12   */
13  public class NormalizedParameters {
14  
15      final String message;
16      final Object[] arguments;
17      final Throwable throwable;
18  
19      public NormalizedParameters(String message, Object[] arguments, Throwable throwable) {
20          this.message = message;
21          this.arguments = arguments;
22          this.throwable = throwable;
23      }
24  
25      public NormalizedParameters(String message, Object[] arguments) {
26          this(message, arguments, null);
27      }
28  
29      public String getMessage() {
30          return message;
31      }
32  
33      public Object[] getArguments() {
34          return arguments;
35      }
36  
37      public Throwable getThrowable() {
38          return throwable;
39      }
40  
41      /**
42       * Helper method to determine if an {@link Object} array contains a
43       * {@link Throwable} as last element
44       *
45       * @param argArray The arguments off which we want to know if it contains a
46       *                 {@link Throwable} as last element
47       * @return if the last {@link Object} in argArray is a {@link Throwable} this
48       *         method will return it, otherwise it returns null
49       */
50      public static Throwable getThrowableCandidate(final Object[] argArray) {
51          if (argArray == null || argArray.length == 0) {
52              return null;
53          }
54  
55          final Object lastEntry = argArray[argArray.length - 1];
56          if (lastEntry instanceof Throwable) {
57              return (Throwable) lastEntry;
58          }
59  
60          return null;
61      }
62  
63      /**
64       * Helper method to get all but the last element of an array
65       *
66       * @param argArray The arguments from which we want to remove the last element
67       *
68       * @return a copy of the array without the last element
69       */
70      public static Object[] trimmedCopy(final Object[] argArray) {
71          if (argArray == null || argArray.length == 0) {
72              throw new IllegalStateException("non-sensical empty or null argument array");
73          }
74  
75          final int trimmedLen = argArray.length - 1;
76  
77          Object[] trimmed = new Object[trimmedLen];
78  
79          if (trimmedLen > 0) {
80              System.arraycopy(argArray, 0, trimmed, 0, trimmedLen);
81          }
82  
83          return trimmed;
84      }
85  
86      /**
87       * This method serves to normalize logging call invocation parameters.
88       * 
89       * More specifically, if a throwable argument is not supplied directly, it
90       * attempts to extract it from the argument array.
91       */
92      public static NormalizedParameters normalize(String msg, Object[] arguments, Throwable t) {
93  
94          if (t != null) {
95              return new NormalizedParameters(msg, arguments, t);
96          }
97  
98          if (arguments == null || arguments.length == 0) {
99              return new NormalizedParameters(msg, arguments, t);
100         }
101 
102         Throwable throwableCandidate = NormalizedParameters.getThrowableCandidate(arguments);
103         if (throwableCandidate != null) {
104             Object[] trimmedArguments = MessageFormatter.trimmedCopy(arguments);
105             return new NormalizedParameters(msg, trimmedArguments, throwableCandidate);
106         } else {
107             return new NormalizedParameters(msg, arguments);
108         }
109 
110     }
111 
112     public static NormalizedParameters normalize(LoggingEvent event) {
113         return normalize(event.getMessage(), event.getArgumentArray(), event.getThrowable());
114     }
115 
116 }