-
Notifications
You must be signed in to change notification settings - Fork 136
/
LogCollectorHandler.java
135 lines (114 loc) · 3.87 KB
/
LogCollectorHandler.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
/*
* Copyright (c) 2022, 2024 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.jul.handler;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.glassfish.main.jul.record.GlassFishLogRecord;
import org.glassfish.main.jul.record.MessageResolver;
/**
* This special {@link Handler} can be used for testing purposes.
* It collects log records passing through the given {@link Logger} instance.
*
* @author David Matejcek
*/
public class LogCollectorHandler extends Handler {
private static final MessageResolver RESOLVER = new MessageResolver();
private final LogRecordBuffer buffer;
private final Logger logger;
/**
* Creates a {@link LogRecord} collector handler with the capacity of 100 records and maximal
* wait time 5 seconds.
*
* @param loggerToFollow this handler will be added to this logger.
* @see LogRecordBuffer
*/
public LogCollectorHandler(final Logger loggerToFollow) {
this(loggerToFollow, 100, 5);
}
/**
* @param loggerToFollow this handler will be added to this logger.
* @param capacity capacity of the buffer.
* @param maxWait maximal time in seconds to wait for the free capacity. If < 1, can wait
* forever.
* @see LogRecordBuffer
*/
public LogCollectorHandler(final Logger loggerToFollow, int capacity, int maxWait) {
buffer = new LogRecordBuffer(capacity, maxWait);
logger = loggerToFollow;
logger.addHandler(this);
}
@Override
public void publish(LogRecord record) {
if (isLoggable(record)) {
this.buffer.add(RESOLVER.resolve(record));
}
}
@Override
public void flush() {
// nothing
}
/**
* Unattaches the handler from the logger and drops all collected log records.
*/
@Override
public void close() throws SecurityException {
this.logger.removeHandler(this);
reset();
}
/**
* Removes the first record in the buffer and returns it.
*
* @return the first {@link GlassFishLogRecord} in the buffer or null if the buffer is empty.
*/
public GlassFishLogRecord pop() {
return this.buffer.poll();
}
/**
* Creates a list of all records and resets the buffer.
*
* @return all collected records
*/
public List<GlassFishLogRecord> getAll() {
final List<GlassFishLogRecord> list = new ArrayList<>(this.buffer.getSize());
while (!this.buffer.isEmpty()) {
list.add(this.buffer.poll());
}
return list;
}
/**
* Creates a list of all records and resets the buffer.
* @param mappingFunction
* @param <R> expected item type
*
* @return all collected records
*/
public <R> List<R> getAll(Function<LogRecord, R> mappingFunction) {
return getAll().stream().map(mappingFunction).collect(Collectors.toList());
}
/**
* Drops all collected records.
*/
public void reset() {
while (!this.buffer.isEmpty()) {
this.buffer.poll();
}
}
}