BAEL-1767 - Java 9 Platform Logging API (#5071)

* java 9 logger

* slf4j + logback modules added

* new module for slf4j

* logback config

* cleanup

* format
This commit is contained in:
Marcos Lopez Gonzalez 2018-09-14 15:54:38 +02:00 committed by Grzegorz Piwowarek
parent d5e6a8d436
commit 8c20c665de
10 changed files with 200 additions and 0 deletions

15
core-java-9/logging.sh Executable file
View File

@ -0,0 +1,15 @@
# compile logging module
# javac --module-path mods -d mods/com.baeldung.logging src/modules/com.baeldung.logging/module-info.java src/modules/com.baeldung.logging/com/baeldung/logging/*.java
# compile logging slf4j module
javac --module-path mods -d mods/com.baeldung.logging.slf4j src/modules/com.baeldung.logging.slf4j/module-info.java src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/*.java
# compile logging main app module
javac --module-path mods -d mods/com.baeldung.logging.app src/modules/com.baeldung.logging.app/module-info.java src/modules/com.baeldung.logging.app/com/baeldung/logging/app/*.java
# run logging main app
# java --module-path mods -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp
# run looging main app using logback
java --module-path mods -Dlogback.configurationFile=mods/logback.xml -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -- %msg%n
</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -0,0 +1,13 @@
package com.baeldung.logging.app;
import static java.lang.System.Logger.*;
public class MainApp {
private static System.Logger LOGGER = System.getLogger("MainApp");
public static void main(String[] args) {
LOGGER.log(Level.ERROR, "error test");
LOGGER.log(Level.INFO, "info test");
}
}

View File

@ -0,0 +1,2 @@
module com.baeldung.logging.app {
}

View File

@ -0,0 +1,99 @@
package com.baeldung.logging.slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ResourceBundle;
public class Slf4jLogger implements System.Logger {
private final String name;
private final Logger logger;
public Slf4jLogger(String name) {
this.name = name;
logger = LoggerFactory.getLogger(name);
}
@Override
public String getName() {
return name;
}
@Override
public boolean isLoggable(Level level) {
switch (level) {
case OFF:
return false;
case TRACE:
return logger.isTraceEnabled();
case DEBUG:
return logger.isDebugEnabled();
case INFO:
return logger.isInfoEnabled();
case WARNING:
return logger.isWarnEnabled();
case ERROR:
return logger.isErrorEnabled();
case ALL:
default:
return true;
}
}
@Override
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
if (!isLoggable(level)) {
return;
}
switch (level) {
case TRACE:
logger.trace(msg, thrown);
break;
case DEBUG:
logger.debug(msg, thrown);
break;
case INFO:
logger.info(msg, thrown);
break;
case WARNING:
logger.warn(msg, thrown);
break;
case ERROR:
logger.error(msg, thrown);
break;
case ALL:
default:
logger.info(msg, thrown);
}
}
@Override
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
if (!isLoggable(level)) {
return;
}
switch (level) {
case TRACE:
logger.trace(format, params);
break;
case DEBUG:
logger.debug(format, params);
break;
case INFO:
logger.info(format, params);
break;
case WARNING:
logger.warn(format, params);
break;
case ERROR:
logger.error(format, params);
break;
case ALL:
default:
logger.info(format, params);
}
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.logging.slf4j;
public class Slf4jLoggerFinder extends System.LoggerFinder {
@Override
public System.Logger getLogger(String name, Module module) {
return new Slf4jLogger(name);
}
}

View File

@ -0,0 +1,6 @@
module com.baeldung.logging.slf4j {
requires org.slf4j;
provides java.lang.System.LoggerFinder
with com.baeldung.logging.slf4j.Slf4jLoggerFinder;
exports com.baeldung.logging.slf4j;
}

View File

@ -0,0 +1,27 @@
package com.baeldung.logging;
import java.text.MessageFormat;
import java.util.ResourceBundle;
public class ConsoleLogger implements System.Logger {
@Override
public String getName() {
return "ConsoleLogger";
}
@Override
public boolean isLoggable(Level level) {
return true;
}
@Override
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
System.out.printf("ConsoleLogger [%s]: %s - %s%n", level, msg, thrown);
}
@Override
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
System.out.printf("ConsoleLogger [%s]: %s%n", level, MessageFormat.format(format, params));
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.logging;
public class CustomLoggerFinder extends System.LoggerFinder {
@Override
public System.Logger getLogger(String name, Module module) {
return new ConsoleLogger();
}
}

View File

@ -0,0 +1,5 @@
module com.baeldung.logging {
provides java.lang.System.LoggerFinder
with com.baeldung.logging.CustomLoggerFinder;
exports com.baeldung.logging;
}