Add example for JAnsi messages. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f1b41d03 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f1b41d03 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f1b41d03
Branch: refs/heads/feature/LOG4J2-1430 Commit: f1b41d0359e6f930f0c84680eaae6589ea79a5d4 Parents: 334745a Author: ggregory <ggreg...@apache.org> Authored: Thu Jun 16 10:33:19 2016 -0700 Committer: ggregory <ggreg...@apache.org> Committed: Thu Jun 16 10:33:19 2016 -0700 ---------------------------------------------------------------------- .../ConsoleAppenderJAnsiMessageMain.java | 75 ++++++++++++++++++++ 1 file changed, 75 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f1b41d03/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderJAnsiMessageMain.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderJAnsiMessageMain.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderJAnsiMessageMain.java new file mode 100644 index 0000000..53a5bfd --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderJAnsiMessageMain.java @@ -0,0 +1,75 @@ +/* + * 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.appender; + +import static org.fusesource.jansi.Ansi.*; +import static org.fusesource.jansi.Ansi.Color.*; + +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; +import org.junit.Test; + +/** + * 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. + * <p> + * Running from a Windows command line from the root of the project: + * </p> + * + * <pre> + * mvn -Dtest=org.apache.logging.log4j.core.appender.ConsoleAppenderJAnsiMessageMain test + * </pre> + * + * or: + * + * <pre> + * java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.11\jansi-1.11.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderJAnsiMessageMain log4j-core/target/test-classes/log4j2-console-style-ansi.xml + * </pre> + * + */ +public class ConsoleAppenderJAnsiMessageMain { + + public static void main(final String[] args) { + new ConsoleAppenderJAnsiMessageMain().test(args); + } + + /** + * This is a @Test method to make it easy to run from a command line with {@code mvn -Dtest=FQCN test} + */ + @Test + public void test() { + test(null); + } + + public void test(final String[] args) { + // System.out.println(System.getProperty("java.class.path")); + final String config = args == null || args.length == 0 ? "target/test-classes/log4j2-console-style-ansi.xml" + : args[0]; + final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config); + final Logger logger = LogManager.getLogger(ConsoleAppenderJAnsiMessageMain.class); + try { + logger.info(ansi().fg(RED).a("Hello").fg(CYAN).a(" World").reset()); + // JAnsi format: + // logger.info("@|red Hello|@ @|cyan World|@"); + } finally { + Configurator.shutdown(ctx); + } + } + +}