aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/core/ConsoleLogFormatterTestCase.java
blob: 017d30623cd9908ce5b7df4b3b48aae434787af2 (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogLevel;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.time.Instant;

import static org.junit.jupiter.api.Assertions.assertEquals;


/**
 * @author Simon Thoresen Hult
 */
public class ConsoleLogFormatterTestCase {

    private static final ConsoleLogFormatter SIMPLE_FORMATTER = new ConsoleLogFormatter(null, null, null);
    private static final LogEntry SIMPLE_ENTRY = new MyEntry(0, LogLevel.AUDIT, null);

    // TODO: Should (at least) use ConsoleLogFormatter.ABSENCE_REPLACEMENT instead of literal '-'. See ticket 7128315.

    @Test
    void requireThatMillisecondsArePadded() {
        for (int i = 0; i < 10000; ++i) {
            LogEntry entry = new MyEntry(i, LogLevel.AUDIT, null);
            Instant instant = Instant.ofEpochMilli(i);
            assertEquals(String.format("%d.%06d\t-\t-\t-\t-\tunknown\t", instant.getEpochSecond(), instant.getNano() / 1000),
                    SIMPLE_FORMATTER.formatEntry(entry));
        }
    }

    @Test
    void requireThatHostNameIsIncluded() {
        assertEquals("0.000000\thostName\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter("hostName", null, null).formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatHostNameIsOptional() {
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, null, null).formatEntry(SIMPLE_ENTRY));
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter("", null, null).formatEntry(SIMPLE_ENTRY));
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(" ", null, null).formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatProcessIdIsIncluded() {
        assertEquals("0.000000\t-\tprocessId\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, "processId", null).formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatProcessIdIsOptional() {
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, null, null).formatEntry(SIMPLE_ENTRY));
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, "", null).formatEntry(SIMPLE_ENTRY));
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, " ", null).formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatProcessIdIncludesThreadIdWhenAvailable() {
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, null).putProperty("THREAD_ID", "threadId");
        assertEquals("0.000000\t-\tprocessId/threadId\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, "processId", null).formatEntry(entry));
    }

    @Test
    void requireThatServiceNameIsIncluded() {
        assertEquals("0.000000\t-\t-\tserviceName\t-\tunknown\t",
                new ConsoleLogFormatter(null, null, "serviceName").formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatServiceNameIsOptional() {
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, null, null).formatEntry(SIMPLE_ENTRY));
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, null, "").formatEntry(SIMPLE_ENTRY));
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                new ConsoleLogFormatter(null, null, " ").formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatBundleNameIsIncluded() {
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, null).setBundleSymbolicName("bundleName");
        assertEquals("0.000000\t-\t-\t-\tbundleName\tunknown\t",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatBundleNameIsOptional() {
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                SIMPLE_FORMATTER.formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatLoggerNameIsIncluded() {
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, null).putProperty("LOGGER_NAME", "loggerName");
        assertEquals("0.000000\t-\t-\t-\t/loggerName\tunknown\t",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatLoggerNameIsOptional() {
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                SIMPLE_FORMATTER.formatEntry(SIMPLE_ENTRY));
    }

    @Test
    void requireThatBundleAndLoggerNameIsCombined() {
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, null).setBundleSymbolicName("bundleName")
                .putProperty("LOGGER_NAME", "loggerName");
        assertEquals("0.000000\t-\t-\t-\tbundleName/loggerName\tunknown\t",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatLevelNameIsIncluded() {
        ConsoleLogFormatter formatter = SIMPLE_FORMATTER;
        assertEquals("0.000000\t-\t-\t-\t-\terror\t",
                formatter.formatEntry(new MyEntry(0, LogLevel.ERROR, null)));
        assertEquals("0.000000\t-\t-\t-\t-\twarning\t",
                formatter.formatEntry(new MyEntry(0, LogLevel.WARN, null)));
        assertEquals("0.000000\t-\t-\t-\t-\tinfo\t",
                formatter.formatEntry(new MyEntry(0, LogLevel.INFO, null)));
        assertEquals("0.000000\t-\t-\t-\t-\tdebug\t",
                formatter.formatEntry(new MyEntry(0, LogLevel.DEBUG, null)));
    }

    @Test
    void requireThatMessageIsIncluded() {
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, "message");
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\tmessage",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatMessageIsOptional() {
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, null);
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatMessageIsEscaped() {
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, "\\\n\r\t");
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t\\\\\\n\\r\\t",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatExceptionIsIncluded() {
        Throwable t = new Throwable();
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, null).setException(t);
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t\\n" + formatThrowable(t),
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatExceptionIsEscaped() {
        Throwable t = new Throwable("\\\n\r\t");
        LogEntry entry = new MyEntry(0, LogLevel.AUDIT, null).setException(t);
        assertEquals("0.000000\t-\t-\t-\t-\tunknown\t\\n" + formatThrowable(t),
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatExceptionIsSimplifiedForInfoEntries() {
        Throwable t = new Throwable("exception");
        LogEntry entry = new MyEntry(0, LogLevel.INFO, "entry").setException(t);
        assertEquals("0.000000\t-\t-\t-\t-\tinfo\tentry: exception",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatSimplifiedExceptionIsEscaped() {
        Throwable t = new Throwable("\\\n\r\t");
        LogEntry entry = new MyEntry(0, LogLevel.INFO, "entry").setException(t);
        assertEquals("0.000000\t-\t-\t-\t-\tinfo\tentry: \\\\\\n\\r\\t",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    @Test
    void requireThatSimplifiedExceptionMessageIsOptional() {
        Throwable t = new Throwable();
        LogEntry entry = new MyEntry(0, LogLevel.INFO, "entry").setException(t);
        assertEquals("0.000000\t-\t-\t-\t-\tinfo\tentry: java.lang.Throwable",
                SIMPLE_FORMATTER.formatEntry(entry));
    }

    private static String formatThrowable(Throwable t) {
        Writer out = new StringWriter();
        t.printStackTrace(new PrintWriter(out));
        return out.toString().replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t");
    }

    private static class MyEntry implements LogEntry {

        final String message;
        final LogLevel level;
        final long time;
        Bundle bundle = null;
        ServiceReference<?> serviceReference = null;
        Throwable exception;

        MyEntry(long time, LogLevel level, String message) {
            this.message = message;
            this.level = level;
            this.time = time;
        }

        MyEntry setBundleSymbolicName(String symbolicName) {
            this.bundle = Mockito.mock(Bundle.class);
            Mockito.doReturn(symbolicName).when(this.bundle).getSymbolicName();
            return this;
        }

        MyEntry setException(Throwable exception) {
            this.exception = exception;
            return this;
        }

        MyEntry putProperty(String key, String val) {
            this.serviceReference = Mockito.mock(ServiceReference.class);
            Mockito.doReturn(val).when(this.serviceReference).getProperty(key);
            return this;
        }

        @Override
        public long getTime() {
            return time;
        }

        @Override public LogLevel getLogLevel() { return level; }
        @Override public String getLoggerName() { return null; }
        @Override public long getSequence() { return 0; }
        @Override public String getThreadInfo() { return null; }
        @Override public StackTraceElement getLocation() { return null; }

        @Override @SuppressWarnings("deprecation")
        public int getLevel() {
            return level.ordinal();
        }

        @Override
        public String getMessage() {
            return message;
        }

        @Override
        public Throwable getException() {
            return exception;
        }

        @Override
        public Bundle getBundle() {
            return bundle;
        }

        @Override
        public ServiceReference<?> getServiceReference() {
            return serviceReference;
        }
    }
}