Changing log level at runtime without restarting the node server is an interesting idea. Didn’t see much posts on it but everywhere it was said, its doable.
I am going to accomplish this using following techniques.
Updates to the environment variables needs to be picked by Node Server
Changing the log level programatically in winston
1. Updating environment variables
Below is the content of the environment file. We are going to change NEW_VALUE variable in this test, initially it has ‘three’.
Below is the simple Node Express program and ’/’ route has the code to update environment variables. Basically we are reading the .env file and initializing values to process.env variable in a loop.
Below is the console output
2. Changing the log level in Winston
Below is a simple NodeJS Winston program where log level is changed at runtime
Below is teh output, you can notice ‘Text Debug 1’ did not appear. But after changing log level to ‘debug’. It shows up.
Logging levels in winston conform to the severity ordering specified by RFC5424: severity of all levels is assumed to be numerically ascending from most important to least important. When you set log level as info, you get all data above from info like error, warn and info.
Levels
Numbers
error
0
warn
1
info
2
http
3
verbose
4
debug
5
silly
6
In the below program, i am going to combine above two concepts together.
Below is the main express program
Below program contains the funtions used in the above main program
Data in environment files
Below is the output of the program and able to change the log level at runtime.