diff --git a/log4j-codegen/.picocli-application-activator b/log4j-codegen/.picocli-application-activator deleted file mode 100644 index a395748..0000000 --- a/log4j-codegen/.picocli-application-activator +++ /dev/null @@ -1,17 +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. -# -This file activates the `picocli` profile diff --git a/log4j-codegen/pom.xml b/log4j-codegen/pom.xml deleted file mode 100644 index b63161a..0000000 --- a/log4j-codegen/pom.xml +++ /dev/null @@ -1,73 +0,0 @@ - - - - 4.0.0 - - org.apache.logging.log4j - log4j-transform-parent - ${revision} - ../log4j-transform-parent - - - log4j-codegen - Apache Log4j Code generator - Generates wrappers for Log4j code loggers. - - - - false - - org.apache.logging.log4j.codegen.Generate - - - 3.4.1 - - - 4.7.6 - - - - - - - info.picocli - picocli - - - - - org.apache.logging.log4j - log4j-api - test - - - - org.apache.logging.log4j - log4j-api-test - test - - - - org.junit.jupiter - junit-jupiter-api - test - - - - - diff --git a/log4j-codegen/src/main/java/org/apache/logging/log4j/codegen/Generate.java b/log4j-codegen/src/main/java/org/apache/logging/log4j/codegen/Generate.java deleted file mode 100644 index 2cdf78a..0000000 --- a/log4j-codegen/src/main/java/org/apache/logging/log4j/codegen/Generate.java +++ /dev/null @@ -1,1159 +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.codegen; - -import static java.nio.charset.StandardCharsets.UTF_8; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import java.io.FilterWriter; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import picocli.CommandLine; -import picocli.CommandLine.Command; -import picocli.CommandLine.Model.CommandSpec; -import picocli.CommandLine.Option; -import picocli.CommandLine.ParameterException; -import picocli.CommandLine.Parameters; -import picocli.CommandLine.Spec; - -/** - * Generates source code for custom or extended logger wrappers. - *

Usage

- *

Extended logger

- *

- * To generate source code for an extended logger that adds custom log levels to the existing ones run: - *

- *
- * {@code java -jar log4j-generator.jar extendedLogger  =
- * [CUSTOMLEVEL2=WEIGHT2 [CUSTOMLEVEL3=WEIGHT3] ...]}
- * 
- *

- * Example: - *

- *
- * {@code java -jar log4j-generator.jar extendedLogger com.mycomp.ExtLogger DIAG=350 NOTICE=450 VERBOSE=550}
- * 
- *

Custom logger

- *

- * To generate source code for a custom logger that replaces the existing log levels with custom ones run: - *

- *
- * {@code java -jar log4j-generator.jar customLogger  =
- * [CUSTOMLEVEL2=WEIGHT2 [CUSTOMLEVEL3=WEIGHT3] ...]}
- * 
- *

- * Example: - *

- * {@code java -jar log4j-generator.jar customLogger com.mycomp.MyLogger DEFCON1=350 DEFCON2=450 DEFCON3=550} - */ -@Command(name = "generate") -public final class Generate { - // Implementation note: - // The generated code is in the user's namespace which has its own versioning scheme, so - // any @since tags in the generated code deliberately mention "Log4j-2.x" rather than just the log4j version number. - - static final String PACKAGE_DECLARATION = "package %s;%n%n"; - - enum Type { - CUSTOM { - @Override - String imports() { - // @formatter:off - return "" - + "import org.apache.logging.log4j.Level;%n" - + "import org.apache.logging.log4j.LogManager;%n" - + "import org.apache.logging.log4j.Logger;%n" - + "import org.apache.logging.log4j.Marker;%n" - + "import org.apache.logging.log4j.message.Message;%n" - + "import org.apache.logging.log4j.message.MessageFactory;%n" - + "import org.apache.logging.log4j.spi.AbstractLogger;%n" - + "import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;%n" - + "import org.apache.logging.log4j.util.MessageSupplier;%n" - + "import org.apache.logging.log4j.util.Supplier;%n" - + "%n"; - // @formatter:on - } - - @Override - String declaration() { - // @formatter:off - return "" - + "/**%n" - + " * Custom Logger interface with convenience methods for%n" - + " * %s%n" - + " *

Compatible with Log4j 2.6 or higher.

%n" - + " */%n" - + "public final class %s {%n" - + " private final ExtendedLoggerWrapper logger;%n" - + "%n"; - // @formatter:on - } - - @Override - String constructor() { - // @formatter:off - return "" - + "%n" - + " private %s(final Logger logger) {%n" - + " this.logger = new ExtendedLoggerWrapper((AbstractLogger) logger, logger.getName(), " - + "logger.getMessageFactory());%n" - + " }%n"; - // @formatter:on - } - }, - EXTEND { - @Override - String imports() { - // @formatter:off - return "" - + "import org.apache.logging.log4j.Level;%n" - + "import org.apache.logging.log4j.LogManager;%n" - + "import org.apache.logging.log4j.Logger;%n" - + "import org.apache.logging.log4j.Marker;%n" - + "import org.apache.logging.log4j.message.Message;%n" - + "import org.apache.logging.log4j.message.MessageFactory;%n" - + "import org.apache.logging.log4j.spi.AbstractLogger;%n" - + "import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;%n" - + "import org.apache.logging.log4j.util.MessageSupplier;%n" - + "import org.apache.logging.log4j.util.Supplier;%n" - + "%n"; - // @formatter:on - } - - @Override - String declaration() { - // @formatter:off - return "" - + "/**%n" - + " * Extended Logger interface with convenience methods for%n" - + " * %s%n" - + " *

Compatible with Log4j 2.6 or higher.

%n" - + " */%n" - + "public final class %s extends ExtendedLoggerWrapper {%n" - + " private static final long serialVersionUID = " + System.nanoTime() + "L;%n" - + " private final ExtendedLoggerWrapper logger;%n" - + "%n"; - // @formatter:on - } - - @Override - String constructor() { - // @formatter:off - return "" - + "%n" - + " private %s(final Logger logger) {%n" - + " super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory());%n" - + " this.logger = this;%n" - + " }%n"; - // @formatter:on - } - }; - - abstract String imports(); - - abstract String declaration(); - - abstract String constructor(); - } - - static final String FQCN_FIELD = "" + " private static final String FQCN = %s.class.getName();%n"; - - static final String LEVEL_FIELD = "" + " private static final Level %s = Level.forName(\"%s\", %d);%n"; - - static final String FACTORY_METHODS = "" - // @formatter:off - + "%n" - + " /**%n" - + " * Returns a custom Logger with the name of the calling class.%n" - + " * %n" - + " * @return The custom Logger for the calling class.%n" - + " */%n" - + " public static CLASSNAME create() {%n" - + " final Logger wrapped = LogManager.getLogger();%n" - + " return new CLASSNAME(wrapped);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Returns a custom Logger using the fully qualified name of the Class as%n" - + " * the Logger name.%n" - + " * %n" - + " * @param loggerName The Class whose name should be used as the Logger name.%n" - + " * If null it will default to the calling class.%n" - + " * @return The custom Logger.%n" - + " */%n" - + " public static CLASSNAME create(final Class loggerName) {%n" - + " final Logger wrapped = LogManager.getLogger(loggerName);%n" - + " return new CLASSNAME(wrapped);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Returns a custom Logger using the fully qualified name of the Class as%n" - + " * the Logger name.%n" - + " * %n" - + " * @param loggerName The Class whose name should be used as the Logger name.%n" - + " * If null it will default to the calling class.%n" - + " * @param messageFactory The message factory is used only when creating a%n" - + " * logger, subsequent use does not change the logger but will log%n" - + " * a warning if mismatched.%n" - + " * @return The custom Logger.%n" - + " */%n" - + " public static CLASSNAME create(final Class loggerName, final MessageFactory" - + " messageFactory) {%n" - + " final Logger wrapped = LogManager.getLogger(loggerName, messageFactory);%n" - + " return new CLASSNAME(wrapped);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Returns a custom Logger using the fully qualified class name of the value%n" - + " * as the Logger name.%n" - + " * %n" - + " * @param value The value whose class name should be used as the Logger%n" - + " * name. If null the name of the calling class will be used as%n" - + " * the logger name.%n" - + " * @return The custom Logger.%n" - + " */%n" - + " public static CLASSNAME create(final Object value) {%n" - + " final Logger wrapped = LogManager.getLogger(value);%n" - + " return new CLASSNAME(wrapped);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Returns a custom Logger using the fully qualified class name of the value%n" - + " * as the Logger name.%n" - + " * %n" - + " * @param value The value whose class name should be used as the Logger%n" - + " * name. If null the name of the calling class will be used as%n" - + " * the logger name.%n" - + " * @param messageFactory The message factory is used only when creating a%n" - + " * logger, subsequent use does not change the logger but will log%n" - + " * a warning if mismatched.%n" - + " * @return The custom Logger.%n" - + " */%n" - + " public static CLASSNAME create(final Object value, final MessageFactory messageFactory) {%n" - + " final Logger wrapped = LogManager.getLogger(value, messageFactory);%n" - + " return new CLASSNAME(wrapped);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Returns a custom Logger with the specified name.%n" - + " * %n" - + " * @param name The logger name. If null the name of the calling class will%n" - + " * be used.%n" - + " * @return The custom Logger.%n" - + " */%n" - + " public static CLASSNAME create(final String name) {%n" - + " final Logger wrapped = LogManager.getLogger(name);%n" - + " return new CLASSNAME(wrapped);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Returns a custom Logger with the specified name.%n" - + " * %n" - + " * @param name The logger name. If null the name of the calling class will%n" - + " * be used.%n" - + " * @param messageFactory The message factory is used only when creating a%n" - + " * logger, subsequent use does not change the logger but will log%n" - + " * a warning if mismatched.%n" - + " * @return The custom Logger.%n" - + " */%n" - + " public static CLASSNAME create(final String name, final MessageFactory messageFactory) {%n" - + " final Logger wrapped = LogManager.getLogger(name, messageFactory);%n" - + " return new CLASSNAME(wrapped);%n" - + " }%n"; - // @formatter:on - - static final String METHODS = "" - // @formatter:off - + "%n" - + " /**%n" - + " * Logs a message with the specific Marker at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param msg the message string to be logged%n" - + " */%n" - + " public void methodName(final Marker marker, final Message msg) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msg, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with the specific Marker at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param msg the message string to be logged%n" - + " * @param t A Throwable or null.%n" - + " */%n" - + " public void methodName(final Marker marker, final Message msg, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msg, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message object with the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message object to log.%n" - + " */%n" - + " public void methodName(final Marker marker, final Object message) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message CharSequence with the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message CharSequence to log.%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final CharSequence message) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message at the {@code CUSTOM_LEVEL} level including the stack trace of%n" - + " * the {@link Throwable} {@code t} passed as parameter.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " */%n" - + " public void methodName(final Marker marker, final Object message, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message at the {@code CUSTOM_LEVEL} level including the stack trace of%n" - + " * the {@link Throwable} {@code t} passed as parameter.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the CharSequence to log.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final CharSequence message, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message object with the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message object to log.%n" - + " */%n" - + " public void methodName(final Marker marker, final String message) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param params parameters to the message.%n" - + " * @see #getMessageFactory()%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object... params) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, params);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2, p3);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2, p3, p4);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2, p3, p4, p5);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2, p3, p4, p5, p6);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @param p7 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6,%n" - + " final Object p7) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2, p3, p4, p5, p6, p7);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @param p7 parameter to the message.%n" - + " * @param p8 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6,%n" - + " final Object p7, final Object p8) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2, p3, p4, p5, p6, p7, " - + "p8);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @param p7 parameter to the message.%n" - + " * @param p8 parameter to the message.%n" - + " * @param p9 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6,%n" - + " final Object p7, final Object p8, final Object p9) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, p0, p1, p2, p3, p4, p5, p6, p7, " - + "p8, p9);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message at the {@code CUSTOM_LEVEL} level including the stack trace of%n" - + " * the {@link Throwable} {@code t} passed as parameter.%n" - + " * %n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs the specified Message at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param msg the message string to be logged%n" - + " */%n" - + " public void methodName(final Message msg) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msg, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs the specified Message at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param msg the message string to be logged%n" - + " * @param t A Throwable or null.%n" - + " */%n" - + " public void methodName(final Message msg, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msg, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message object with the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message object to log.%n" - + " */%n" - + " public void methodName(final Object message) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message at the {@code CUSTOM_LEVEL} level including the stack trace of%n" - + " * the {@link Throwable} {@code t} passed as parameter.%n" - + " * %n" - + " * @param message the message to log.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " */%n" - + " public void methodName(final Object message, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message CharSequence with the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message CharSequence to log.%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final CharSequence message) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a CharSequence at the {@code CUSTOM_LEVEL} level including the stack trace of%n" - + " * the {@link Throwable} {@code t} passed as parameter.%n" - + " * %n" - + " * @param message the CharSequence to log.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final CharSequence message, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message object with the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message object to log.%n" - + " */%n" - + " public void methodName(final String message) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param params parameters to the message.%n" - + " * @see #getMessageFactory()%n" - + " */%n" - + " public void methodName(final String message, final Object... params) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, params);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2, p3);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2, p3, p4);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2, p3, p4, p5);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2, p3, p4, p5, p6);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @param p7 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6,%n" - + " final Object p7) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2, p3, p4, p5, p6, p7);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @param p7 parameter to the message.%n" - + " * @param p8 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6,%n" - + " final Object p7, final Object p8) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2, p3, p4, p5, p6, p7, " - + "p8);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters at the {@code CUSTOM_LEVEL} level.%n" - + " * %n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param p0 parameter to the message.%n" - + " * @param p1 parameter to the message.%n" - + " * @param p2 parameter to the message.%n" - + " * @param p3 parameter to the message.%n" - + " * @param p4 parameter to the message.%n" - + " * @param p5 parameter to the message.%n" - + " * @param p6 parameter to the message.%n" - + " * @param p7 parameter to the message.%n" - + " * @param p8 parameter to the message.%n" - + " * @param p9 parameter to the message.%n" - + " * @see #getMessageFactory()%n" - + " * @since Log4j-2.6%n" - + " */%n" - + " public void methodName(final String message, final Object p0, " - + "final Object p1, final Object p2,%n" - + " final Object p3, final Object p4, final Object p5, final Object p6,%n" - + " final Object p7, final Object p8, final Object p9) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, p0, p1, p2, p3, p4, p5, p6, p7, " - + "p8, p9);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message at the {@code CUSTOM_LEVEL} level including the stack trace of%n" - + " * the {@link Throwable} {@code t} passed as parameter.%n" - + " * %n" - + " * @param message the message to log.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " */%n" - + " public void methodName(final String message, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message which is only to be constructed if the logging level is the {@code CUSTOM_LEVEL}" - + "level.%n" - + " *%n" - + " * @param msgSupplier A function, which when called, produces the desired log message;%n" - + " * the format depends on the message factory.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final Supplier msgSupplier) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" - + " * level) including the stack trace of the {@link Throwable} t passed as parameter.%n" - + " *%n" - + " * @param msgSupplier A function, which when called, produces the desired log message;%n" - + " * the format depends on the message factory.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final Supplier msgSupplier, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message which is only to be constructed if the logging level is the%n" - + " * {@code CUSTOM_LEVEL} level with the specified Marker.%n" - + " *%n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param msgSupplier A function, which when called, produces the desired log message;%n" - + " * the format depends on the message factory.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final Marker marker, final Supplier msgSupplier) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters which are only to be constructed if the logging level is the%n" - + " * {@code CUSTOM_LEVEL} level.%n" - + " *%n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param paramSuppliers An array of functions, which when called, produce the desired log" - + " message parameters.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final Marker marker, final String message, final Supplier..." - + " paramSuppliers) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, paramSuppliers);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" - + " * level) with the specified Marker and including the stack trace of the {@link Throwable}%n" - + " * t passed as parameter.%n" - + " *%n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param msgSupplier A function, which when called, produces the desired log message;%n" - + " * the format depends on the message factory.%n" - + " * @param t A Throwable or null.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final Marker marker, final Supplier msgSupplier, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message with parameters which are only to be constructed if the logging level is%n" - + " * the {@code CUSTOM_LEVEL} level.%n" - + " *%n" - + " * @param message the message to log; the format depends on the message factory.%n" - + " * @param paramSuppliers An array of functions, which when called, produce the desired log" - + " message parameters.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final String message, final Supplier... paramSuppliers) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, paramSuppliers);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message which is only to be constructed if the logging level is the%n" - + " * {@code CUSTOM_LEVEL} level with the specified Marker. The {@code MessageSupplier} may or may%n" - + " * not use the {@link MessageFactory} to construct the {@code Message}.%n" - + " *%n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param msgSupplier A function, which when called, produces the desired log message.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final Marker marker, final MessageSupplier msgSupplier) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" - + " * level) with the specified Marker and including the stack trace of the {@link Throwable}%n" - + " * t passed as parameter. The {@code MessageSupplier} may or may not use the%n" - + " * {@link MessageFactory} to construct the {@code Message}.%n" - + " *%n" - + " * @param marker the marker data specific to this log statement%n" - + " * @param msgSupplier A function, which when called, produces the desired log message.%n" - + " * @param t A Throwable or null.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final Marker marker, final MessageSupplier msgSupplier, final " - + "Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, t);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message which is only to be constructed if the logging level is the%n" - + " * {@code CUSTOM_LEVEL} level. The {@code MessageSupplier} may or may not use the%n" - + " * {@link MessageFactory} to construct the {@code Message}.%n" - + " *%n" - + " * @param msgSupplier A function, which when called, produces the desired log message.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final MessageSupplier msgSupplier) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, (Throwable) null);%n" - + " }%n" - + "%n" - + " /**%n" - + " * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" - + " * level) including the stack trace of the {@link Throwable} t passed as parameter.%n" - + " * The {@code MessageSupplier} may or may not use the {@link MessageFactory} to construct the%n" - + " * {@code Message}.%n" - + " *%n" - + " * @param msgSupplier A function, which when called, produces the desired log message.%n" - + " * @param t the exception to log, including its stack trace.%n" - + " * @since Log4j-2.4%n" - + " */%n" - + " public void methodName(final MessageSupplier msgSupplier, final Throwable t) {%n" - + " logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, t);%n" - + " }%n"; - // @formatter:on - - @Option(names = "-f", description = "Output file.") - private Path outputFile; - - @Spec - private CommandSpec spec; - - private Generate() {} - - public static class LevelInfo { - final String name; - final int intLevel; - - LevelInfo(final String name, final int intLevel) { - this.name = name; - this.intLevel = intLevel; - } - } - - public static void main(final String[] args) { - final Generate command = new Generate(); - new CommandLine(command) - .registerConverter(LevelInfo.class, command::convertLevelInfo) - .execute(args); - } - - private LevelInfo convertLevelInfo(final String value) { - final String[] parts = value.split("=", 2); - if (parts.length == 2) { - try { - final int intLevel = Integer.parseInt(parts[1]); - return new LevelInfo(parts[0], intLevel); - } catch (final NumberFormatException ignored) { - // Fall through to throw instruction - } - } - throw new ParameterException( - spec.commandLine(), - String.format("Invalid level value '%s'; expected form CUSTOMLEVEL=WEIGHT%nE.g. AUDIT=50", value)); - } - - private Writer getWriter() throws IOException { - if (outputFile != null) { - return Files.newBufferedWriter(outputFile, UTF_8); - } - return new FilterWriter(new OutputStreamWriter(System.out, UTF_8)) { - - @Override - public void close() throws IOException { - // Don't close the standard output. - } - }; - } - - private void validateLevels(final List levels) { - if (levels == null || levels.isEmpty()) { - throw new ParameterException(spec.commandLine(), "At least one level parameter is required"); - } - } - - /** - * Generates source code for extended logger wrappers that provide convenience methods for the specified custom - * levels. - * - * @param classNameFQN className of the custom logger to generate - * @param levels a list of NAME=intLevel pair for each custom log - * level to generate convenience methods for - */ - @Command(name = "extendedLogger", description = "Generates a logger with additional log levels.") - public void generateExtend( - final @Parameters(description = "Class name to generate", paramLabel = "") String classNameFQN, - final @Parameters(description = "Additional log levels", paramLabel = "") List levels) - throws IOException { - validateLevels(levels); - try (final Writer writer = getWriter()) { - generateSource(Type.EXTEND, classNameFQN, levels, writer); - writer.flush(); - } - } - - /** - * Generates source code for custom logger wrappers that only provide convenience methods for the specified - * custom levels, not for the standard built-in levels. - * - * @param classNameFQN className of the custom logger to generate - * @param levels a list of NAME=intLevel pair for each custom log - * level to generate convenience methods for - */ - @Command(name = "customLogger", description = "Generates a logger with custom log methods.") - public void generateCustom( - final @Parameters(description = "Class name to generate", paramLabel = "") String classNameFQN, - final @Parameters(description = "Log levels", paramLabel = "") List levels) - throws IOException { - validateLevels(levels); - try (final Writer writer = getWriter()) { - generateSource(Type.CUSTOM, classNameFQN, levels, writer); - writer.flush(); - } - } - - @SuppressFBWarnings( - value = "FORMAT_STRING_MANIPULATION", - justification = "The format strings come from constants. The replacement is done for readability.") - static String generateSource( - final Type type, final String classNameFQN, final List levels, final Writer writer) - throws IOException { - final int lastDot = classNameFQN.lastIndexOf('.'); - final String pkg = classNameFQN.substring(0, Math.max(lastDot, 0)); - if (!pkg.isEmpty()) { - writer.append(String.format(PACKAGE_DECLARATION, pkg)); - } - writer.append(String.format(type.imports(), "")); - final String className = classNameFQN.substring(classNameFQN.lastIndexOf('.') + 1); - final String javadocDescr = javadocDescription(levels); - writer.append(String.format(type.declaration(), javadocDescr, className)); - writer.append(String.format(FQCN_FIELD, className)); - for (final LevelInfo level : levels) { - writer.append(String.format(LEVEL_FIELD, level.name, level.name, level.intLevel)); - } - writer.append(String.format(type.constructor(), className)); - writer.append(String.format(FACTORY_METHODS.replaceAll("CLASSNAME", className), "")); - for (final LevelInfo level : levels) { - final String methodName = camelCase(level.name); - final String phase1 = METHODS.replaceAll("CUSTOM_LEVEL", level.name); - final String phase2 = phase1.replaceAll("methodName", methodName); - writer.append(String.format(phase2, "")); - } - - writer.append('}'); - writer.append(System.getProperty("line.separator")); - return writer.toString(); - } - - static String javadocDescription(final List levels) { - if (levels.size() == 1) { - return "the " + levels.get(0).name + " custom log level."; - } - final StringBuilder sb = new StringBuilder(512); - sb.append("the "); - String sep = ""; - for (int i = 0; i < levels.size(); i++) { - sb.append(sep); - sb.append(levels.get(i).name); - sep = (i == levels.size() - 2) ? " and " : ", "; - } - sb.append(" custom log levels."); - return sb.toString(); - } - - static String camelCase(final String customLevel) { - final StringBuilder sb = new StringBuilder(customLevel.length()); - boolean lower = true; - for (final char ch : customLevel.toCharArray()) { - if (ch == '_') { - lower = false; - continue; - } - sb.append(lower ? Character.toLowerCase(ch) : Character.toUpperCase(ch)); - lower = true; - } - return sb.toString(); - } -} diff --git a/log4j-codegen/src/main/java/org/apache/logging/log4j/codegen/package-info.java b/log4j-codegen/src/main/java/org/apache/logging/log4j/codegen/package-info.java deleted file mode 100644 index 028b41b..0000000 --- a/log4j-codegen/src/main/java/org/apache/logging/log4j/codegen/package-info.java +++ /dev/null @@ -1,25 +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. - */ -/** - * Log4j 2 command line tools. - */ -@Export -@Version("2.20.1") -package org.apache.logging.log4j.codegen; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/log4j-codegen/src/test/java/org/apache/logging/log4j/codegen/GenerateCustomLoggerTest.java b/log4j-codegen/src/test/java/org/apache/logging/log4j/codegen/GenerateCustomLoggerTest.java deleted file mode 100644 index a953f83..0000000 --- a/log4j-codegen/src/test/java/org/apache/logging/log4j/codegen/GenerateCustomLoggerTest.java +++ /dev/null @@ -1,189 +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.codegen; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.BufferedWriter; -import java.io.File; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import javax.tools.Diagnostic; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.StandardJavaFileManager; -import javax.tools.ToolProvider; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.codegen.Generate.LevelInfo; -import org.apache.logging.log4j.codegen.Generate.Type; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.test.TestLogger; -import org.apache.logging.log4j.util.MessageSupplier; -import org.apache.logging.log4j.util.Strings; -import org.apache.logging.log4j.util.Supplier; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; -import org.junitpioneer.jupiter.SetSystemProperty; - -@Tag("functional") -@SetSystemProperty( - key = "log4j2.loggerContextFactory", - value = "org.apache.logging.log4j.test.TestLoggerContextFactory") -public class GenerateCustomLoggerTest { - - private static final String TEST_SOURCE = "target/test-classes/org/apache/logging/log4j/core/MyCustomLogger.java"; - - @AfterAll - public static void afterClass() { - File file = new File(TEST_SOURCE); - final File parent = file.getParentFile(); - if (file.exists()) { - file.delete(); - } - file = new File(parent, "MyCustomLogger.class"); - if (file.exists()) { - file.delete(); - } - } - - @Test - @SuppressWarnings("ReturnValueIgnored") - public void testGenerateSource() throws Exception { - final String CLASSNAME = "org.apache.logging.log4j.core.MyCustomLogger"; - - // generate custom logger source - final List levels = Arrays.asList( - new LevelInfo("DEFCON1", 350), new LevelInfo("DEFCON2", 450), new LevelInfo("DEFCON3", 550)); - final Path testSource = Paths.get(TEST_SOURCE); - Files.createDirectories(testSource.getParent()); - try (final BufferedWriter writer = Files.newBufferedWriter(testSource, UTF_8)) { - Generate.generateSource(Type.CUSTOM, CLASSNAME, levels, writer); - } - - // set up compiler - final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - final DiagnosticCollector diagnostics = new DiagnosticCollector<>(); - final List errors = new ArrayList<>(); - try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) { - final Iterable compilationUnits = - fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(testSource.toFile())); - final String classPath = System.getProperty("jdk.module.path"); - final List optionList = new ArrayList<>(); - if (Strings.isNotBlank(classPath)) { - optionList.add("-classpath"); - optionList.add(classPath); - } - // compile generated source - compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnits) - .call(); - - // check we don't have any compilation errors - for (final Diagnostic diagnostic : diagnostics.getDiagnostics()) { - if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { - errors.add(String.format("Compile error: %s%n", diagnostic.getMessage(Locale.getDefault()))); - } - } - } - assertTrue(errors.isEmpty(), errors.toString()); - - // load the compiled class - final Class cls = Class.forName(CLASSNAME); - - // check that all factory methods exist and are static - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create").getModifiers())); - assertTrue( - Modifier.isStatic(cls.getDeclaredMethod("create", Class.class).getModifiers())); - assertTrue( - Modifier.isStatic(cls.getDeclaredMethod("create", Object.class).getModifiers())); - assertTrue( - Modifier.isStatic(cls.getDeclaredMethod("create", String.class).getModifiers())); - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create", Class.class, MessageFactory.class) - .getModifiers())); - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create", Object.class, MessageFactory.class) - .getModifiers())); - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create", String.class, MessageFactory.class) - .getModifiers())); - - // check that all log methods exist - final String[] logMethods = {"defcon1", "defcon2", "defcon3"}; - for (final String name : logMethods) { - assertDoesNotThrow(() -> { - cls.getDeclaredMethod(name, Marker.class, Message.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, Object.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, String.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, Message.class); - cls.getDeclaredMethod(name, Marker.class, Object.class); - cls.getDeclaredMethod(name, Marker.class, String.class); - cls.getDeclaredMethod(name, Message.class); - cls.getDeclaredMethod(name, Object.class); - cls.getDeclaredMethod(name, String.class); - cls.getDeclaredMethod(name, Message.class, Throwable.class); - cls.getDeclaredMethod(name, Object.class, Throwable.class); - cls.getDeclaredMethod(name, String.class, Throwable.class); - cls.getDeclaredMethod(name, String.class, Object[].class); - cls.getDeclaredMethod(name, Marker.class, String.class, Object[].class); - - // 2.4 lambda support - cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class); - cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, String.class, Supplier[].class); - cls.getDeclaredMethod(name, Marker.class, Supplier.class); - cls.getDeclaredMethod(name, Marker.class, Supplier.class, Throwable.class); - cls.getDeclaredMethod(name, MessageSupplier.class); - cls.getDeclaredMethod(name, MessageSupplier.class, Throwable.class); - cls.getDeclaredMethod(name, String.class, Supplier[].class); - cls.getDeclaredMethod(name, Supplier.class); - cls.getDeclaredMethod(name, Supplier.class, Throwable.class); - }); - } - - final TestLogger underlying = (TestLogger) LogManager.getLogger("X.Y.Z"); - - try { - // now see if it actually works... - final Method create = cls.getDeclaredMethod("create", String.class); - final Object customLogger = create.invoke(null, "X.Y.Z"); - int n = 0; - for (final String name : logMethods) { - final Method method = cls.getDeclaredMethod(name, String.class); - method.invoke(customLogger, "This is message " + n++); - } - - final List lines = underlying.getEntries(); - for (int i = 0; i < lines.size(); i++) { - assertEquals(" " + levels.get(i).name + " This is message " + i, lines.get(i)); - } - } finally { - underlying.getEntries().clear(); - } - } -} diff --git a/log4j-codegen/src/test/java/org/apache/logging/log4j/codegen/GenerateExtendedLoggerTest.java b/log4j-codegen/src/test/java/org/apache/logging/log4j/codegen/GenerateExtendedLoggerTest.java deleted file mode 100644 index 9795dd8..0000000 --- a/log4j-codegen/src/test/java/org/apache/logging/log4j/codegen/GenerateExtendedLoggerTest.java +++ /dev/null @@ -1,209 +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.codegen; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.BufferedWriter; -import java.io.File; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import javax.tools.Diagnostic; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.StandardJavaFileManager; -import javax.tools.ToolProvider; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.codegen.Generate.LevelInfo; -import org.apache.logging.log4j.codegen.Generate.Type; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.apache.logging.log4j.test.TestLogger; -import org.apache.logging.log4j.util.MessageSupplier; -import org.apache.logging.log4j.util.Strings; -import org.apache.logging.log4j.util.Supplier; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; -import org.junitpioneer.jupiter.SetSystemProperty; - -@Tag("functional") -@SetSystemProperty( - key = "log4j2.loggerContextFactory", - value = "org.apache.logging.log4j.test.TestLoggerContextFactory") -public class GenerateExtendedLoggerTest { - - private static final String TEST_SOURCE = "target/test-classes/org/apache/logging/log4j/core/MyExtendedLogger.java"; - - @AfterAll - public static void afterClass() { - File file = new File(TEST_SOURCE); - final File parent = file.getParentFile(); - if (file.exists()) { - file.delete(); - } - file = new File(parent, "MyExtendedLogger.class"); - if (file.exists()) { - file.delete(); - } - } - - @Test - @SuppressWarnings("ReturnValueIgnored") - public void testGenerateSource() throws Exception { - final String CLASSNAME = "org.apache.logging.log4j.core.MyExtendedLogger"; - - // generate custom logger source - final List levels = - Arrays.asList(new LevelInfo("DIAG", 350), new LevelInfo("NOTICE", 450), new LevelInfo("VERBOSE", 550)); - final Path testSource = Paths.get(TEST_SOURCE); - Files.createDirectories(testSource.getParent()); - try (final BufferedWriter writer = Files.newBufferedWriter(testSource, UTF_8)) { - Generate.generateSource(Type.EXTEND, CLASSNAME, levels, writer); - } - - // set up compiler - final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - final DiagnosticCollector diagnostics = new DiagnosticCollector<>(); - final List errors = new ArrayList<>(); - try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) { - final Iterable compilationUnits = - fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(testSource.toFile())); - final String classPath = System.getProperty("jdk.module.path"); - final List optionList = new ArrayList<>(); - if (Strings.isNotBlank(classPath)) { - optionList.add("-classpath"); - optionList.add(classPath); - } - // compile generated source - compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnits) - .call(); - - // check we don't have any compilation errors - for (final Diagnostic diagnostic : diagnostics.getDiagnostics()) { - if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { - errors.add(String.format("Compile error: %s%n", diagnostic.getMessage(Locale.getDefault()))); - } - } - } - assertTrue(errors.isEmpty(), errors.toString()); - - // load the compiled class - final Class cls = Class.forName(CLASSNAME); - - // check that all factory methods exist and are static - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create").getModifiers())); - assertTrue( - Modifier.isStatic(cls.getDeclaredMethod("create", Class.class).getModifiers())); - assertTrue( - Modifier.isStatic(cls.getDeclaredMethod("create", Object.class).getModifiers())); - assertTrue( - Modifier.isStatic(cls.getDeclaredMethod("create", String.class).getModifiers())); - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create", Class.class, MessageFactory.class) - .getModifiers())); - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create", Object.class, MessageFactory.class) - .getModifiers())); - assertTrue(Modifier.isStatic(cls.getDeclaredMethod("create", String.class, MessageFactory.class) - .getModifiers())); - - // check that the extended log methods exist - final String[] extendedMethods = {"diag", "notice", "verbose"}; - for (final String name : extendedMethods) { - assertDoesNotThrow(() -> { - cls.getDeclaredMethod(name, Marker.class, Message.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, Object.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, String.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, Message.class); - cls.getDeclaredMethod(name, Marker.class, Object.class); - cls.getDeclaredMethod(name, Marker.class, String.class); - cls.getDeclaredMethod(name, Message.class); - cls.getDeclaredMethod(name, Object.class); - cls.getDeclaredMethod(name, String.class); - cls.getDeclaredMethod(name, Message.class, Throwable.class); - cls.getDeclaredMethod(name, Object.class, Throwable.class); - cls.getDeclaredMethod(name, String.class, Throwable.class); - cls.getDeclaredMethod(name, String.class, Object[].class); - cls.getDeclaredMethod(name, Marker.class, String.class, Object[].class); - - // 2.4 lambda support - cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class); - cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class, Throwable.class); - cls.getDeclaredMethod(name, Marker.class, String.class, Supplier[].class); - cls.getDeclaredMethod(name, Marker.class, Supplier.class); - cls.getDeclaredMethod(name, Marker.class, Supplier.class, Throwable.class); - cls.getDeclaredMethod(name, MessageSupplier.class); - cls.getDeclaredMethod(name, MessageSupplier.class, Throwable.class); - cls.getDeclaredMethod(name, String.class, Supplier[].class); - cls.getDeclaredMethod(name, Supplier.class); - cls.getDeclaredMethod(name, Supplier.class, Throwable.class); - }); - } - - // now see if it actually works... - final Method create = cls.getDeclaredMethod("create", String.class); - final Object extendedLogger = create.invoke(null, "X.Y.Z"); - int n = 0; - for (final String name : extendedMethods) { - final Method method = cls.getDeclaredMethod(name, String.class); - method.invoke(extendedLogger, "This is message " + n++); - } - - final TestLogger underlying = (TestLogger) LogManager.getLogger("X.Y.Z"); - - try { - // This logger extends o.a.l.log4j.spi.ExtendedLogger, - // so all the standard logging methods can be used as well - final ExtendedLogger logger = (ExtendedLogger) extendedLogger; - logger.trace("trace message"); - logger.debug("debug message"); - logger.info("info message"); - logger.warn("warn message"); - logger.error("error message"); - logger.fatal("fatal message"); - - final List lines = underlying.getEntries(); - for (int i = 0; i < lines.size() - 6; i++) { - assertEquals(" " + levels.get(i).name + " This is message " + i, lines.get(i)); - } - - // test that the standard logging methods still work - int i = lines.size() - 6; - assertEquals(" TRACE trace message", lines.get(i++)); - assertEquals(" DEBUG debug message", lines.get(i++)); - assertEquals(" INFO info message", lines.get(i++)); - assertEquals(" WARN warn message", lines.get(i++)); - assertEquals(" ERROR error message", lines.get(i++)); - assertEquals(" FATAL fatal message", lines.get(i++)); - } finally { - underlying.getEntries().clear(); - } - } -} diff --git a/pom.xml b/pom.xml index 528fc88..da197dd 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,6 @@ log4j-transform-parent - log4j-codegen log4j-converter-config log4j-converter-plugin-descriptor log4j-transform-cli @@ -132,12 +131,6 @@ - - org.apache.logging.log4j - log4j-codegen - ${project.version} - - org.apache.logging.log4j log4j-converter-config diff --git a/src/changelog/.0.x.x/add-codegen.xml b/src/changelog/.0.x.x/add-codegen.xml deleted file mode 100644 index 0fd69a5..0000000 --- a/src/changelog/.0.x.x/add-codegen.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - Adds a - xref:cli.adoc#log4j-codegen[`log4j-codegen` CLI tool] - to generate custom loggers. - - diff --git a/src/site/antora/modules/ROOT/pages/cli.adoc b/src/site/antora/modules/ROOT/pages/cli.adoc index 53ac087..b106c8e 100644 --- a/src/site/antora/modules/ROOT/pages/cli.adoc +++ b/src/site/antora/modules/ROOT/pages/cli.adoc @@ -19,62 +19,6 @@ The Log4j Transform CLI Tools are a set of command line tools to help users with various Log4j-related formats. -[#log4j-codegen] -== `log4j-codegen` - -The `log4j-codegen` tool is a small command line application that generates -{logging-services-url}/log4j/2.x/javadoc/log4j-api/org/apache/logging/log4j/Logger.html[`Logger`] -wrappers with custom log levels. - -[#log4j-codegen-syntax] -=== Syntax - -The general syntax of the tool is: - -[source,subs="+attributes"] ----- -java -jar log4j-codegen-{project-version}.jar [-f=] [...] ----- - -where the elements have the following meaning: - -``:: -The name of the output file. -By default, the output is printed on the standard output. - -``:: -This can be one of: - -extendedLogger::: -Generates a logger class that supports the -{logging-services-url}/log4j/2.x/manual/customloglevels.html[standard log levels] -and some additional ones. - -customLogger::: -Generates a logger class that supports only the levels specified on the command line. - -``:: -The fully qualified name of the `Logger` class to generate. - -``:: -A string of the form `=`, where: - -``::: -The name of the level, e.g. `CONFIG`, `AUDIT`. - -``::: -An integer that specifies the severity of each level. -For a list of standard priorities see -{logging-services-url}/log4j/2.x/manual/customloglevels.html[Levels]. - -[#log4j-codegen-examples] -=== Usage examples - -[source,subs="+attributes"] ----- -java -jar log4j-codegen-{project-version}.jar extendedLogger DIAG=350 NOTICE=450 VERBOSE=550 ----- - [#log4j-converter-plugin-descriptor] == `log4j-converter-plugin-descriptor`