Skip to content

Commit

Permalink
chore: integrate Kotlin logging
Browse files Browse the repository at this point in the history
fixed #400
  • Loading branch information
worstell committed Oct 12, 2023
1 parent 22c577b commit c02001e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 */
Expand All @@ -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<ILoggingEvent> = 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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
<kotlin.version>1.9.0</kotlin.version>
<wire.version>4.8.1</wire.version>
<grpc.version>1.56.1</grpc.version>
<logback.version>1.4.5</logback.version>
<logback.version>1.4.8</logback.version>
<junit.version>5.10.0</junit.version>
<logstash.version>7.4</logstash.version>
</properties>

<dependencyManagement>
Expand All @@ -45,6 +46,11 @@
</dependency>

<!-- Logback dependencies -->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down

0 comments on commit c02001e

Please sign in to comment.