From 8c924b4c26ef385614a5784b1684f35729e71dcc Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Sun, 21 Mar 2021 15:35:27 +0300 Subject: [PATCH 1/2] Bug 572145 - [Passage][Floating] extend logging plug logging programmatically Signed-off-by: eparovyshnaya --- .../META-INF/MANIFEST.MF | 1 - .../META-INF/MANIFEST.MF | 1 + .../build.properties | 4 +- .../config}/log4j2.xml | 6 +-- .../lbc/internal/jetty/JettyActivator.java | 23 ++++++++-- .../META-INF/MANIFEST.MF | 4 +- .../lic/internal/base/logging/Logging.java | 46 +++++++++++++++++++ .../lic/internal/jetty/JettyServer.java | 28 +++++------ .../org.eclipse.passage.lbc.server.product | 2 +- 9 files changed, 92 insertions(+), 23 deletions(-) rename bundles/{org.eclipse.passage.lbc.base/src => org.eclipse.passage.lbc.jetty/config}/log4j2.xml (70%) create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/logging/Logging.java diff --git a/bundles/org.eclipse.passage.lbc.base/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lbc.base/META-INF/MANIFEST.MF index d2bb28109..a507be052 100644 --- a/bundles/org.eclipse.passage.lbc.base/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lbc.base/META-INF/MANIFEST.MF @@ -7,7 +7,6 @@ Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright Bundle-RequiredExecutionEnvironment: JavaSE-1.8 -Bundle-ClassPath: . Require-Bundle: javax.servlet;bundle-version="0.0.0", org.eclipse.osgi;bundle-version="0.0.0", org.eclipse.passage.lic.base;bundle-version="0.0.0", diff --git a/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF index 7c23de55b..a811f99c1 100644 --- a/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF @@ -7,6 +7,7 @@ Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Bundle-ClassPath: . Require-Bundle: org.eclipse.osgi.services;bundle-version="0.0.0", org.eclipse.passage.lbc.base;bundle-version="1.1.1", org.eclipse.passage.lic.net;bundle-version="1.0.100", diff --git a/bundles/org.eclipse.passage.lbc.jetty/build.properties b/bundles/org.eclipse.passage.lbc.jetty/build.properties index 341338601..4fb74c6c4 100644 --- a/bundles/org.eclipse.passage.lbc.jetty/build.properties +++ b/bundles/org.eclipse.passage.lbc.jetty/build.properties @@ -16,4 +16,6 @@ output.. = bin/ bin.includes = META-INF/,\ .,\ OSGI-INF/,\ - about.html + about.html,\ + src/log4j2.xml,\ + config/ diff --git a/bundles/org.eclipse.passage.lbc.base/src/log4j2.xml b/bundles/org.eclipse.passage.lbc.jetty/config/log4j2.xml similarity index 70% rename from bundles/org.eclipse.passage.lbc.base/src/log4j2.xml rename to bundles/org.eclipse.passage.lbc.jetty/config/log4j2.xml index f746d0f4b..5e1e46eea 100644 --- a/bundles/org.eclipse.passage.lbc.base/src/log4j2.xml +++ b/bundles/org.eclipse.passage.lbc.jetty/config/log4j2.xml @@ -1,6 +1,6 @@ + packages="org.eclipse.passage.lbc.internal.base,org.eclipse.passage.lbc.internal.jetty,org.eclipse.passage.lic.internal.jetty,org.eclipse.passage.lic.internal.net"> @@ -15,9 +15,9 @@ - %d{HH:mm:ss} %p %c{1.} [%t]: %m%n + %d{HH:mm:ss} %p %c{3.} [%t] %m%n - + diff --git a/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyActivator.java b/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyActivator.java index cc242e49a..3d99e0c9f 100644 --- a/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyActivator.java +++ b/bundles/org.eclipse.passage.lbc.jetty/src/org/eclipse/passage/lbc/internal/jetty/JettyActivator.java @@ -12,23 +12,30 @@ *******************************************************************************/ package org.eclipse.passage.lbc.internal.jetty; +import java.nio.file.Path; + +import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Platform; -import org.eclipse.passage.lbc.internal.base.FlotingRequestHandled; import org.eclipse.passage.lbc.internal.base.EagerFloatingState; +import org.eclipse.passage.lbc.internal.base.FlotingRequestHandled; import org.eclipse.passage.lbc.internal.base.api.FloatingState; +import org.eclipse.passage.lic.internal.base.logging.Logging; import org.eclipse.passage.lic.internal.jetty.JettyHandler; import org.eclipse.passage.lic.internal.jetty.JettyServer; import org.eclipse.passage.lic.internal.net.connect.Port; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; public class JettyActivator implements BundleActivator { private final JettyServer jetty; - private final FloatingState state = new EagerFloatingState(); + private final FloatingState state; public JettyActivator() { - jetty = new JettyServer(this::handler); + configureLogging(); + this.jetty = new JettyServer(this::handler); + this.state = new EagerFloatingState(); } @Override @@ -45,4 +52,14 @@ private JettyHandler handler() { return new JettyHandler(request -> new FlotingRequestHandled(new StatedRequest(request, state)).get()); } + private void configureLogging() { + new Logging(this::logConfig).configure(); + } + + private Path logConfig() throws Exception { + return FileLocator.getBundleFile(FrameworkUtil.getBundle(getClass())).toPath()// + .resolve("config") //$NON-NLS-1$ + .resolve("log4j2.xml"); //$NON-NLS-1$ + } + } diff --git a/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF index 5fb27e250..348948649 100644 --- a/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF @@ -7,7 +7,8 @@ Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright Bundle-RequiredExecutionEnvironment: JavaSE-1.8 -Require-Bundle: org.eclipse.passage.lic.api;bundle-version="1.0.0";visibility:=reexport +Require-Bundle: org.eclipse.passage.lic.api;bundle-version="1.0.0";visibility:=reexport, + org.apache.logging.log4j;bundle-version="2.8.2" Export-Package: org.eclipse.passage.lic.internal.base; x-friends:="org.eclipse.passage.lbc.base, org.eclipse.passage.lic.base.tests, @@ -76,6 +77,7 @@ Export-Package: org.eclipse.passage.lic.internal.base; org.eclipse.passage.lic.net, org.eclipse.passage.loc.products.core, org.eclipse.passage.loc.products.emfforms", + org.eclipse.passage.lic.internal.base.logging;x-friends:="org.eclipse.passage.lbc.jetty,org.eclipse.passage.lac.jetty", org.eclipse.passage.lic.internal.base.observatory;x-internal:=true, org.eclipse.passage.lic.internal.base.registry;x-friends:="org.eclipse.passage.lbc.base,org.eclipse.passage.seal.demo.tests,org.eclipse.passage.lic.hc.tests", org.eclipse.passage.lic.internal.base.requirements;x-friends:="org.eclipse.passage.lic.equinox,org.eclipse.passage.lic.json", diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/logging/Logging.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/logging/Logging.java new file mode 100644 index 000000000..120df92bd --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/logging/Logging.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2021 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.logging; + +import java.io.FileInputStream; +import java.nio.file.Path; + +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.Configurator; + +public final class Logging { + + private final ConfigSupplier config; + + public Logging(ConfigSupplier config) { + this.config = config; + } + + @SuppressWarnings("resource") + public void configure() { + try { + ConfigurationSource source = new ConfigurationSource(new FileInputStream(config.get().toFile())); + Configurator.initialize(getClass().getClassLoader().getParent(), source); + } catch (Exception e) { + System.err.println("Failed to configure logging"); //$NON-NLS-1$ + e.printStackTrace(); + } + } + + public static interface ConfigSupplier { + + Path get() throws Exception; + + } + +} diff --git a/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyServer.java b/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyServer.java index fae66d52b..217085837 100644 --- a/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyServer.java +++ b/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyServer.java @@ -15,15 +15,15 @@ import java.util.Objects; import java.util.function.Supplier; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; import org.eclipse.passage.lic.internal.jetty.i18n.Messages; import org.eclipse.passage.lic.internal.net.connect.Port; public final class JettyServer { - private final Logger logger = Log.getLogger(JettyServer.class); + private final Logger log = LogManager.getLogger(getClass()); private final Supplier handler; private Server server; @@ -37,23 +37,25 @@ public void launch(Port port) throws JettyException { server = new Server(port.get().get()); server.setHandler(handler.get()); server.start(); - logger.info(String.format(Messages.started, port.get())); - } catch (Exception exception) { - throw new JettyException(// - String.format(Messages.error_onstart, exception.getClass(), exception.getMessage()), // - exception); + log.info(String.format(Messages.started, port.get().get())); + } catch (Exception e) { + logAndRethrow(e, Messages.error_onstart); } } public void terminate() throws JettyException { try { server.stop(); - logger.info(String.format(Messages.stopped)); - } catch (Exception exception) { - throw new JettyException(// - String.format(Messages.error_onstop, exception.getClass(), exception.getMessage()), // - exception); + log.info(String.format(Messages.stopped)); + } catch (Exception e) { + logAndRethrow(e, Messages.error_onstop); } } + private void logAndRethrow(Exception e, String template) throws JettyException { + String message = String.format(template, e.getClass(), e.getMessage()); + log.error(message, e); + throw new JettyException(message, e); + } + } diff --git a/products/org.eclipse.passage.lbc.server.product/org.eclipse.passage.lbc.server.product b/products/org.eclipse.passage.lbc.server.product/org.eclipse.passage.lbc.server.product index b5888eb08..a977dcde4 100644 --- a/products/org.eclipse.passage.lbc.server.product/org.eclipse.passage.lbc.server.product +++ b/products/org.eclipse.passage.lbc.server.product/org.eclipse.passage.lbc.server.product @@ -19,7 +19,7 @@ - -os ${target.os} -ws ${target.ws} -arch ${target.arch} -consoleLog -console + -os ${target.os} -ws ${target.ws} -arch ${target.arch} -consoleLog -console -server.port=8090 -Declipse.ignoreApp=true -Dosgi.noShutdown=true From 88c21f44a5ec1f5abbb1cf6de75fbdfe4941424c Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Sun, 21 Mar 2021 15:40:28 +0300 Subject: [PATCH 2/2] Bug 572145 - [Passage][Floating] extend logging plug logging programmatically Signed-off-by: eparovyshnaya --- bundles/org.eclipse.passage.lbc.jetty/build.properties | 1 - 1 file changed, 1 deletion(-) diff --git a/bundles/org.eclipse.passage.lbc.jetty/build.properties b/bundles/org.eclipse.passage.lbc.jetty/build.properties index 4fb74c6c4..c537c7dfa 100644 --- a/bundles/org.eclipse.passage.lbc.jetty/build.properties +++ b/bundles/org.eclipse.passage.lbc.jetty/build.properties @@ -17,5 +17,4 @@ bin.includes = META-INF/,\ .,\ OSGI-INF/,\ about.html,\ - src/log4j2.xml,\ config/