W
Wasp3mo ago
p4j4r5

Can't find example to configure logging(ex winston) on the server.

I would like log some information during api calls . I would like it to be debug for dev and info for production. And also log thing in json format instead of lines which get split across in log aggregation framework. Any suggestion how to achieve this
2 Replies
kapa.ai
kapa.ai3mo ago
Hi @p4j4r5. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain specific examples or instructions on how to configure logging with Winston on the Wasp server. However, I can guide you on how to add custom middleware to your Wasp server, which could potentially be used to add a custom logging middleware. In Wasp, you can customize middleware in three places: globally, per-api, and per-path. If you want to apply your logging middleware to all operations and APIs, you would modify the global middleware. This can be done by defining a middlewareConfigFn in the server field of your app declaration in your main.wasp file. This function should take the current middleware configuration and return a new configuration with your modifications. Here is an example of how to do this:
app MyApp {
// ...
server: {
setupFn: import { mySetupFunction } from "@src/myServerSetupCode.js",
middlewareConfigFn: import { myMiddlewareConfigFn } from "@src/myServerSetupCode.js"
}
}
app MyApp {
// ...
server: {
setupFn: import { mySetupFunction } from "@src/myServerSetupCode.js",
middlewareConfigFn: import { myMiddlewareConfigFn } from "@src/myServerSetupCode.js"
}
}
In your myMiddlewareConfigFn, you could potentially add your Winston logger as a middleware. However, the specifics of how to do this are not provided in the knowledge sources. For more information on how to customize middleware in Wasp, you can refer to the Wasp middleware configuration documentation (https://wasp-lang.dev/docs/advanced/middleware-config). For environment-specific configurations (like setting the log level to debug for development and info for production), you can use environment variables. In Wasp, you can define environment variables in .env.server and .env.client files for server and client environments respectively. These variables can then be accessed in your code using process.env. For more information on how to use environment variables in Wasp, you can refer to the Wasp environment variables documentation (https://wasp-lang.dev/docs/project/env-vars). As for logging in JSON format, this would depend on the capabilities of the logging library you are using. You would need to refer to the documentation of your logging library (Winston, in this case) for instructions on how to do this. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @p4j4r5, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos3mo ago
Server Config | Wasp
You can configure the behavior of the server via the server field of app declaration:
Want results from more Discord servers?
Add your server