Return-Path: X-Original-To: apmail-logging-commits-archive@minotaur.apache.org Delivered-To: apmail-logging-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6DC53D6FD for ; Mon, 10 Dec 2012 15:40:39 +0000 (UTC) Received: (qmail 8922 invoked by uid 500); 10 Dec 2012 15:40:39 -0000 Delivered-To: apmail-logging-commits-archive@logging.apache.org Received: (qmail 8845 invoked by uid 500); 10 Dec 2012 15:40:37 -0000 Mailing-List: contact commits-help@logging.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@logging.apache.org Delivered-To: mailing list commits@logging.apache.org Received: (qmail 8814 invoked by uid 99); 10 Dec 2012 15:40:36 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Dec 2012 15:40:36 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Dec 2012 15:40:32 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 6308523888CD; Mon, 10 Dec 2012 15:40:10 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1419531 - in /logging/log4j/log4j2/trunk/core/src: main/java/org/apache/logging/log4j/core/pattern/ test/resources/ Date: Mon, 10 Dec 2012 15:40:10 -0000 To: commits@logging.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121210154010.6308523888CD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ggregory Date: Mon Dec 10 15:40:04 2012 New Revision: 1419531 URL: http://svn.apache.org/viewvc?rev=1419531&view=rev Log: You can now use %black, %blue, %cyan, and so on. Removed: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/StyleRedConverter.java logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/StyleRedConverter1.java Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/AbstractStyleNameConverter.java logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console-style-name-ansi.xml Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/AbstractStyleNameConverter.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/AbstractStyleNameConverter.java?rev=1419531&r1=1419530&r2=1419531&view=diff ============================================================================== --- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/AbstractStyleNameConverter.java (original) +++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/AbstractStyleNameConverter.java Mon Dec 10 15:40:04 2012 @@ -16,19 +16,368 @@ */ package org.apache.logging.log4j.core.pattern; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.List; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.layout.PatternLayout; /** * Style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. */ public abstract class AbstractStyleNameConverter extends LogEventPatternConverter { + /** + * Black style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = Black.NAME, type = "Converter") + @ConverterKeys(Black.NAME) + public static final class Black extends AbstractStyleNameConverter { + + protected static final String NAME = "black"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static Black newInstance(Configuration config, final String[] options) { + return newInstance(Black.class, NAME, config, options); + } + + /** + * Constructs the converter. This constructor must be public. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public Black(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * Blue style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = Blue.NAME, type = "Converter") + @ConverterKeys(Blue.NAME) + public static final class Blue extends AbstractStyleNameConverter { + + protected static final String NAME = "blue"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static Blue newInstance(Configuration config, final String[] options) { + return newInstance(Blue.class, NAME, config, options); + } + + /** + * Constructs the converter. This constructor must be public. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public Blue(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * Cyan style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = Cyan.NAME, type = "Converter") + @ConverterKeys(Cyan.NAME) + public static final class Cyan extends AbstractStyleNameConverter { + + protected static final String NAME = "cyan"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static Cyan newInstance(Configuration config, final String[] options) { + return newInstance(Cyan.class, NAME, config, options); + } + + /** + * Constructs the converter. This constructor must be public. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public Cyan(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * Green style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = Green.NAME, type = "Converter") + @ConverterKeys(Green.NAME) + public static final class Green extends AbstractStyleNameConverter { + + protected static final String NAME = "green"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static Green newInstance(Configuration config, final String[] options) { + return newInstance(Green.class, NAME, config, options); + } + + /** + * Constructs the converter. This constructor must be public. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public Green(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * Magenta style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = Magenta.NAME, type = "Converter") + @ConverterKeys(Magenta.NAME) + public static final class Magenta extends AbstractStyleNameConverter { + + protected static final String NAME = "magenta"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static Magenta newInstance(Configuration config, final String[] options) { + return newInstance(Magenta.class, NAME, config, options); + } + + /** + * Constructs the converter. This constructor must be public. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public Magenta(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * Red style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = Red.NAME, type = "Converter") + @ConverterKeys(Red.NAME) + public static final class Red extends AbstractStyleNameConverter { + + protected static final String NAME = "red"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static Red newInstance(Configuration config, final String[] options) { + return newInstance(Red.class, NAME, config, options); + } + + /** + * Constructs the converter. This constructor must be public. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public Red(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * White style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = White.NAME, type = "Converter") + @ConverterKeys(White.NAME) + public static final class White extends AbstractStyleNameConverter { + + protected static final String NAME = "white"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static White newInstance(Configuration config, final String[] options) { + return newInstance(White.class, NAME, config, options); + } + + /** + * Constructs the converter. This constructor must be public. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public White(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * Yellow style pattern converter. Adds ANSI color styling to the result of the enclosed pattern. + */ + @Plugin(name = Yellow.NAME, type = "Converter") + @ConverterKeys(Yellow.NAME) + public static final class Yellow extends AbstractStyleNameConverter { + + protected static final String NAME = "yellow"; + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be + * formatted. + * @return new instance of class or null + */ + public static Yellow newInstance(Configuration config, final String[] options) { + return newInstance(Yellow.class, NAME, config, options); + } + + /** + * Constructs the converter. + * + * @param formatters + * The PatternFormatters to generate the text to manipulate. + * @param styling + * The styling that should encapsulate the pattern. + */ + public Yellow(List formatters, String styling) { + super(NAME, formatters, styling); + } + } + + /** + * Gets an instance of the class (called via reflection). + * + * @param config + * The current Configuration. + * @param options + * The pattern options, may be null. If the first element is "short", only the first line of the throwable will be formatted. + * @return new instance of class or null + */ + private static T newInstance(Class asnConverterClass, String name, Configuration config, + final String[] options) { + List formatters = toPatternFormatterList(config, options); + if (formatters == null) { + return null; + } + try { + final Constructor constructor = asnConverterClass.getConstructor(List.class, String.class); + return constructor.newInstance(formatters, AnsiEscape.createSequence(name)); + } catch (SecurityException e) { + LOGGER.error(e.toString(), e); + } catch (NoSuchMethodException e) { + LOGGER.error(e.toString(), e); + } catch (IllegalArgumentException e) { + LOGGER.error(e.toString(), e); + } catch (InstantiationException e) { + LOGGER.error(e.toString(), e); + } catch (IllegalAccessException e) { + LOGGER.error(e.toString(), e); + } catch (InvocationTargetException e) { + LOGGER.error(e.toString(), e); + } + return null; + } + + /** + * Creates a list of PatternFormatter from the given configuration and options or null if no pattern is supplied. + * + * @param config + * A configuration + * @param options + * pattern options + * @return a list of PatternFormatter from the given configuration and options or null if no pattern is supplied. + */ + private static List toPatternFormatterList(Configuration config, final String[] options) { + if (options.length == 0 || options[0] == null) { + LOGGER.error("No pattern supplied on style for config=" + config); + return null; + } + PatternParser parser = PatternLayout.createPatternParser(config); + if (parser == null) { + LOGGER.error("No PatternParser created for config=" + config + ", options=" + options); + return null; + } + return parser.parse(options[0]); + } + private final List formatters; private final String style; - + /** * Constructs the converter. * Modified: logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console-style-name-ansi.xml URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console-style-name-ansi.xml?rev=1419531&r1=1419530&r2=1419531&view=diff ============================================================================== --- logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console-style-name-ansi.xml (original) +++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j2-console-style-name-ansi.xml Mon Dec 10 15:40:04 2012 @@ -19,8 +19,7 @@ - - +