How to use Morgan logger?

后端 未结 10 1628
囚心锁ツ
囚心锁ツ 2020-12-07 11:19

I cannot log with Morgan. It doesn\'t log info to console. The documentation doesn\'t tell how to use it.

I want to see what a variable is. This is a code from

10条回答
  •  日久生厌
    2020-12-07 11:59

    Using morgan is pretty much straightforward. As the documentation suggests, there are different ways to get your desired output with morgan. It comes with preconfigured logging methods or you can define one yourself. Eg.

    const morgan = require('morgan')

    app.use(morgan('tiny')

    This will give you the preconfiguration called tiny. You will notice in your terminal what it does. In case you are not satisfied with this and you want deeper e.g. lets say the request url, then this is where tokens come in.

    morgan.token('url', function (req, res){ return '/api/myendpoint' })

    then use it like so:

    app.use(morgan(' :url ')

    Check the documentation its all highlighted there.

提交回复
热议问题