Skip to content

Commit 3b92141

Browse files
authored
feat: Add a Logger.removeContext function. (#147)
1 parent 16ddce7 commit 3b92141

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/main/java/org/jitsi/utils/logging2/Logger.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,10 @@ public interface Logger
262262
* @param value the context value
263263
*/
264264
void addContext(String key, String value);
265+
266+
/**
267+
* Remove a context key/value pair from this logger.
268+
* @param key the key to remove.
269+
*/
270+
void removeContext(String key);
265271
}

src/main/java/org/jitsi/utils/logging2/LoggerImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void removeHandler(Handler handler) throws SecurityException
100100
{
101101
loggerDelegate.removeHandler(handler);
102102
}
103-
103+
104104
private boolean isLoggable(Level level)
105105
{
106106
return level.intValue() >= minLogLevel.intValue() && loggerDelegate.isLoggable(level);
@@ -309,5 +309,11 @@ public void addContext(String key, String value)
309309
logContext.addContext(key, value);
310310
}
311311

312+
@Override
313+
public void removeContext(String key)
314+
{
315+
logContext.removeContext(key);
316+
}
317+
312318
static Function<String, java.util.logging.Logger> loggerFactory = java.util.logging.Logger::getLogger;
313319
}

src/main/kotlin/org/jitsi/utils/logging2/LogContext.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class LogContext private constructor(
6868

6969
fun addContext(key: String, value: String) = addContext(mapOf(key to value))
7070

71+
@Synchronized
72+
fun removeContext(key: String) {
73+
context = context - key
74+
updateFormattedContext()
75+
}
76+
7177
@Synchronized
7278
fun addContext(addedContext: Map<String, String>) {
7379
context = context + addedContext

src/test/java/org/jitsi/utils/logging2/LoggerImplTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ public void testLoggingWithContext()
140140
assertTrue(containsData(contextTokens, "keyOne=value1"));
141141
assertTrue(containsData(contextTokens, "keyTwo=value2"));
142142
}
143+
@Test
144+
public void testRemovingContext()
145+
{
146+
Map<String, String> ctxData = new HashMap<>();
147+
ctxData.put("keyOne", "value1");
148+
ctxData.put("keyTwo", "value2");
149+
LogContext ctx = new LogContext(ctxData);
150+
151+
LoggerImpl logger = new LoggerImpl("test", ctx);
152+
logger.removeContext("keyOne");
153+
154+
logger.info("hello, world!");
155+
156+
String[] contextTokens = fakeLogger.getLastLineContextTokens();
157+
assertTrue(containsData(contextTokens, "keyTwo=value2"));
158+
assertTrue(Arrays.stream(contextTokens)
159+
.noneMatch(token -> token.contains("keyOne")));
160+
}
143161

144162
@Test
145163
public void testChildLoggerInheritsContext()

0 commit comments

Comments
 (0)