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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log.internal;
import java.util.LinkedHashMap;
import java.util.Map;
public class LevelsModSpec {
private static final String ON = "on";
private static final String OFF = "off";
private static Map<String, String> defaultLogLevels() {
var m = new LinkedHashMap<String,String>();
m.put("fatal", ON);
m.put("error", ON);
m.put("warning", ON);
m.put("info", ON);
m.put("event", ON);
m.put("config", ON);
m.put("debug", OFF);
m.put("spam", OFF);
return m;
}
private Map<String, String> levelMods = defaultLogLevels();
private void setAll(String value) {
for (String k : levelMods.keySet()) {
levelMods.put(k, value);
}
}
private void setAll() {
setAll(ON);
}
private void clearAll() {
setAll(OFF);
}
public LevelsModSpec addModifications(String mods) {
for (String s : mods.split("[+ ,]")) {
String offOn = ON;
if (s.startsWith("-")) {
offOn = OFF;
s = s.substring(1);
}
if (s.isEmpty()) continue;
if (s.equals("all")) {
setAll(offOn);
} else if (levelMods.containsKey(s)) {
levelMods.put(s, offOn);
} else {
throw new IllegalArgumentException("Unknown log level: "+s);
}
}
return this;
}
public LevelsModSpec setLevels(String levels) {
if (! (levels.startsWith("+") || levels.startsWith("-"))) {
clearAll();
}
return addModifications(levels);
}
public String toLogctlModSpec() {
var spec = new StringBuilder();
boolean comma = false;
for (var entry : levelMods.entrySet()) {
if (comma) {
spec.append(",");
}
spec.append(entry.getKey());
spec.append("=");
spec.append(entry.getValue());
comma = true;
}
return spec.toString();
}
}
|