Second cut of rendering a message with ANSI escape codes using JAnsi
syntax, this time with _style names_.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/79e0012c
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/79e0012c
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/79e0012c

Branch: refs/heads/master
Commit: 79e0012cfc47d099c745a40bbb33c7d8022556f8
Parents: 48c808a
Author: Gary Gregory <ggreg...@apache.org>
Authored: Mon Jun 27 16:38:34 2016 -0700
Committer: Gary Gregory <ggreg...@apache.org>
Committed: Mon Jun 27 16:38:34 2016 -0700

----------------------------------------------------------------------
 .../log4j/core/pattern/HtmlMessageRenderer.java |  4 ++
 .../core/pattern/JAnsiMessageRenderer.java      | 76 ++++++++++++++++----
 .../core/pattern/MessagePatternConverter.java   |  8 +--
 .../core/pattern/StyledMessageRenderer.java     | 26 -------
 .../core/pattern/MessageJansiConverterTest.java |  2 +-
 .../pattern/MessageStyledConverterTest.java     | 62 ++++++++++++++++
 .../src/test/resources/log4j-message-ansi.xml   | 34 +++++++++
 .../src/test/resources/log4j-message-jansi.xml  | 34 ---------
 .../src/test/resources/log4j-message-styled.xml | 34 +++++++++
 9 files changed, 202 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HtmlMessageRenderer.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HtmlMessageRenderer.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HtmlMessageRenderer.java
index d3e8026..b105a3c 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HtmlMessageRenderer.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HtmlMessageRenderer.java
@@ -18,6 +18,10 @@ package org.apache.logging.log4j.core.pattern;
 
 public final class HtmlMessageRenderer implements MessageRenderer {
 
+    public HtmlMessageRenderer(String[] formats) {
+        // TODO Auto-generated constructor stub
+    }
+
     @Override
     public void render(StringBuilder source, StringBuilder target) {
         // TODO Auto-generated method stub

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/JAnsiMessageRenderer.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/JAnsiMessageRenderer.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/JAnsiMessageRenderer.java
index 01d390b..4bcd5bd 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/JAnsiMessageRenderer.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/JAnsiMessageRenderer.java
@@ -16,8 +16,11 @@
  */
 package org.apache.logging.log4j.core.pattern;
 
+import java.util.HashMap;
 import java.util.Locale;
+import java.util.Map;
 
+import org.apache.logging.log4j.status.StatusLogger;
 import org.fusesource.jansi.Ansi;
 import org.fusesource.jansi.AnsiRenderer;
 import org.fusesource.jansi.AnsiRenderer.Code;
@@ -50,25 +53,66 @@ import org.fusesource.jansi.AnsiRenderer.Code;
 public final class JAnsiMessageRenderer implements MessageRenderer {
 
     private static final int BEGIN_TOKEN_LEN = 
AnsiRenderer.BEGIN_TOKEN.length();
-    private static final int END_TOKEN_LEN = AnsiRenderer.END_TOKEN.length();
 
-    private String render(final String text, final String... codes) {
-        Ansi ansi = Ansi.ansi();
-        for (final String name : codes) {
-            final Code code = Code.valueOf(name.toUpperCase(Locale.ENGLISH));
+    private static final int END_TOKEN_LEN = AnsiRenderer.END_TOKEN.length();
+    private final Map<String, Code[]> styleMap;
 
-            if (code.isColor()) {
-                if (code.isBackground()) {
-                    ansi = ansi.bg(code.getColor());
+    public JAnsiMessageRenderer(final String[] formats) {
+        Map<String, Code[]> map;
+        if (formats.length > 1) {
+            final String allStylesStr = formats[1];
+            final String[] allStylesArr = splitOnCommas(allStylesStr);
+            map = new HashMap<>(allStylesArr.length);
+            for (final String styleStr : allStylesArr) {
+                final String[] styleArr = styleStr.split("\\s*=\\s*");
+                if (styleArr.length != 2) {
+                    StatusLogger.getLogger().warn("{} style {} is expected to 
be of length 2, not {}",
+                            getClass().getSimpleName(), styleStr, 
styleArr.length);
                 } else {
-                    ansi = ansi.fg(code.getColor());
+                    final String[] codeNames = splitOnCommas(styleArr[1]);
+                    final Code[] codes = new Code[codeNames.length];
+                    for (int i = 0; i < codes.length; i++) {
+                        codes[i] = toCode(codeNames[i]);
+                    }
+                    map.put(styleArr[0], codes);
                 }
-            } else if (code.isAttribute()) {
-                ansi = ansi.a(code.getAttribute());
             }
+        } else {
+            map = new HashMap<>(0);
         }
+        styleMap = map;
+    }
 
+    private void render(final Ansi ansi, final Code code) {
+        if (code.isColor()) {
+            if (code.isBackground()) {
+                ansi.bg(code.getColor());
+            } else {
+                ansi.fg(code.getColor());
+            }
+        } else if (code.isAttribute()) {
+            ansi.a(code.getAttribute());
+        }
+    }
+
+    private void render(final Ansi ansi, final Code... codes) {
+        for (final Code code : codes) {
+            render(ansi, code);
+        }
+    }
+
+    private String render(final String text, final String... names) {
+        final Ansi ansi = Ansi.ansi();
+        for (final String name : names) {
+            final Code[] codes = styleMap.get(name);
+            if (codes != null) {
+                render(ansi, codes);
+            } else {
+                render(ansi, toCode(name));
+            }
+        }
         return ansi.a(text).reset().toString();
+
     }
 
     @Override
@@ -101,7 +145,7 @@ public final class JAnsiMessageRenderer implements 
MessageRenderer {
                 target.append(input);
                 return;
             }
-            final String replacement = render(items[1], 
items[0].split(AnsiRenderer.CODE_LIST_SEPARATOR));
+            final String replacement = render(items[1], 
splitOnCommas(items[0]));
 
             target.append(replacement);
 
@@ -109,4 +153,12 @@ public final class JAnsiMessageRenderer implements 
MessageRenderer {
         }
     }
 
+    private String[] splitOnCommas(final String allStylesStr) {
+        return allStylesStr.split("\\s*,\\s*");
+    }
+
+    private Code toCode(final String name) {
+        return Code.valueOf(name.toUpperCase(Locale.ENGLISH));
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
index 70b6bec..9e6d5b3 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
@@ -54,12 +54,10 @@ public final class MessagePatternConverter extends 
LogEventPatternConverter {
         }
         final String format = formats[0];
         switch (format) {
-        case "jansi":
-            return new JAnsiMessageRenderer();
-        case "styled":
-            return new StyledMessageRenderer();
+        case "ansi":
+            return new JAnsiMessageRenderer(formats);
         case "html":
-            return new HtmlMessageRenderer();
+            return new HtmlMessageRenderer(formats);
         }
         return null;
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/StyledMessageRenderer.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/StyledMessageRenderer.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/StyledMessageRenderer.java
deleted file mode 100644
index 137b77b..0000000
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/StyledMessageRenderer.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.pattern;
-
-public final class StyledMessageRenderer implements MessageRenderer {
-
-    @Override
-    public void render(StringBuilder source, StringBuilder target) {
-        // TODO Auto-generated method stub
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageJansiConverterTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageJansiConverterTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageJansiConverterTest.java
index 9227b2b..9f4366d 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageJansiConverterTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageJansiConverterTest.java
@@ -37,7 +37,7 @@ public class MessageJansiConverterTest {
             + Constants.LINE_SEPARATOR;
 
     @Rule
-    public LoggerContextRule init = new 
LoggerContextRule("log4j-message-jansi.xml");
+    public LoggerContextRule init = new 
LoggerContextRule("log4j-message-ansi.xml");
 
     private Logger logger;
     private ListAppender app;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageStyledConverterTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageStyledConverterTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageStyledConverterTest.java
new file mode 100644
index 0000000..dd28e0d
--- /dev/null
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessageStyledConverterTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.pattern;
+
+import java.util.List;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ */
+public class MessageStyledConverterTest {
+
+    private static final String EXPECTED = "\u001B[31mWarning!\u001B[m Pants 
on \u001B[31mfire!\u001B[m"
+            + Constants.LINE_SEPARATOR;
+
+    @Rule
+    public LoggerContextRule init = new 
LoggerContextRule("log4j-message-styled.xml");
+
+    private Logger logger;
+    private ListAppender app;
+
+    @Before
+    public void setUp() throws Exception {
+        this.logger = this.init.getLogger("LoggerTest");
+        this.app = this.init.getListAppender("List").clear();
+    }
+
+    @Test
+    public void testReplacement() {
+        // See org.fusesource.jansi.AnsiRenderer
+        logger.error("@|WarningStyle Warning!|@ Pants on @|WarningStyle 
fire!|@");
+
+        final List<String> msgs = app.getMessages();
+        assertNotNull(msgs);
+        assertEquals("Incorrect number of messages. Should be 1 is " + 
msgs.size(), 1, msgs.size());
+        assertTrue("Replacement failed - expected ending " + EXPECTED + ", 
actual " + msgs.get(0),
+                msgs.get(0).endsWith(EXPECTED));
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/test/resources/log4j-message-ansi.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j-message-ansi.xml 
b/log4j-core/src/test/resources/log4j-message-ansi.xml
new file mode 100644
index 0000000..685ee82
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-message-ansi.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<Configuration status="OFF" name="StyleTest">
+  <Appenders>
+    <List name="List">
+       <PatternLayout>
+         <Pattern>%message{ansi}%n</Pattern>
+      </PatternLayout>
+    </List>
+  </Appenders>
+
+  <Loggers>
+    <Root level="trace">
+      <AppenderRef ref="List"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/test/resources/log4j-message-jansi.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j-message-jansi.xml 
b/log4j-core/src/test/resources/log4j-message-jansi.xml
deleted file mode 100644
index 4dc783f..0000000
--- a/log4j-core/src/test/resources/log4j-message-jansi.xml
+++ /dev/null
@@ -1,34 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements.  See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License.  You may obtain a copy of the License at
-
-      http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
--->
-<Configuration status="OFF" name="StyleTest">
-  <Appenders>
-    <List name="List">
-       <PatternLayout>
-         <Pattern>%message{jansi}%n</Pattern>
-      </PatternLayout>
-    </List>
-  </Appenders>
-
-  <Loggers>
-    <Root level="trace">
-      <AppenderRef ref="List"/>
-    </Root>
-  </Loggers>
-
-</Configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/79e0012c/log4j-core/src/test/resources/log4j-message-styled.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j-message-styled.xml 
b/log4j-core/src/test/resources/log4j-message-styled.xml
new file mode 100644
index 0000000..e5881c3
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-message-styled.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<Configuration status="OFF" name="StyledTest">
+  <Appenders>
+    <List name="List">
+       <PatternLayout>
+         <Pattern>%message{ansi}{WarningStyle=red, DataStyle=blue}%n</Pattern>
+      </PatternLayout>
+    </List>
+  </Appenders>
+
+  <Loggers>
+    <Root level="trace">
+      <AppenderRef ref="List"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file

Reply via email to