Skip to content

Kotlin logging library that makes your logs look more โœจ ๐™ฅ๐™ง๐™š๐™ฉ๐™ฉ๐™ฎ โœจ

License

Notifications You must be signed in to change notification settings

LukynkaCZE/PrettyLog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

45 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒˆ PrettyLog โœจ

Maven metadata URL wakatime Language

A Kotlin logging library focused on readability in console. PrettyLog takes advantage of ANSI color codes to make your logs look โœจ pretty โœจ.

image

Installation

As of 2.0, PrettyLog supports the following targets:

  • jvm (Java 21)
  • mingwx64 (Windows)
  • macosx64
  • macosarm64
  • linuxx64
  • linuxarm64

Supporting both Kotlin/JVM and Kotlin/Native

Kotlin

repositories {
    maven("https://mvn.devos.one/releases")
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.2")
    implementation("com.squareup.okio:okio:3.10.2")

    // Import the common code
    implementation("cz.lukynka:pretty-log-common:<version>")

    // Import the platform-specific library. Do jvm if you're doing Kotlin/JVM / Java, 
    // Do any other platform if you're doing Kotlin/Native [like linuxx64 or mingwx64]
    implementation("cz.lukynka:pretty-log-PLATFORMNAMEHERE:<version>")
}

Logging

Logging is very easy, just call the log(message, type) method. type parameter is optional and defaults to RUNTIME. Add LoggerFileWriter.load() to your main function if you want logs to be saved.

log("Hello there!")
log("general kenobi", LogType.NETWORK)

image

You can also log exceptions!

} catch (exception: Exception) {
    log(exception)
}

image

Logger Settings

You can change settings by simply setting LoggerSettings.<setting> to its new value

// Should the logs be saved to file?
PrettyLogSettings.saveToFile = true
// The path to the logs directory
PrettyLogSettings.saveDirectoryPath = "./logs/"
// Format of the log file name
PrettyLogSettings.logFileNameFormat = "yyyy-MM-dd-Hms"
// Logs which will be disabled and not show up
PrettyLogSettings.disabledLogTypes = setOf(LogType.WARNING)

Log Types

There are 16 log types by default:

image

You can make custom log types by making your own LogType instance. Everything from the prefix background color, text color to the actual message text color is customizable

// Create custom log prefix
val customLogStyle = LogStyle(textColor = AnsiColor.BLACK, backgroundColor = AnsiColor.CUTE_PINK_BACKGROUND)
val customLogPrefix = StaticLogPrefix(" โ‰ฝ^โ€ขโฉŠโ€ข^โ‰ผ ", customLogStyle)

// Create custom log type
val customLogType = LogType(textStyle = LogStyle.CUTE_PINK, listOf(customLogPrefix))

log("T-This is vewy cuwute message OwO", customLogType)
image

There is also DynamicLogPrefix which has field for text supplier instead of static string.

You may alternatively use simpler version of this for class-specific logger by referencing the class in the constructor:

val logger = PrettyLogger(this::class)
image

Loggers with custom prefixes

You can add multiple prefixes to a custom logger. For example, we can add "Inbound" prefix and a "Proxy" for better understanding what is being logged and from where

val proxyPrefix = StaticLogPrefix(" Proxy ", LogStyle.FILLED_PURPLE)
val inboundPrefix = StaticLogPrefix("-> Inbound ", LogStyle.GRAY)
// create custom instance of PrettyLogger
val logger = PrettyLogger(inboundPrefix, proxyPrefix)


Events.on<PacketReceivedEvent> { event ->
    logger.log(event.packet.name, LogType.NETWORK)
}
image

Implementations in other languages

About

Kotlin logging library that makes your logs look more โœจ ๐™ฅ๐™ง๐™š๐™ฉ๐™ฉ๐™ฎ โœจ

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages