How do I get my Lambda function to have a different log level depending on stage?

I want to have different log levels for the prod and dev env, how can I get it to do this?

I thought of a custom variable in serverless.yml, like loglevel, and then give the different stages various log levels, but then how do I get the code to know which environment it is in?

Any help much appreciated. Thanks

use a variable in self.provider.envrironment like

LOG_LEVEL: ${self:custom.environment.${self:custom.stage}.logging}

then have the appropriate level for each stage like

self.custom.environment.dev: debug
self.custom.environment.prod: warn/error (whatever your requirement is)

your can then reference it from code via

process.env.LOG_LEVEL

we use bole so in the log.js file we have

bole.output({ level: process.env.LOG_LEVEL, stream })

You can also define a LOG_LEVEL at the function level to override the stages settings if you need

Hope this helps