From eaa9fae39d6d33521e8232c3f4f439d7db3c3532 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Thu, 12 Aug 2021 13:05:04 +0200 Subject: [PATCH] ongoing work on the slf4j-jdk-platform-logging --- .../org/slf4j/helpers/StringPrintStream.java | 9 +++ slf4j-jdk-platform-logging/pom.xml | 14 +++- .../platform/logging/SLF4JPlarformLogger.java | 2 +- .../logging/SLF4JPlatformLoggingTest.java | 76 ++++++++++++++++++- .../platform/logging/StringPrintStream.java | 67 ++++++++++++++++ slf4j-site/src/site/pages/news.html | 12 +++ 6 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/StringPrintStream.java diff --git a/slf4j-api/src/test/java/org/slf4j/helpers/StringPrintStream.java b/slf4j-api/src/test/java/org/slf4j/helpers/StringPrintStream.java index 68df0548..3a1cc234 100755 --- a/slf4j-api/src/test/java/org/slf4j/helpers/StringPrintStream.java +++ b/slf4j-api/src/test/java/org/slf4j/helpers/StringPrintStream.java @@ -29,6 +29,15 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +/** + * + * Copied from org.slfj.helpers. + * + * Currently it is not possible to use test-jar from tests running on the module-path. + * + * @author ceki + * + */ public class StringPrintStream extends PrintStream { public static final String LINE_SEP = System.getProperty("line.separator"); diff --git a/slf4j-jdk-platform-logging/pom.xml b/slf4j-jdk-platform-logging/pom.xml index 2f568ca5..9bfbd95f 100644 --- a/slf4j-jdk-platform-logging/pom.xml +++ b/slf4j-jdk-platform-logging/pom.xml @@ -62,10 +62,22 @@ 9 - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + 1 + false + plain + false + + + + diff --git a/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlarformLogger.java b/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlarformLogger.java index 833bba20..dd5a4e1f 100644 --- a/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlarformLogger.java +++ b/slf4j-jdk-platform-logging/src/main/java/org/slf4j/jdk/platform/logging/SLF4JPlarformLogger.java @@ -26,7 +26,6 @@ package org.slf4j.jdk.platform.logging; import static java.util.Objects.requireNonNull; -import java.text.MessageFormat; import java.util.MissingResourceException; import java.util.ResourceBundle; @@ -35,6 +34,7 @@ import org.slf4j.spi.LoggingEventBuilder; /** * Adapts {@link Logger} to {@link System.Logger}. + * @since 2.0.0 */ class SLF4JPlarformLogger implements System.Logger { diff --git a/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLoggingTest.java b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLoggingTest.java index d7083568..1cd9a120 100644 --- a/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLoggingTest.java +++ b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/SLF4JPlatformLoggingTest.java @@ -24,21 +24,91 @@ */ package org.slf4j.jdk.platform.logging; -import java.lang.System.Logger; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.io.PrintStream; +import java.lang.System.Logger; import java.lang.System.Logger.Level; import java.lang.System.LoggerFinder; +import java.util.List; +import java.util.Random; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; +/** + * The present test is fragile in the sense that it sets up SimpleLogger + * with a StringPrintStream and reverts to the old stream when done. + * + * Any tests running simultaneously (and using SimpleLogger) will be affected + * by this. Moreover, since SimpleLogger is initialized by the call to LoggerFactory + * and tests also using LoggerFactory will also be affected. + * + * @author Ceki Gülcü + * + */ public class SLF4JPlatformLoggingTest { + static final String PREFIX = "org.slf4j.simpleLogger."; + static final String SIMPLE_LOGGER_FILE_PROPERTY = PREFIX + "logFile"; + static final String SIMPLE_LOGGER_THREAD_NAME_PROPERTY = PREFIX + "showThreadName"; + + static int diff = new Random().nextInt(100*1000*1000); + + static final PrintStream oldErr = System.err; + static StringPrintStream SPS = new StringPrintStream(oldErr, false); + + @BeforeClass + static public void beforeClass() throws Exception { + System.setErr(SPS); + //System.setProperty(SIMPLE_LOGGER_FILE_PROPERTY, targetFile); + System.setProperty(SIMPLE_LOGGER_THREAD_NAME_PROPERTY, "false"); + } + + @AfterClass + static public void afterClass() { + System.setErr(oldErr); + System.clearProperty(SIMPLE_LOGGER_THREAD_NAME_PROPERTY); + } + + @After + public void tearDown() { + SPS.stringList.clear(); + } @Test - public void smoke() { + public void smoke() throws IOException { LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); - Logger systemLogger = finder.getLogger("x", null); + Logger systemLogger = finder.getLogger("smoke", null); systemLogger.log(Level.INFO, "hello"); systemLogger.log(Level.INFO, "hello %s", "world"); + + List results = SPS.stringList; + assertEquals(2, results.size()); + assertEquals("INFO smoke - hello", results.get(0)); + assertEquals("INFO smoke - hello world", results.get(1)); } + + @Test + public void throwTest() throws IOException { + LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); + Logger systemLogger = finder.getLogger("throwTest", null); + systemLogger.log(Level.INFO, "we have a problem", new Exception()); + + List results = SPS.stringList; + //INFO throwTest - a problem + //java.lang.Exception + // at org.slf4j.jdk.platform.logging/org.slf4j.jdk.platform.logging.SLF4JPlatformLoggingTest.throwTest(SLF4JPlatformLoggingTest.java:92) + + assertEquals("INFO throwTest - we have a problem", results.get(0)); + assertEquals(Exception.class.getName(), results.get(1)); + assertTrue(results.get(2).contains("at ")); + assertTrue(results.get(2).contains(this.getClass().getName())); + } + + } diff --git a/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/StringPrintStream.java b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/StringPrintStream.java new file mode 100644 index 00000000..56d42c8f --- /dev/null +++ b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/StringPrintStream.java @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2004-2021 QOS.ch + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.slf4j.jdk.platform.logging; + +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class StringPrintStream extends PrintStream { + + public static final String LINE_SEP = System.getProperty("line.separator"); + PrintStream other; + boolean duplicate = false; + + public List stringList = Collections.synchronizedList(new ArrayList()); + + public StringPrintStream(PrintStream ps, boolean duplicate) { + super(ps); + other = ps; + this.duplicate = duplicate; + } + + public StringPrintStream(PrintStream ps) { + this(ps, false); + } + + public void print(String s) { + if (duplicate) + other.print(s); + stringList.add(s); + } + + public void println(String s) { + if (duplicate) + other.println(s); + stringList.add(s); + } + + public void println(Object o) { + if (duplicate) + other.println(o); + stringList.add(o.toString()); + } +} \ No newline at end of file diff --git a/slf4j-site/src/site/pages/news.html b/slf4j-site/src/site/pages/news.html index ae2a0d67..930cd62c 100755 --- a/slf4j-site/src/site/pages/news.html +++ b/slf4j-site/src/site/pages/news.html @@ -36,6 +36,18 @@ file names in class names in --> + +
+ +

th of August, 2021 - Release of SLF4J 2.0.0-alpha4

+ +

Added support for the Java Platform Logging API + (JEP 264) in the new slf4j-jdk-platform-logging + module. JEP 264 was added in Java 9. Many thanks to Nicolai Parlog + for providing the relevant PR. +

+

10th of August, 2021 - Release of SLF4J 2.0.0-alpha3