gemmellr commented on code in PR #4246: URL: https://github.com/apache/activemq-artemis/pull/4246#discussion_r991242486
########## artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/InitLogging.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.activemq.artemis.cli.commands.tools; + +import java.io.File; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.invoke.MethodHandles; +import java.util.Collections; + +import io.airlift.airline.Command; +import io.airlift.airline.Option; +import org.apache.activemq.artemis.cli.commands.ActionAbstract; +import org.apache.activemq.artemis.cli.commands.ActionContext; +import org.apache.activemq.artemis.cli.commands.Create; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Command(name = "init-logging", description = "Initialize a default log4j configuration") +public class InitLogging extends ActionAbstract { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public static final String OLD_LOG_NAME = "logging.properties"; + + @Option(name = "--force", description = "Overwrite configuration at destination directory") + private boolean force; + + @Override + public Object execute(ActionContext context) throws Exception { + super.execute(context); + + initLogging(force, new File(getBrokerInstance())); + + return null; + } + + + public static void initLogging(boolean force, File brokerInstance) throws Exception { + File etc = new File(brokerInstance, "etc"); + File newLogging = new File(etc, Create.ETC_LOG4J2_PROPERTIES); + + if (!force && newLogging.exists()) { + System.err.println("A configuration file " + newLogging.getAbsolutePath() + " already exists! Use the option -f if you want to re-initialize it"); + return; + } + + Create.write("etc/" + Create.ETC_LOG4J2_PROPERTIES, newLogging, Collections.emptyMap(), false, force, "utf-8"); + } + + public static void verifyOlderLogging(File fileInstance) throws Exception { + File etc = new File(fileInstance, "etc"); + File newLogging = new File(etc, Create.ETC_LOG4J2_PROPERTIES); + File oldLogging = new File(etc, OLD_LOG_NAME); + + if (oldLogging.exists() && !newLogging.exists()) { + logger.warn("Logging not initialized!"); + StringWriter out = new StringWriter(); + PrintWriter writer = new PrintWriter(out); + writer.println("It seems you are migrating a previous version of an artemis instance without the new log4j configuration"); + writer.println(); + writer.printf("A new logging configuration is being created at %s", newLogging.getAbsolutePath()); + writer.println(); + writer.printf("The older configuration file %s should be manually removed.", oldLogging.getAbsolutePath()); + ActionContext.system().out.println(out); + + initLogging(false, fileInstance); + } Review Comment: On trying this as-is I notice it prints out 2 different 'old config should be removed' style instructions twice in succession, and also interferes with the first line of the 'Artemis' asciiart, presumably by not having a newline at the end of the second one. It then also has no effect on the first startup since the log4j2.properties file is created too late to have an effect, meaning you have to restart the broker anyway. Someone trying an upgrade like this without actually e.g looking at the script and config changes noted in the upgrade guide, is actually more likely to stop and look due to lasck of regularly verbose startup output, and also the big ClassNotFoundException stacktrace that comes from not having updated the script as documented, with the script still instructing the JVM to use the JBL LogManager which wont be found: ``` [instance-upgrade]$ "/path/to/2.26.0/instance-upgrade/bin/artemis" run Could not load Logmanager "org.jboss.logmanager.LogManager" java.lang.ClassNotFoundException: org.jboss.logmanager.LogManager at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) at java.logging/java.util.logging.LogManager$1.run(LogManager.java:239) at java.logging/java.util.logging.LogManager$1.run(LogManager.java:223) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.logging/java.util.logging.LogManager.<clinit>(LogManager.java:223) at java.logging/java.util.logging.Logger.demandLogger(Logger.java:648) at java.logging/java.util.logging.Logger.getLogger(Logger.java:717) at java.logging/java.util.logging.Logger.getLogger(Logger.java:701) at org.apache.activemq.artemis.boot.Artemis.<clinit>(Artemis.java:40) It seems you are migrating a previous version of an artemis instance without the new log4j configuration A new logging configuration is being created at /path/to/2.26.0/instance-upgrade/etc/log4j2.properties The older configuration file /path/to/2.26.0/instance-upgrade/etc/logging.properties should be manually removed. The old logging configuration file /path/to/2.26.0/instance-upgrade/etc/logging.properties should be removed _ _ _ / \ ____| |_ ___ __ __(_) _____ / _ \| _ \ __|/ _ \ \/ | |/ __/ / ___ \ | \/ |_/ __/ |\/| | |\___ \ /_/ \_\| \__\____|_| |_|_|/___ / Apache ActiveMQ Artemis 2.27.0-SNAPSHOT ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
