I have created a simple Web App(published as accessible to anonymous user) which gets the query param as follows:
function doGet(e){Logger.log("Key is"+e.parameter.key);}
Passed the param to app as follows: https://script.google.com/macros/s/AKfycbxZ_r7tTs1sCQH9UOhPg2AVdWuMCDk6zD58lnsXciazRLmZ7q7P/exec?key=test
It works fine when I hit the url as a logged in user, able to get the param. But when an anonymous user hits the same, data is not getting logged.
Is it possible. How to achieve this?
There is slight tweak here to the problem statement - Logger only works for only the script owner (who also happens to be logged). I double checked and this is indeed the behavior. I checked accessing a simple web app as another logged in user with a test account and Logger didn't work for that account either.
This is actually currently a known limitation of the Logger.
Logger is more useful for debugging as you are writing the code, not when other people are accessing your code. Logger information is also not persisted across different runs.
I actually end up writing my log statements to a simple file. I add little things like timestamps to it and you can make this much more robust. You can even add exception logs and effective user information to it if necessary.
Here is a couple snippets on how I usually set this up with the couple of exercises left to the reader.
Function I run once -
function initLog() {
//add check to see if log already exists so you don't init multiple times
var logfile = DocsList.createFile('Log file for my script','') ;
ScriptProperties.setProperty('LogFile', logfile.getId());
logfile.getId();
}
Utility function I can call from anywhere in my code -
function logMsg(msg){
//add more checks here to init log if its not already done
var logfile = DocsList.getFileById(ScriptProperties.getProperty('LogFile'));
logfile.append(new Date().toString() + ' - ' + msg + '\n');
}
Simple made up harness to show how this would work -
function logTests(){
initLog();
logMsg('hello world');
logMsg('test message');
}
And here is how it looks like. Hope this helps.