简体   繁体   中英

How should I include my custom logger using nodejs?

I wish to include my custom log in loggings,I am using pino module for loggings, Here is the log example This is the response every time when I am hitting my API

"1493118596934" "TRACE" "services-mc" "victoria-lap" "sr" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "" "GET" "/user/v1/favoritepets/?userId=123" "request received"
"1493118598480" "TRACE" "services-mc" "victoria-lap" "" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "" "GET" "/user/v1/favoritepets/?userId=123" ""
"1493118598490" "TRACE" "services-mc" "victoria-lap" "" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "" "GET" "/user/v1/favoritepets/?userId=123" ""
"1493118598494" "TRACE" "services-mc" "victoria-lap" "ss" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "200" "GET" "/users/v1/favoritepets/?userId=123" "response sent"

Here I have to include my custom log like this

"1493118596934" "TRACE" "services-mc" "victoria-lap" "sr" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "" "GET" "/user/v1/favoritepets/?userId=123" "request received"
*"pet_hit_journal" "15829507" "2017-03-10" "741184567" "" "web"*
"1493118598480" "TRACE" "services-mc" "victoria-lap" "" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "" "GET" "/user/v1/favoritepets/?userId=123" ""
"1493118598490" "TRACE" "services-mc" "victoria-lap" "" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "" "GET" "/user/v1/favoritepets/?userId=123" ""
"1493118598494" "TRACE" "services-mc" "victoria-lap" "ss" "" "" "094ce63c-e067-4c4e-a441-92c247f0ed43" "200" "GET" "/users/v1/favoritepets/?userId=123" "**response sent**"

Note: The custom log messages for knowing that the data is fetched from the database when I am using the particular controller. Should it be possible? any idea, please...

Logging in Hapi can be done through the server.log and request.log functions.

It's possible to intercept these manually and add any extra information you need using server.on('log', ...) and server.on('request', ...) before continuing to log your message. To keep your code organized, you can also wrap these in a custom plugin . In one of these is likely where you would include your custom logger.

You should take a look at the Logging Tutorial as it explains the parameters for the above functions.

Good is an official plugin to monitor and report on hapi server events. Also have a look at this. Good-console will help you output those events to the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM