Skip to content

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

Open

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.

Activity

  1. oshai commented on Nov 9, 2025

    @oshai
    Owner

    I don't know why option #2 doesn't work for you but it's the most straight forward way to define logger name. Looks like somehow the common code is not linked so you have this failure (the definition is in here).

  2. TheBestPessimist commented on Nov 9, 2025

    @TheBestPessimist
    Author

    @oshai do you have any suggestions for things i could try?

  3. TheBestPessimist commented on Nov 9, 2025

    @TheBestPessimist
    Author

    If it helps, my repo is here: https://github.com/TheBestPessimist/tbp-toolbox-obsidian

    Also, maybe it helps (or not?) but i'm running my logging code from inside a manually created coroutine scope (and i'm a noob at coroutines):

    open class TbpToolboxPlugin(app: App, manifest: PluginManifest) : Plugin(app, manifest) {
        // var settings: MyPluginSettings = DefaultSettings()
    
        // Note that using `MainScope()` does not work!
        private val pluginScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)  // ❗ here
    
        override fun onload(): Promise<Unit> {
            console.log("Loading TbpToolboxPlugin in Kotlin!")
    
            return pluginScope.promise {
                // loadSettings()
                setupPlugin()       				// ❗ my code is loaded from here
                // delay(10.seconds)
            }.catch {
                console.error("Error loading TbpToolboxPlugin", it)
            }
        }
    
  4. oshai commented on Jan 2, 2026

    @oshai
    Owner

    Have you find the cause or still need assistance?

  5. TheBestPessimist commented on Jan 3, 2026

    @TheBestPessimist
    Author

    Thanks for asking @oshai. I have stopped trying to understand what's going on. If i needed a lot, i added console.logs and that's it. Not nice, but it works for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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