1 package org.slf4j.helpers;
2
3 import org.slf4j.event.LoggingEvent;
4
5
6
7
8
9
10
11
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
43
44
45
46
47
48
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
65
66
67
68
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
88
89
90
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 }