Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Change Log level at runtime #11

Closed
nigampatel opened this issue Apr 18, 2017 · 7 comments
Closed

Change Log level at runtime #11

nigampatel opened this issue Apr 18, 2017 · 7 comments

Comments

@nigampatel
Copy link

Is there a way in pino to change Log level at runtime?

My goal is to create an endpoint where we can change log level without restarting an app.

Similar to this.
https://github.com/yannvr/Winston-dynamic-loglevel

@davidmarkclements
Copy link
Member

hi @nigampatel see http://npm.im/pino-arborsculpture

(with thanks to @jsumners for creating it)

@jsumners
Copy link
Member

jsumners commented Apr 18, 2017

And if you really want to emulate that Winston wrapper, all you have to do is implement an end point in your application that sets log.level = level.

Note: not the req.logger. That one exists only for the life time of the request.

@nigampatel
Copy link
Author

Thank you for the quick response.

I don't have an option to use the pino-arborsculpture. I'll try to see if I can get it work using the log.level approach.

@MarkusAnthony
Copy link

MarkusAnthony commented Jun 7, 2019

Can I get a little more info on this?
(I am relatively new to nodejs)

In server.js I:

const pino = require('pino');
const log = pino({ level: process.env.LOG_LEVEL || 'info' });

const expressPino = require('express-pino-logger');
const expressLogger = expressPino({ logger:log });
const app = express();

app.use(expressLogger);

So in "routes\changelevel.js" how would I set

"log.level = level"

@jsumners How do I get access to log from server.js? I am assuming you are talking about the initial instantiation of pino?

-Markus

@jsumners
Copy link
Member

jsumners commented Jun 7, 2019

I know nothing of Express. But I imagine it is attached to the request like req.log. So req.log.info('foo') would log the string "foo" and req.log.level = 'debug' would change the level to "debug".

@MarkusAnthony
Copy link

MarkusAnthony commented Jun 7, 2019

The level change does not persist when using the following:

router.get('/level/:level', async function(req, res) {
  req.log.level = req.params.level
  res.send(200, 'Update log level to: ' + req.params.level);
});

router.get('/level', async function(req, res) {
  res.send(200, 'log level: ' + req.log.level);
});

@nigampatel did you find a way?

Markus

@medolino
Copy link

I run into the same problem today. It's a bit late to answer but anyway :)

If you want the level change to be persisted, you need to change loggingMiddleware.logger.level.

Here is an example, how you can do it:

const express = require('express')
const bodyParser = require('body-parser')
const pinoExpress = require('express-pino-logger')

const app = express()

app.use(bodyParser.json())

const pinoExpressMiddleware = pinoExpress()

app.use(pinoExpressMiddleware)

app.route('/log-level')
  // get current log level
  .get((req, res) => {
    res.json({ level: req.log.level })
  })
  // update log level
  .put((req, res) => {
    const newLogLevel = req.body.level

    pinoExpressMiddleware.logger.level = newLogLevel

    res.json({ status: `Log level changed to ${newLogLevel}` })
  })

app.listen(3000, () => console.log('Server started.'))

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants