Ramyachinna
Ramyachinna

Reputation: 483

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...

Upvotes: 0

Views: 217

Answers (1)

Cuthbert
Cuthbert

Reputation: 2998

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.

Upvotes: 1

Related Questions