Skip to content

How to manually set the logger name in Kotlin/Js? #580

@TheBestPessimist

Description

@TheBestPessimist

I have read a few issues here (almost all opened and closed which mention Kotlin JS: https://github.com/oshai/kotlin-logging/issues?q=sort%3Aupdated-desc%20is%3Aissue%20kotlin%20js), and almost nothing works for me. I also looked at - #500.

I am working on an Obsidian plugin, which is an electron application (so i would say it's Node).

My setup:

// kotlin version: 2.2.20

kotlin {
	js {
		outputModuleName = "shared"
		nodejs {
		}
		binaries.executable()
		generateTypeScriptDefinitions()
		compilerOptions {
			target = "es2015"
			moduleKind = org.jetbrains.kotlin.gradle.dsl.JsModuleKind.MODULE_COMMONJS
		}
	}
	
	compilerOptions {
		apiVersion = KotlinVersion.KOTLIN_2_2
		languageVersion = KotlinVersion.KOTLIN_2_2
	}

	sourceSets {
		commonMain {
			dependencies {
				api(kotlin("stdlib"))
				implementation(libs.kotlinx.coroutines.core)
                implementation ("io.github.oshai:kotlin-logging:7.0.13")
			}
		}

		jsMain.dependencies {
			api(kotlin("stdlib-js"))
			api(project(":obsidian-api"))
        }

I have tried multiple combinations of creating my logger (top level variable, object, companion object) and this is my current state:

class log {
    // val log = KotlinLogging.logger {}
    // val log = KotlinLogging.logger("ll")
    val log by KotlinLogging.logger()
}

And usage in another function outside of a class:

fun cycleViewStateForwardCallback(leaf: WorkspaceLeaf) {
    [...]
    KotlinLoggingConfiguration.logLevel = Level.TRACE
    log().log.info { "log info" }

Here are my outputs and errors:

  1. val log = KotlinLogging.logger {} - this is the only way in which i can get any output
INFO: [    at new log (plugin:tbp-toolbox:4028:24)] log info
  1. val log = KotlinLogging.logger("ll") - error
Plugin failure: tbp-toolbox TypeError: context.m9 is not a function
    at SupervisorJobImpl.plus [as n9] (plugin:tbp-toolbox:1694:21)
    at new TbpToolboxPlugin (plugin:tbp-toolbox:3637:52)
    at new default_0 (plugin:tbp-toolbox:3655:3)
    (longer stacktrace, doesn't look interesting to me)
  1. val log by KotlinLogging.logger() - error
lugin failure: tbp-toolbox TypeError: context.p9 is not a function
    at SupervisorJobImpl.plus [as q9] (plugin:tbp-toolbox:1702:21)
    at new TbpToolboxPlugin (plugin:tbp-toolbox:3669:52)
    at new default_0 (plugin:tbp-toolbox:3687:3)
    (longer stacktrace, doesn't look interesting to me)

Looking through all those issues, I believe i tried all ways to create my logger and none of them give me the proper log name that i want.
My desired output would be:

INFO: [ll] log info

maybe customized somehow with the current time (23:51:66.01214567), but that's just if i want to get fancy.

How can i set my desired log name?
If there is any output you may need, i can give that too. My plugin is exported in a single main.js file, so I can provide that too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions