Configuration
Colotok output the log where you specified by the provider and formatted with you passed.
Get Logger
val logger = LoggerFactory()
.addProvider(ConsoleProvider())
.getLogger()
you can get the logger instance by LoggerFactory.getLogger()
ConsoleProvider
is a builtin provider which used for print the log into console
Print log
Text Logging
use bellow methods for appropriately level
logger.trace("message what happen")
logger.debug("message what happen")
logger.info("message what happen")
logger.warn("message what happen")
logger.error("message what happen")
// 2023-12-29T12:21:13.354328+09:00 (main)[TRACE] - message what happen, additional = {}
// 2023-12-29T12:21:13.354328+09:00 (main)[DEBUG] - message what happen, additional = {}
// 2023-12-29T12:21:13.354328+09:00 (main)[INFO] - message what happen, additional = {}
// 2023-12-29T12:21:13.354328+09:00 (main)[WARN] - message what happen, additional = {}
// 2023-12-29T12:21:13.354328+09:00 (main)[ERROR] - message what happen, additional = {}
or
logger.atInfo {
print("in this block")
print("all of logs are printed out with INFO level")
}
// 2023-12-29T12:21:13.354328+09:00 (main)[INFO] - in this block, additional = {}
// 2023-12-29T12:21:13.356133+09:00 (main)[INFO] - all of logs are printed out with INFO level, additional = {}
Structured Logging
Colotok also can print structured log using kotlinx-serialization
.
implement the log structure
@Serializable
class LogDetail(val scope: String, val message: String): LogStructure
@Serializable
class Log(val name: String, val logDetail: LogDetail): LogStructure
and use DetailStructureFormatter
to format the log.
val logger = LoggerFactory()
.addProvider(ConsoleProvider{
formatter = DetailStructureFormatter
})
.getLogger()
then write the log
logger.info(
Log(
name = "illegal state",
LogDetail(
"args",
"argument must be greater than zero"
)
),
Log.serializer()
)
// {"message":{"name":"illegal state","logDetail":{"scope":"args","message":"argument must be greater than zero"}},"level":"INFO","date":"2023-12-29T12:34:56Z"}
Last modified: 12 March 2024