You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2012/11/01 03:11:50 UTC

svn commit: r1404450 - in /logging/log4j/log4j2/trunk/core/src/test: java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java resources/log4j2-console.xml

Author: ggregory
Date: Thu Nov  1 02:11:49 2012
New Revision: 1404450

URL: http://svn.apache.org/viewvc?rev=1404450&view=rev
Log:
Shows how to use ANSI escape codes to color messages. Each message is printed to the console in color, but the rest of the log entry (time stamp for example) is in the default color for that console.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java   (with props)
    logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console.xml   (with props)

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java?rev=1404450&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java (added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java Thu Nov  1 02:11:49 2012
@@ -0,0 +1,34 @@
+package org.apache.logging.log4j.core.appender;
+
+import java.io.IOException;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configurator;
+
+/**
+ * Shows how to use ANSI escape codes to color messages. Each message is printed to the console in color, but the rest of the log entry
+ * (time stamp for example) is in the default color for that console.
+ */
+public class ColorConsoleAnsiMessagesMain {
+
+    private static final Logger LOG = LogManager.getLogger(ColorConsoleAnsiMessagesMain.class);
+
+    public static void main(String[] args) {
+        LoggerContext ctx = Configurator.initialize(ColorConsoleAnsiMessagesMain.class.getName(), null,
+                "target/test-classes/log4j2-console.xml");
+        try {
+            LOG.fatal("\u001b[1;35mFatal message.\u001b[0m");
+            LOG.error("\u001b[1;31mError message.\u001b[0m");
+            LOG.warn("\u001b[0;33mWarning message.\u001b[0m");
+            LOG.info("\u001b[0;32mInformation message.\u001b[0m");
+            LOG.debug("\u001b[0;36mDebug message.\u001b[0m");
+            LOG.trace("\u001b[1;30mTrace message.\u001b[0m");
+            LOG.error("\u001b[1;31mError message.\u001b[0m", new IOException("test"));
+        } finally {
+            Configurator.shutdown(ctx);
+        }
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ColorConsoleAnsiMessagesMain.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console.xml?rev=1404450&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console.xml (added)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console.xml Thu Nov  1 02:11:49 2012
@@ -0,0 +1,31 @@
+<?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">
+  <appenders>
+    <Console name="Console" target="SYSTEM_OUT">
+      <PatternLayout pattern="%d{ ISO8601 } [%t] %-5level: %msg%n%throwable" />
+    </Console>
+  </appenders>
+  <loggers>
+    <logger name="org.foo" level="DEBUG" />
+    <root level="TRACE">
+      <appender-ref ref="Console" />
+    </root>
+  </loggers>
+</configuration>
\ No newline at end of file

Propchange: logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console.xml
------------------------------------------------------------------------------
    svn:keywords = Id