diff --git a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/logging/Logging.kt b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/logging/Logging.kt index 940bd290f4..966a9e41bb 100644 --- a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/logging/Logging.kt +++ b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/logging/Logging.kt @@ -3,9 +3,15 @@ package xyz.block.ftl.logging import ch.qos.logback.classic.Level import ch.qos.logback.classic.Logger import ch.qos.logback.classic.LoggerContext -import ch.qos.logback.classic.encoder.PatternLayoutEncoder import ch.qos.logback.classic.spi.ILoggingEvent import ch.qos.logback.core.ConsoleAppender +import ch.qos.logback.core.joran.spi.ConsoleTarget +import com.fasterxml.jackson.core.JsonGenerator +import net.logstash.logback.composite.JsonProviders +import net.logstash.logback.composite.JsonWritingUtils +import net.logstash.logback.composite.loggingevent.LogLevelJsonProvider +import net.logstash.logback.composite.loggingevent.MessageJsonProvider +import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder import org.slf4j.LoggerFactory import kotlin.reflect.KClass @@ -15,9 +21,10 @@ class Logging { companion object { private val logging = Logging() + private const val DEFAULT_LOG_LEVEL = "info" fun logger(name: String): Logger { - val logger = LoggerFactory.getLogger(name) as Logger + val logger = logging.lc.getLogger(name) as Logger logger.addAppender(logging.appender) logger.level = Level.DEBUG logger.isAdditive = false /* set to true if root should log too */ @@ -29,18 +36,30 @@ class Logging { return logger(kClass.qualifiedName!!) } - fun init() { - val ple = PatternLayoutEncoder() + init { + val je = LoggingEventCompositeJsonEncoder() + je.context = logging.lc - ple.pattern = "%date %level %logger{10} - %msg%n" - ple.context = logging.lc - ple.start() + val providers: JsonProviders = je.providers + providers.setContext(je.context) + // Custom LogLevelJsonProvider converts level value to lowercase + providers.addProvider(object : LogLevelJsonProvider() { + override fun writeTo(generator: JsonGenerator, event: ILoggingEvent) { + JsonWritingUtils.writeStringField(generator, fieldName, event.level.toString().lowercase()) + } + }) + providers.addProvider(MessageJsonProvider()) + je.providers = providers + je.start() - logging.appender.encoder = ple + logging.appender.target = ConsoleTarget.SystemErr.toString() logging.appender.context = logging.lc + logging.appender.encoder = je logging.appender.start() - logger(Logger.ROOT_LOGGER_NAME).level = Level.INFO + val rootLogger = logger(Logger.ROOT_LOGGER_NAME) + val rootLevelCfg = Level.valueOf(System.getenv("LOG_LEVEL") ?: DEFAULT_LOG_LEVEL) + rootLogger.level = rootLevelCfg } } } diff --git a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/main/main.kt b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/main/main.kt index d4551fb6ba..95c114ec74 100644 --- a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/main/main.kt +++ b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/main/main.kt @@ -1,11 +1,9 @@ package xyz.block.ftl.main -import ch.qos.logback.classic.Level import io.grpc.ServerInterceptors import io.grpc.netty.NettyServerBuilder import xyz.block.ftl.client.GrpcVerbServiceClient import xyz.block.ftl.client.makeGrpcClient -import xyz.block.ftl.logging.Logging import xyz.block.ftl.registry.Registry import xyz.block.ftl.server.Server import xyz.block.ftl.server.ServerInterceptor @@ -15,7 +13,6 @@ import java.net.URL val defaultBindAddress = "http://127.0.0.1:8896" val defaultFtlEndpoint = "http://127.0.0.1:8892" -private const val grpcServerLoggerName = "io.netty" fun main() { val bind = URL(System.getenv("FTL_BIND") ?: defaultBindAddress) @@ -29,9 +26,6 @@ fun main() { val grpcClient = VerbServiceBlockingStub(makeGrpcClient(ftlEndpoint)) val verbRoutingClient = GrpcVerbServiceClient(grpcClient) val server = Server(registry, verbRoutingClient) - - // suppress logs below INFO level on GRPC server - Logging.logger(grpcServerLoggerName).level = Level.INFO val grpcServer = NettyServerBuilder.forAddress(addr) .addService(ServerInterceptors.intercept(server, ServerInterceptor())) .build() diff --git a/pom.xml b/pom.xml index d47161e4e4..437fc5d5f6 100644 --- a/pom.xml +++ b/pom.xml @@ -21,8 +21,9 @@ 1.9.0 4.8.1 1.56.1 - 1.4.5 + 1.4.8 5.10.0 + 7.4 @@ -45,6 +46,11 @@ + + net.logstash.logback + logstash-logback-encoder + ${logstash.version} + ch.qos.logback logback-classic