-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
<appender name="basic" class="io.initialcapacity.logging.BasicJSONServiceProvider"/> | ||
<root level="info"> | ||
<appender-ref ref="basic"/> | ||
</root> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
plugins { | ||
id("org.jetbrains.kotlin.plugin.serialization") | ||
} | ||
|
||
dependencies { | ||
implementation("org.slf4j:slf4j-api:2.0.7") | ||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") | ||
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0-RC.2") | ||
} |
111 changes: 111 additions & 0 deletions
111
support/logging-support/src/main/kotlin/io/initialcapacity/logging/BasicJSONLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package io.initialcapacity.logging | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.slf4j.Marker | ||
import org.slf4j.event.Level | ||
import org.slf4j.helpers.LegacyAbstractLogger | ||
import org.slf4j.helpers.MessageFormatter | ||
import org.slf4j.spi.LocationAwareLogger | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.time.format.DateTimeFormatter | ||
|
||
|
||
class BasicJSONLogger(val desiredName: String) : LegacyAbstractLogger() { | ||
init { | ||
this.name = desiredName | ||
} | ||
|
||
override fun isTraceEnabled(): Boolean { | ||
return isLevelEnabled(LocationAwareLogger.TRACE_INT); | ||
} | ||
|
||
override fun isDebugEnabled(): Boolean { | ||
return isLevelEnabled(LocationAwareLogger.DEBUG_INT); | ||
} | ||
|
||
override fun isInfoEnabled(): Boolean { | ||
return isLevelEnabled(LocationAwareLogger.INFO_INT); | ||
} | ||
|
||
override fun isWarnEnabled(): Boolean { | ||
return isLevelEnabled(LocationAwareLogger.WARN_INT); | ||
} | ||
|
||
override fun isErrorEnabled(): Boolean { | ||
return isLevelEnabled(LocationAwareLogger.ERROR_INT); | ||
} | ||
|
||
override fun getFullyQualifiedCallerName(): String? { | ||
return null | ||
} | ||
|
||
@Serializable | ||
data class Entry( | ||
val time: String, | ||
val threadName: String, | ||
val severity: String, | ||
val loggerName: String, | ||
val message: String | ||
) | ||
|
||
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ") | ||
|
||
@Synchronized | ||
override fun handleNormalizedLoggingCall( | ||
level: Level?, | ||
marker: Marker?, | ||
messagePattern: String?, | ||
arguments: Array<out Any>?, | ||
throwable: Throwable? | ||
) { | ||
|
||
val time = Instant.now().atZone(ZoneId.of("UTC")).format(formatter) | ||
val threadName = Thread.currentThread().name | ||
val severity = renderLevel(level!!.toInt()) | ||
val loggerName = name | ||
val message = MessageFormatter.basicArrayFormat(messagePattern, arguments) | ||
|
||
// %r [%t] %level %logger - %m%n | ||
// val builder = StringBuilder(32) | ||
// builder.append(time); | ||
// builder.append(" ") | ||
// builder.append("[") | ||
// builder.append(threadName) | ||
// builder.append("] ") | ||
// builder.append(levelString) | ||
// builder.append(" ") | ||
// builder.append(loggetName).append(" - "); | ||
// builder.append(message); | ||
// val entry = builder.toString() | ||
|
||
val entry = Entry(time, threadName, severity, loggerName, message) | ||
val encodeToString = Json.encodeToString(entry) | ||
|
||
System.err.println(encodeToString) | ||
throwable?.printStackTrace() | ||
System.err.flush() | ||
} | ||
|
||
private fun isLevelEnabled(level: Int): Boolean { | ||
return (level >= LocationAwareLogger.INFO_INT) | ||
} | ||
|
||
private fun renderLevel(level: Int): String { | ||
when (level) { | ||
LocationAwareLogger.TRACE_INT -> return "TRACE" | ||
LocationAwareLogger.DEBUG_INT -> return "DEBUG" | ||
LocationAwareLogger.INFO_INT -> return "INFO" | ||
LocationAwareLogger.WARN_INT -> return "WARN" | ||
LocationAwareLogger.ERROR_INT -> return "ERROR" | ||
} | ||
throw IllegalStateException("Unrecognized level [$level]") | ||
} | ||
|
||
private val json = Json { | ||
isLenient = true | ||
ignoreUnknownKeys = true | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
support/logging-support/src/main/kotlin/io/initialcapacity/logging/BasicJSONLoggerFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.initialcapacity.logging | ||
|
||
import org.slf4j.ILoggerFactory | ||
import org.slf4j.Logger | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
|
||
class BasicJSONLoggerFactory : ILoggerFactory { | ||
private var loggerMap = ConcurrentHashMap<String, Logger>(); | ||
|
||
override fun getLogger(name: String): Logger { | ||
return loggerMap.computeIfAbsent(name) { | ||
BasicJSONLogger(it) | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...rt/logging-support/src/main/kotlin/io/initialcapacity/logging/BasicJSONServiceProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.initialcapacity.logging | ||
|
||
import org.slf4j.ILoggerFactory | ||
import org.slf4j.IMarkerFactory | ||
import org.slf4j.helpers.BasicMarkerFactory | ||
import org.slf4j.helpers.NOPMDCAdapter | ||
import org.slf4j.spi.MDCAdapter | ||
import org.slf4j.spi.SLF4JServiceProvider | ||
|
||
|
||
class BasicJSONServiceProvider : SLF4JServiceProvider { | ||
var REQUESTED_API_VERSION: String = "2.0.99" | ||
|
||
private var loggerFactory = BasicJSONLoggerFactory() | ||
private var markerFactory = BasicMarkerFactory() | ||
private var mdcAdapter = NOPMDCAdapter() | ||
|
||
override fun getLoggerFactory(): ILoggerFactory { | ||
return loggerFactory | ||
} | ||
|
||
override fun getMarkerFactory(): IMarkerFactory { | ||
return markerFactory | ||
} | ||
|
||
override fun getMDCAdapter(): MDCAdapter { | ||
return mdcAdapter | ||
} | ||
|
||
override fun getRequestedApiVersion(): String { | ||
return REQUESTED_API_VERSION | ||
} | ||
|
||
override fun initialize() { | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...t/logging-support/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.initialcapacity.logging.BasicJSONServiceProvider |
13 changes: 13 additions & 0 deletions
13
...ging-support/src/test/kotlin/test/initialcapacity/logging/BasicJSONServiceProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package test.initialcapacity.logging | ||
|
||
import org.slf4j.LoggerFactory | ||
import kotlin.test.Test | ||
|
||
class BasicJSONServiceProviderTest { | ||
|
||
@Test | ||
fun test1() { | ||
val logger = LoggerFactory.getLogger("test1") | ||
logger.info("Hello world.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<configuration> | ||
<appender name="basic" class="io.initialcapacity.logging.BasicJSONServiceProvider"/> | ||
<root level="info"> | ||
<appender-ref ref="basic"/> | ||
</root> | ||
</configuration> |