Tired of typing Logger.log() - Here's a function to shorten it to "ll"

64 views
Skip to first unread message

Alan Wells

unread,
Jan 22, 2020, 8:54:01 PM1/22/20
to Google Apps Script Community
I don't like typing

Logger.log()

constantly.  I'd rather just type ll()

And I don't want to type:
JSON.stringify() every time I want to log an object

Here is the code I use to do that.


/*{version:"1/22/20"}
1/22/20 - var f
7 8 19 proc essValForLog


*/


function ll(a,b) {
 
var f;
 
 
//Logger.log('ll ran: ' + a + " " + b)
 
if (ll.arguments.length === 2) {//two arguments were passed in
    b
= processValForLog(b);
    f
= new Function('a','b','Logger.log(a + ":" + b)');//Create new fnk
    f
(a,b);//call fnk
 
} else {
    f
= new Function('a','Logger.log(a)');
    f
(a);
 
}
}

function processValForLog(val) {
try{
 
var typeOfThisVal;
 
  typeOfThisVal
= typeof val;

 
if (val) {
   
if (typeOfThisVal === 'object') {//The variable val can be an object and still be null
     
     
if (Array.isArray(val)) {//The very first test should be for an array because that is easy to check for
       
//console.log('it is an array')
       
if (val.toString().indexOf("[object Object]") !== -1) {
          val
= JSON.stringify(val);
       
} else {
          val
= val.toString();
       
}
       
//Logger.log('val: ' + val)
     
} else {//It is an object but not an array
       
//Logger.log('its NOT an array')
       
try{
          val
= JSON.stringify(val);//Test for whether it is a date
       
}catch(e) {//If this is an invalid date then JSON stringify will fail
         
//console.log('Error stringifying')
       
}
     
}
     
     
//Logger.log('typeof val: ' + typeof val)
     
       
if (typeof val !== 'string') {
         
try{
            val
= val.toString();
         
}catch(e) {
            val
= e.message;
         
}
       
}
     
     
if (val.indexOf("{") !== 0 && typeof val !== 'string') {
        val
= val.toString();
     
}
     
     
//continue;
   
}
 
}
 
 
if (typeOfThisVal === 'number') {
    val
= '"' + val.toString();
 
}
 
 
if (val === undefined) {//Avoid having all three cells undefined
    val
= 'UNDEFINEDDD';
 
} else if (val === null) {
    val
= 'NULLLL';
 
} else if (val === false) {
    val
= 'FALZZE';
 
} else if (val === true) {
    val
= 'TREWWW';
 
}
 
 
return val;
}catch(e) {
 
var f = new Function('e','Logger.log(e.message + ":\n\n" + e.stack)');
  f
(e);
}
}



Dimu Designs

unread,
Jan 23, 2020, 8:52:54 AM1/23/20
to Google Apps Script Community
I'm good with Logger.log(). It tells me exactly what it does at a glance, making for more readable code. I imagine you'll save a lot of key strokes though.
Reply all
Reply to author
Forward
0 new messages