Couple of questions: 1. Is it correct that Ktor Se...
# ktor
e
Couple of questions: 1. Is it correct that Ktor Server logging ALWAYS outputs to
console
in JS? 2. Would you be up for a PR to allow programmatically setting a different output/level? I need to integrate logging with VS Code's own log channels.
c
you can log wherever you want using a custom logger or a library https://ktor.io/docs/client-logging.html
e
Is it the same for the server? I see Ktor uses
KtorSimpleLogger
with a logging level that works through
process.env
and that always outputs to
console
.
c
for the server I think there is just call logging -> https://ktor.io/docs/server-call-logging.html
I guess JS runs as “native” here, so its just “stdout”.
e
Yup but that's way too high level. The internal logging gets outputted to console. And I don't see how to override it.
b
You should be able to assign it with a custom implementation with the environment arg on the embedded server function.
Copy code
val environment = applicationEnvironment {
    this.log = CustomFileLogger("output.log")
}
That being said, there's a fair number static loggers floating around that default to
KtorSimpleLogger
which seem to be a legacy of a common practice with SLF4J when we were working only with JVM.
e
Thanks @Bruce Hamilton. The difference in my case is I'm using Ktor to output a library, so logging should be customizable programmatically even for those simple loggers.
If there is a new way in which loggers are being created now, do you think we can convert all existing usages of
KtorSimpleLogger
?
b
Yeah, we'll need to audit the codebase for all static references and provide a factory at the top level instead. It won't be a trivial fix, unfortunately. Alternatively, we could have some global state you can set in the js / wasm sources for the output. I think the only way to change the output globally atm would be to monkey-patch the KtorSimpleLogger output declaration and replace all the functions with something pointing to your custom sink.
Can you describe your usage a little more? You're wrapping the Ktor server?
e
@Bruce Hamilton I'm using the embedded server to create an MCP server. The MCP server is exposed as a library to a VS Code extension, which has its own logging. I can't use env vars or custom output files, but log messages must go through specific VS Code objects.
In the YT issue I suggested the same thing as a quick/temporary UX patch: let's create a global object that can be set by consumers to specify where messages end up (defaulting to console).
b
yeah that's the approach we have for setting the log level atm, it seems like a reasonable stop gap until we can refactor all the references
sounds like a cool project 👍
e
Yup! I'm testing out https://github.com/modelcontextprotocol/kotlin-sdk capabilities compared to the TypeScript implementation with servers such as Hono/Express. I'm tempted to using Kotlin/JS, but there are still rough edges as you can see.
b
Yeah there's been more and more interest in the wasm server implementation, though it's been mostly experimental. Moving the Ktor server to a multiplatform-friendly state is still an ongoing process, so please let us know if you run into any other issues.
e
Thank you! Btw to clarify, deploying Wasm on VS Code is no go at the moment, so JS is the main target for us.
👍 1
👍🏾 1