0

I have created an Application using Hapi.js and i have used good-file to write logs to a file and when ever I use request.log() and server.log() methods, its writing the logs to the file. But my requirement is when ever I use console.log() it should log that into the file. Because i will not have request or server object in all of my files. How can i achieve it?

This is how i configured my reporter

myFileReporter: [{
    "module": "good-squeeze",
    "name": "Squeeze",
    "args": [{ 
        "request": "*", 
        "error": "*", 
        "response":"*",
        "log":"*"
    }]},
   {
        "module": "good-squeeze",
        "name": "SafeJson"
   }, 
   {
        "module": "good-file",
        "args": ["./logs/log"]
   }]
Liam
  • 27,717
  • 28
  • 128
  • 190
Anil Kumar R
  • 283
  • 1
  • 4
  • 12

3 Answers3

0

Since console.log has predefined behavior, you will either need to override it, or create a new function. For example:

const debugLog = (content) => {
    request.log(content);
    console.log(content);
};
ahrobins
  • 352
  • 2
  • 5
  • 13
0

From this answer by Ludovic Feltz...

// define a new console
var console=(function(oldCons){
    return {
        log: function(text){
            oldCons.log(text);
            // Your logging code here
        },
        info: function (text) {
            oldCons.info(text);
        },
        warn: function (text) {
            oldCons.warn(text);
        },
        error: function (text) {
            oldCons.error(text);
        }
    };
}(window.console));

//Then redefine the old console
window.console = console;

This allows you to continue using console.log while maintaining its original functionality alongside your modifications.

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
0

I think that the best solution is to make available the server object in the function you want to log from.

Can you tell me more ?

With custom plug in, you can almost make the server object available everywhere

Ernest Jones
  • 564
  • 6
  • 20