-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogMessage.java
138 lines (122 loc) · 3.96 KB
/
LogMessage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2010-2020 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
package org.eclipse.scout.sdk.core.log;
import static java.lang.System.lineSeparator;
import static java.util.Collections.unmodifiableList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Stream;
import org.eclipse.scout.sdk.core.util.Ensure;
import org.eclipse.scout.sdk.core.util.Strings;
/**
* <h3>{@link LogMessage}</h3> Represents a message to write to an {@link ISdkConsoleSpi}.
* <p>
* Use {@link SdkLog} and its methods to create log messages.
*
* @since 6.1.0
*/
public class LogMessage {
private final Level m_severity;
private final String m_prefix;
private final String m_text;
private final List<Throwable> m_throwables;
LogMessage(Level severity, String prefix, String text, List<Throwable> throwables) {
m_severity = Ensure.notNull(severity);
m_prefix = Ensure.notNull(prefix);
m_text = Ensure.notNull(text);
m_throwables = throwables;
}
/**
* @return The main message to log. It corresponds to the message entered in
* {@link SdkLog#log(Level, CharSequence, Object...)}. If the message used placeholders (see
* {@link MessageFormatter}) these have already been replaced. Is never {@code null}.
*/
public String text() {
return m_text;
}
/**
* @return A {@link Stream} with all {@link Throwable}s of the arguments. Neither the {@link Stream} nor one of its
* elements may be {@code null}.
*/
public Stream<Throwable> throwables() {
return m_throwables.stream();
}
/**
* @return All {@link Throwable}s of the arguments as unmodifiable {@link List}. Neither the {@link List} nor one of
* its elements may be {@code null}.
*/
public List<Throwable> throwableList() {
return unmodifiableList(m_throwables);
}
/**
* @return The first {@link Throwable}.
*/
public Optional<Throwable> firstThrowable() {
return throwables().findFirst();
}
/**
* @return The severity of the message. Is never {@code null}.
*/
public Level severity() {
return m_severity;
}
/**
* @return The message prefix. It consists of a timestamp and the message severity name. Is never {@code null}.
*/
public String prefix() {
return m_prefix;
}
/**
* @return {@link #prefix()} and {@link #text()} and the stack traces of all {@link #throwables()}.
*/
public String all() {
StringBuilder logContent = new StringBuilder(prefix()).append(text());
if (!m_throwables.isEmpty()) {
Iterator<Throwable> iterator = m_throwables.iterator();
Throwable t = iterator.next();
logContent.append(lineSeparator()).append(Strings.fromThrowable(t));
while (iterator.hasNext()) {
t = iterator.next();
logContent.append(lineSeparator()).append(Strings.fromThrowable(t));
}
}
return logContent.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LogMessage that = (LogMessage) o;
return m_severity.equals(that.m_severity) &&
m_prefix.equals(that.m_prefix) &&
m_text.equals(that.m_text) &&
m_throwables.equals(that.m_throwables);
}
@Override
public int hashCode() {
return Objects.hash(m_severity, m_prefix, m_text, m_throwables);
}
@Override
public String toString() {
return new StringBuilder(LogMessage.class.getSimpleName())
.append(": ")
.append(m_prefix)
.append(m_text)
.toString();
}
}