summaryrefslogtreecommitdiffstats
path: root/container-core
diff options
context:
space:
mode:
authorArne Juul <arnej@yahooinc.com>2022-12-08 14:42:38 +0000
committerArne Juul <arnej@yahooinc.com>2022-12-09 13:49:45 +0000
commitca58eb35a1c06721cfb125e54b019a7536b2b1e4 (patch)
tree8a69e86b7f0bcc2cce8b5d6318e0c77bc5e9130e /container-core
parent0f845e25cb7f0f4c91002a03b4c4c679cbb833e0 (diff)
move common parsing
Diffstat (limited to 'container-core')
-rw-r--r--container-core/src/main/java/com/yahoo/container/logging/LevelsModSpec.java78
-rw-r--r--container-core/src/test/java/com/yahoo/container/logging/LevelsModSpecTest.java64
2 files changed, 142 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/logging/LevelsModSpec.java b/container-core/src/main/java/com/yahoo/container/logging/LevelsModSpec.java
new file mode 100644
index 00000000000..050b24562a3
--- /dev/null
+++ b/container-core/src/main/java/com/yahoo/container/logging/LevelsModSpec.java
@@ -0,0 +1,78 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+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();
+ }
+
+}
diff --git a/container-core/src/test/java/com/yahoo/container/logging/LevelsModSpecTest.java b/container-core/src/test/java/com/yahoo/container/logging/LevelsModSpecTest.java
new file mode 100644
index 00000000000..d126f605d38
--- /dev/null
+++ b/container-core/src/test/java/com/yahoo/container/logging/LevelsModSpecTest.java
@@ -0,0 +1,64 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * @author arnej
+ */
+public class LevelsModSpecTest {
+
+ @Test
+ public void hasCorrectDefault() {
+ String wanted = "fatal=on,error=on,warning=on,info=on,event=on,config=on,debug=off,spam=off";
+ var l = new LevelsModSpec();
+ assertEquals(wanted, l.toLogctlModSpec());
+ }
+
+ @Test
+ public void canTurnInfoOff() {
+ String wanted = "fatal=on,error=on,warning=on,info=off,event=on,config=on,debug=off,spam=off";
+ var l = new LevelsModSpec();
+ l.setLevels("-info");
+ assertEquals(wanted, l.toLogctlModSpec());
+ }
+
+ @Test
+ public void canTurnDebugOn() {
+ String wanted = "fatal=on,error=on,warning=on,info=on,event=on,config=on,debug=on,spam=off";
+ var l = new LevelsModSpec();
+ l.setLevels("+debug");
+ assertEquals(wanted, l.toLogctlModSpec());
+ }
+
+ @Test
+ public void canSpeficyOnlySome() {
+ String wanted = "fatal=off,error=off,warning=on,info=off,event=off,config=off,debug=on,spam=off";
+ var l = new LevelsModSpec();
+ l.setLevels("warning debug");
+ assertEquals(wanted, l.toLogctlModSpec());
+ l = new LevelsModSpec();
+ l.setLevels("warning,debug");
+ assertEquals(wanted, l.toLogctlModSpec());
+ l = new LevelsModSpec();
+ l.setLevels("warning, debug");
+ assertEquals(wanted, l.toLogctlModSpec());
+ }
+
+ @Test
+ public void canSpeficyAllMinusSome() {
+ String wanted ="fatal=on,error=on,warning=on,info=off,event=on,config=on,debug=on,spam=off";
+ var l = new LevelsModSpec();
+ l.setLevels("all -info -spam");
+ assertEquals(wanted, l.toLogctlModSpec());
+ l = new LevelsModSpec();
+ l.setLevels("all,-info,-spam");
+ assertEquals(wanted, l.toLogctlModSpec());
+ l = new LevelsModSpec();
+ l.setLevels("all, -info, -spam");
+ assertEquals(wanted, l.toLogctlModSpec());
+ }
+
+}