Thursday, March 6, 2025

Javascript function to log the current method name and parameter

 It may be helpful to log the current method with some parameter without hardcode the method name for each log entry.  The below method can be used for this purpose


    logHelper(obj = {}) {
        var stack = new Error().stack,
        caller = stack.split('\n')[2].trim().substring(3);
        console.log(caller + ":" + JSON.stringify(obj));
     }

    someFunction(changes: SimpleChanges) {
        this.logHelper(changes);
// your business logic here
   }