Skip to content

Commit e59884b

Browse files
committed
[LOGMGR-356] Ensure that nested handlers which log through a JUL handler create the ExtLogHandler with a FormatStyle of NO_FORMAT.
https://issues.redhat.com/browse/LOGMGR-356 Signed-off-by: James R. Perkins <[email protected]>
1 parent 7fcaab6 commit e59884b

File tree

3 files changed

+110
-5
lines changed

3 files changed

+110
-5
lines changed

src/main/java/org/jboss/logmanager/ExtHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void publish(final ExtLogRecord record) {
117117
@SuppressWarnings("deprecation") // record.getFormattedMessage()
118118
protected void publishToNestedHandlers(final ExtLogRecord record) {
119119
if (record != null) {
120-
LogRecord oldRecord = null;
120+
ExtLogRecord oldRecord = null;
121121
for (Handler handler : getHandlers())
122122
try {
123123
if (handler != null) {
@@ -129,7 +129,7 @@ protected void publishToNestedHandlers(final ExtLogRecord record) {
129129
if (record.getFormatStyle() == ExtLogRecord.FormatStyle.PRINTF) {
130130
// reformat it in a simple way, but only for legacy handler usage
131131
oldRecord = new ExtLogRecord(record);
132-
oldRecord.setMessage(record.getFormattedMessage());
132+
oldRecord.setMessage(record.getFormattedMessage(), ExtLogRecord.FormatStyle.NO_FORMAT);
133133
oldRecord.setParameters(null);
134134
} else {
135135
oldRecord = record;

src/main/java/org/jboss/logmanager/LoggerNode.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.logging.Filter;
4141
import java.util.logging.Handler;
4242
import java.util.logging.Level;
43-
import java.util.logging.LogRecord;
4443

4544
import io.smallrye.common.constraint.Assert;
4645
import io.smallrye.common.ref.PhantomReference;
@@ -431,7 +430,7 @@ void setUseParentHandlers(final boolean useParentHandlers) {
431430

432431
@SuppressWarnings("deprecation") // record#getFormattedMessage
433432
void publish(final ExtLogRecord record) {
434-
LogRecord oldRecord = null;
433+
ExtLogRecord oldRecord = null;
435434
for (Handler handler : getHandlers())
436435
try {
437436
if (handler instanceof ExtHandler || handler.getFormatter() instanceof ExtFormatter) {
@@ -442,7 +441,7 @@ void publish(final ExtLogRecord record) {
442441
if (record.getFormatStyle() == ExtLogRecord.FormatStyle.PRINTF) {
443442
// reformat it in a simple way, but only for legacy handler usage
444443
oldRecord = new ExtLogRecord(record);
445-
oldRecord.setMessage(record.getFormattedMessage());
444+
oldRecord.setMessage(record.getFormattedMessage(), ExtLogRecord.FormatStyle.NO_FORMAT);
446445
oldRecord.setParameters(null);
447446
} else {
448447
oldRecord = record;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
*
4+
* Copyright 2025 Red Hat, Inc., and individual contributors
5+
* as indicated by the @author tags.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.jboss.logmanager.handlers;
21+
22+
import java.util.concurrent.BlockingDeque;
23+
import java.util.concurrent.LinkedBlockingDeque;
24+
import java.util.logging.Handler;
25+
import java.util.logging.LogRecord;
26+
27+
import org.jboss.logmanager.ExtFormatter;
28+
import org.jboss.logmanager.ExtLogRecord;
29+
import org.jboss.logmanager.Level;
30+
import org.jboss.logmanager.LogContext;
31+
import org.jboss.logmanager.Logger;
32+
import org.jboss.logmanager.formatters.PatternFormatter;
33+
import org.junit.jupiter.api.Assertions;
34+
import org.junit.jupiter.api.Test;
35+
36+
/**
37+
* @author <a href="mailto:[email protected]">James R. Perkins</a>
38+
*/
39+
public class NestedHandlerTest {
40+
41+
public static class TestHandler extends Handler {
42+
final BlockingDeque<ExtLogRecord> messages = new LinkedBlockingDeque<>();
43+
final ExtFormatter formatter;
44+
45+
TestHandler() {
46+
this.formatter = new PatternFormatter("%s");
47+
}
48+
49+
@Override
50+
public void publish(final LogRecord record) {
51+
final ExtLogRecord extLogRecord;
52+
if (record instanceof ExtLogRecord) {
53+
extLogRecord = (ExtLogRecord) record;
54+
} else {
55+
extLogRecord = new ExtLogRecord(Level.ERROR, String.format("An ExtLogRecord was expected but was %s", record),
56+
ExtLogRecord.FormatStyle.PRINTF, NestedHandlerTest.class.getPackage().getName());
57+
}
58+
messages.add(extLogRecord);
59+
}
60+
61+
@Override
62+
public void flush() {
63+
64+
}
65+
66+
@Override
67+
public void close() throws SecurityException {
68+
messages.clear();
69+
}
70+
}
71+
72+
@Test
73+
public void julHandler() {
74+
final TestHandler handler = new TestHandler();
75+
handler.setErrorManager(AssertingErrorManager.of());
76+
try (DelayedHandler delayedHandler = new DelayedHandler()) {
77+
delayedHandler.addHandler(handler);
78+
final ExtLogRecord record = new ExtLogRecord(Level.INFO, "FormatStyle %s", ExtLogRecord.FormatStyle.PRINTF,
79+
NestedHandlerTest.class.getPackage().getName());
80+
record.setParameters(new Object[] { "PRINTF" });
81+
delayedHandler.publish(record);
82+
final var found = handler.messages.poll();
83+
Assertions.assertNotNull(found);
84+
Assertions.assertEquals("FormatStyle PRINTF", found.getMessage());
85+
Assertions.assertEquals(ExtLogRecord.FormatStyle.NO_FORMAT, found.getFormatStyle());
86+
}
87+
}
88+
89+
@Test
90+
public void loggerNode() throws Exception {
91+
final TestHandler handler = new TestHandler();
92+
handler.setErrorManager(AssertingErrorManager.of());
93+
try (LogContext context = LogContext.getLogContext()) {
94+
final Logger logger = context.getLogger(NestedHandlerTest.class.getName());
95+
logger.addHandler(handler);
96+
final ExtLogRecord record = new ExtLogRecord(Level.INFO, "FormatStyle %s", ExtLogRecord.FormatStyle.PRINTF,
97+
NestedHandlerTest.class.getPackage().getName());
98+
record.setParameters(new Object[] { "PRINTF" });
99+
logger.log(record);
100+
final var found = handler.messages.poll();
101+
Assertions.assertNotNull(found);
102+
Assertions.assertEquals("FormatStyle PRINTF", found.getMessage());
103+
Assertions.assertEquals(ExtLogRecord.FormatStyle.NO_FORMAT, found.getFormatStyle());
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)