ongoing work on the slf4j-jdk-platform-logging

This commit is contained in:
Ceki Gulcu 2021-08-12 13:05:04 +02:00
parent a42ad1f4f7
commit eaa9fae39d
6 changed files with 175 additions and 5 deletions

View File

@ -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");

View File

@ -62,10 +62,22 @@
<release>9</release>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<reportFormat>plain</reportFormat>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -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 {

View File

@ -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&uuml;lc&uuml;
*
*/
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<String> 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<String> 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()));
}
}

View File

@ -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<String> stringList = Collections.synchronizedList(new ArrayList<String>());
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());
}
}

View File

@ -36,6 +36,18 @@
file names in <em/>
class names in <code/>
-->
<hr noshade="noshade" size="1"/>
<h3>th of August, 2021 - Release of SLF4J 2.0.0-alpha4</h3>
<p>Added support for the <a
href="https://openjdk.java.net/jeps/264">Java Platform Logging API
(JEP 264)</a> in the new <em>slf4j-jdk-platform-logging</em>
module. JEP 264 was added in Java 9. Many thanks to Nicolai Parlog
for providing the relevant PR.
</p>
<hr noshade="noshade" size="1"/>
<h3>10th of August, 2021 - Release of SLF4J 2.0.0-alpha3</h3>