XeroxDucati
XeroxDucati

Reputation: 5190

Vert.X SLF4J Logging

I'm trying to get Vert.X to log via SLF4J and running into some oddness.. I started with the basic vertx-default-jul-logging.properties per the docs;

handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
java.util.logging.SimpleFormatter.format=%5$s %6$s\n
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.formatter=io.vertx.core.logging.impl.VertxLoggerFormatter

# Put the log in the system temporary directory
java.util.logging.FileHandler.pattern=%t/vertx.log

.level=INFO
io.vertx.ext.web.level=FINEST
io.vertx.level=INFO
com.hazelcast.level=INFO
io.netty.util.internal.PlatformDependent.level=SEVERE

And when I hit my endpoint, I see;

...
2019-10-16 08:04:49 DEBUG ZlibCodecFactory:39 - -Dio.netty.noJdkZlibDecoder: false
2019-10-16 08:04:49 DEBUG ZlibCodecFactory:42 - -Dio.netty.noJdkZlibEncoder: false
Router: 982634710 accepting request GET http://192.168.7.47:8080/metrics 
Route matches: Route[ path:/metrics pattern:null handlers:[MainVerticle$$Lambda$24/1221310547@70a9786e] failureHandlers:[] order:0 methods:[]]@261133359 
Metrics Request

So the Router logs, Netty logs, etc. Then, per the docs, I re-point everything to SLF4J by adding dependencies;

compile("org.slf4j:slf4j-api:$slf4jVersion")
compile("org.apache.logging.log4j:log4j-slf4j18-impl:$log4jVersion")
compile("org.apache.logging.log4j:log4j-core:$log4jVersion")

Re-pointing Vert.X and Netty;

System.setProperty("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.SLF4JLogDelegateFactory");
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);

And adding a log4j2.properties;

status = debug
name = PropertiesConfig

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = console

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

Now when I hit my endpoint;

2019-10-16 08:08:59 DEBUG ZlibCodecFactory:39 - -Dio.netty.noJdkZlibDecoder: false
2019-10-16 08:08:59 DEBUG ZlibCodecFactory:42 - -Dio.netty.noJdkZlibEncoder: false
Metrics Request

So Netty is logging, and I can see some log entries from Vert.X as well, but I no longer see my Router log lines.. Has anyone come across this? I can't figure out what I'm missing.. I've followed the docs here: https://vertx.io/docs/vertx-core/java/#_logging but somethings still off..

Upvotes: 2

Views: 5565

Answers (1)

DoctorPangloss
DoctorPangloss

Reputation: 3073

When you need a logger, get one using org.slf4j.LoggerFactory.getLogger.

Then, try calling

System.setProperty("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.SLF4JLogDelegateFactory");
LoggerFactory.initialise();

right before you call Vertx.vertx() and long before you call LoggerHandler.create(false, LoggerFormat.DEFAULT). You might be creating that logger handler before the delegate factory is set.

Finally, remove the vertx-default-jul-logging.properties file. Also, do not do InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);

Consider using logback.groovy instead of the properties approach in the future.

Upvotes: 3

Related Questions