-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggerNameRewritePolicy.java
67 lines (58 loc) · 2.34 KB
/
LoggerNameRewritePolicy.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
package xyz.bobkinn.log4jutils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.rewrite.RewritePolicy;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.util.KeyValuePair;
import java.util.ArrayList;
import java.util.List;
@Getter
@RequiredArgsConstructor
@Plugin(name = "LoggerNameRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true)
public class LoggerNameRewritePolicy implements RewritePolicy {
@RequiredArgsConstructor
public static class Selector {
private final boolean isPackage;
private final String oldName;
private final String newName;
}
public static Selector createSelector(String oldName, String newName){
return new Selector(oldName.endsWith("."), oldName, newName);
}
private final List<Selector> selectors;
@PluginFactory
public static LoggerNameRewritePolicy createPolicy(
@PluginElement("KeyValuePair") final KeyValuePair[] map) {
final List<Selector> selectors = new ArrayList<>();
for (KeyValuePair pair : map){
String[] oldNames = pair.getKey().split(",");
for (String name : oldNames) {
selectors.add(createSelector(name, pair.getValue()));
}
}
return new LoggerNameRewritePolicy(selectors);
}
public Selector findSelector(String name){
for (Selector sel : selectors){
if (sel.isPackage && name.startsWith(sel.oldName)) {
return sel;
} else if (name.equals(sel.oldName)) {
return sel;
}
}
return null;
}
@Override
public LogEvent rewrite(LogEvent event) {
if (event.getLoggerName() == null) return event;
final String name = event.getLoggerName();
Selector selector = findSelector(name);
if (selector == null) return event;
return new Log4jLogEvent.Builder(event).setLoggerName(selector.newName).build();
}
}