From 0298983e9cf87c8f95643edf2bdde44da21cd3f0 Mon Sep 17 00:00:00 2001 From: Steve Millidge Date: Sun, 18 Feb 2018 20:51:31 +0000 Subject: [PATCH] PAYARA-2512 Ensure formatter added to handler correctly on reading log config (#2418) * PAYARA-2512 ensure root handlers are given the correct formatter * PAYARA-2512 stop adding ANSI color to the bracket --- .../payara/micro/impl/PayaraMicroImpl.java | 24 ++++++--- .../server/logging/ODLLogFormatter.java | 7 ++- .../server/logging/JSONLogFormatter.java | 51 ++++++++++++++----- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java index 7e32b0aeb4b..f2f5e2e8866 100644 --- a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java +++ b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java @@ -89,6 +89,8 @@ import fish.payara.nucleus.requesttracing.RequestTracingService; import java.io.FileNotFoundException; import java.io.OutputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collections; import java.util.concurrent.TimeUnit; @@ -1532,11 +1534,21 @@ private void resetLogging() { } System.setProperty("java.util.logging.config.file", runtimeDir.getLoggingProperties().getAbsolutePath()); - try { - LogManager.getLogManager().readConfiguration(); - } catch (SecurityException | IOException ex) { + try (InputStream is = new FileInputStream(runtimeDir.getLoggingProperties())){ + LogManager.getLogManager().readConfiguration(is); + + // go through all root handlers and set formatters based on properties + Logger rootLogger = LogManager.getLogManager().getLogger(""); + for (Handler handler : rootLogger.getHandlers()) { + String formatter = LogManager.getLogManager().getProperty(handler.getClass().getCanonicalName()+".formatter"); + if (formatter != null) { + handler.setFormatter((Formatter) Class.forName(formatter).newInstance()); + } + } + + } catch (SecurityException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) { LOGGER.log(Level.SEVERE, "Unable to reset the log manager", ex); - } + } } else { // system property was not set on the command line using the command option or via -D // we are likely using our default properties file so see if we need to rewrite it if (logToFile) { @@ -1567,8 +1579,8 @@ private void resetLogging() { } } System.setProperty("java.util.logging.config.file", runtimeDir.getLoggingProperties().getAbsolutePath()); - try { - LogManager.getLogManager().readConfiguration(); + try (InputStream is = new FileInputStream(runtimeDir.getLoggingProperties())){ + LogManager.getLogManager().readConfiguration(is); // reset the formatters on the two handlers //Logger rootLogger = Logger.getLogger(""); diff --git a/nucleus/core/logging/src/main/java/com/sun/enterprise/server/logging/ODLLogFormatter.java b/nucleus/core/logging/src/main/java/com/sun/enterprise/server/logging/ODLLogFormatter.java index e176544477a..eb30beb5886 100644 --- a/nucleus/core/logging/src/main/java/com/sun/enterprise/server/logging/ODLLogFormatter.java +++ b/nucleus/core/logging/src/main/java/com/sun/enterprise/server/logging/ODLLogFormatter.java @@ -221,17 +221,16 @@ private String odlLogFormat(LogRecord record) { // Adding messageType Level logLevel = record.getLevel(); + recordBuffer.append(FIELD_BEGIN_MARKER); if (color()) { recordBuffer.append(getColor(logLevel)); - } - recordBuffer.append(FIELD_BEGIN_MARKER); - String odlLevel = logLevel.getLocalizedName(); + } String odlLevel = logLevel.getLocalizedName(); logEvent.setLevel(odlLevel); recordBuffer.append(odlLevel); - recordBuffer.append(FIELD_END_MARKER); if (color()) { recordBuffer.append(getReset()); } + recordBuffer.append(FIELD_END_MARKER); recordBuffer.append(getRecordFieldSeparator() != null ? getRecordFieldSeparator() : FIELD_SEPARATOR); // Adding message ID diff --git a/nucleus/core/logging/src/main/java/fish/payara/enterprise/server/logging/JSONLogFormatter.java b/nucleus/core/logging/src/main/java/fish/payara/enterprise/server/logging/JSONLogFormatter.java index 7d7d3f8e6ce..b95c2630708 100644 --- a/nucleus/core/logging/src/main/java/fish/payara/enterprise/server/logging/JSONLogFormatter.java +++ b/nucleus/core/logging/src/main/java/fish/payara/enterprise/server/logging/JSONLogFormatter.java @@ -1,20 +1,45 @@ /* -DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -Copyright (c) 2016 Payara Foundation. All rights reserved. -The contents of this file are subject to the terms of the Common Development -and Distribution License("CDDL") (collectively, the "License"). You -may not use this file except in compliance with the License. You can -obtain a copy of the License at -https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html -or packager/legal/LICENSE.txt. See the License for the specific -language governing permissions and limitations under the License. -When distributing the software, include this License Header Notice in each -file and include the License file at packager/legal/LICENSE.txt. -*/ + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2017-2018 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ package fish.payara.enterprise.server.logging; import com.sun.common.util.logging.GFLogRecord; -import com.sun.enterprise.server.logging.AnsiColorFormatter; import com.sun.enterprise.server.logging.ExcludeFieldsSupport; import com.sun.enterprise.server.logging.FormatterDelegate; import com.sun.enterprise.server.logging.LogEvent;