angularjs-logDecorator icon indicating copy to clipboard operation
angularjs-logDecorator copied to clipboard

$log.info with objects

Open negue opened this issue 11 years ago • 0 comments

Hey,

first of all: Great Decorator - Example, thanks!

I often use $log.info with objects and often with multiple params, to show all properties, but with your decorator these are all gone... and don't get "logged".

Here is my updated function to support both features:

var enhancedLogFn = function ( )
{
   // prepend a timestamp and optional classname to the original output message
   var now  = DateTime.formattedNow(),
       args = [now, className || ''], extraArguments = [],
       argCount = arguments.length,
       format = ["{0} - {1}"], formatCounter = 2, i;

       if(argCount >= 1){
          for(i in arguments){
             if(typeof(arguments[i]) in { "array":1, "object":1, "error": 1, "function":1 } ){
                extraArguments.push(arguments[i]);
             }else{
                format.push(" {",formatCounter++,"};");
                args.push(arguments[i]);
              }
           }
       }

       args = [ supplant(format.join(""), args) ];
       args = colorify( supplant.apply( null, args ), colorCSS );

       args = args.concat(extraArguments);

       logFn.apply( null, args );
};


Now you can use:

$log.info("My Info", someObject, [myArray:2], "other Text");

negue avatar Feb 01 '14 01:02 negue