From eaef36776cd0beb79f877904b507e8c6f53ee853 Mon Sep 17 00:00:00 2001 From: James Perkins Date: Fri, 11 Nov 2016 15:59:26 -0800 Subject: [PATCH] [LOGMGR-144] Allow a # to be used as an alternate for expressions in a pattern formatter. --- .../formatters/FormatStringParser.java | 1 + .../formatters/PatternFormatterTests.java | 58 ++++++++++--------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java b/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java index 7e46f248..89632e15 100644 --- a/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java +++ b/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java @@ -178,6 +178,7 @@ public static FormatStep[] getSteps(final String formatString, ColorMap colors) timeZone = TimeZone.getTimeZone(argument); break; } + case '#': case '$': { stepList.add(Formatters.systemPropertyFormatStep(argument, leftJustify, minimumWidth, truncateBeginning, maximumWidth)); break; diff --git a/src/test/java/org/jboss/logmanager/formatters/PatternFormatterTests.java b/src/test/java/org/jboss/logmanager/formatters/PatternFormatterTests.java index a99d2c2e..25907408 100644 --- a/src/test/java/org/jboss/logmanager/formatters/PatternFormatterTests.java +++ b/src/test/java/org/jboss/logmanager/formatters/PatternFormatterTests.java @@ -130,32 +130,8 @@ public void threads() throws Exception { @Test public void systemProperties() throws Exception { - final ExtLogRecord record = createLogRecord("test"); - PatternFormatter formatter = new PatternFormatter("%${org.jboss.logmanager.testProp}"); - Assert.assertEquals("testValue", formatter.format(record)); - - formatter = new PatternFormatter("%${invalid:defaultValue}"); - Assert.assertEquals("defaultValue", formatter.format(record)); - - formatter = new PatternFormatter("%${invalid}"); - Assert.assertEquals("null", formatter.format(record)); - - // Test null arguments - try { - formatter = new PatternFormatter("%$"); - formatter.format(record); - Assert.fail("Should not allow null arguments"); - } catch (IllegalArgumentException ignore) { - - } - - try { - formatter = new PatternFormatter("%${}"); - formatter.format(record); - Assert.fail("Should not allow null arguments"); - } catch (IllegalArgumentException ignore) { - - } + systemProperties("$"); + systemProperties("#"); } @Test @@ -249,6 +225,36 @@ public void extendedThrowable() throws Exception { Assert.assertTrue(formatted.contains("CIRCULAR REFERENCE:java.lang.IllegalStateException: suppressedLevel1")); } + + private void systemProperties(final String propertyPrefix) throws Exception { + final ExtLogRecord record = createLogRecord("test"); + PatternFormatter formatter = new PatternFormatter("%" + propertyPrefix + "{org.jboss.logmanager.testProp}"); + Assert.assertEquals("testValue", formatter.format(record)); + + formatter = new PatternFormatter("%" + propertyPrefix + "{invalid:defaultValue}"); + Assert.assertEquals("defaultValue", formatter.format(record)); + + formatter = new PatternFormatter("%" + propertyPrefix + "{invalid}"); + Assert.assertEquals("null", formatter.format(record)); + + // Test null arguments + try { + formatter = new PatternFormatter("%" + propertyPrefix); + formatter.format(record); + Assert.fail("Should not allow null arguments"); + } catch (IllegalArgumentException ignore) { + + } + + try { + formatter = new PatternFormatter("%" + propertyPrefix + "{}"); + formatter.format(record); + Assert.fail("Should not allow null arguments"); + } catch (IllegalArgumentException ignore) { + + } + } + protected static ExtLogRecord createLogRecord(final String msg) { final ExtLogRecord result = new ExtLogRecord(org.jboss.logmanager.Level.INFO, msg, PatternFormatterTests.class.getName()); result.setSourceClassName(PatternFormatterTests.class.getName());