4

I have a bunch of server side code from which I log using Logger.log("message"). But one single .gs file does not log! Even with a statement as simple as this:

function uploadFiles(form) {
  Logger.log("uploadFiles() Hello?");
  ...
}

So simple yet I get zilch. Does anyone know a reason why I can't get any logging output from one .gs file while others in the same project can log fine?

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
jeff
  • 109
  • 1
  • 1
  • 9
  • 1
    I did a search in the Google Issue Tracker. Here is the search [link to Logger.log() issues](https://issuetracker.google.com/issues?q=status:open%20componentid:191640%20title:Logger.log) Click the star to indicate that it affects you. – Alan Wells Jan 17 '20 at 19:19
  • 1
    Is this affecting just one function, or al functions in the project? Also, what kind of project is this - is this an addon, a bound script, or a standalone script? – Rafa Guillermo Jan 20 '20 at 10:28
  • @RafaGuillermo - it's all logging from just one file .gs. It's a bound script and the file's code responsible for sending an email notification after a customer file is uploaded to Drive. I'm beginning to wonder whether it's a timing thing, that the app finishes before the logging is complete. The notification email it sends is correctly sent and looks good but from that particular file there's nothing. – jeff Jan 21 '20 at 16:25
  • @AlanWells - Thanks Alan. I see an interesting bug (comment?) about the logger not working sometimes. Perhaps I need to study how the logger's used, although for the last 4 years I've been using it and it's behaved just like I expected. I'll read through that discussion. – jeff Jan 21 '20 at 16:29
  • @jeff can you make a function in the `.gs` project which has nothing but a `Logger.log()` in and see if that works? Also - how long does the script take to run? You can see this in your execution logs in the `View > Execution Transcript` menu item. – Rafa Guillermo Jan 21 '20 at 16:41
  • @RafaGuillermo - I think I know what the problem is. In [this](https://issuetracker.google.com/issues/36764984) discussion at the issue tracker dy...@gmail.com says "There's no problem with the Logger. It CLEARS EVERY TIME IT IS CALLED BY A NEW FUNCTION. It is primarily used for running a single function from the Scripts interface." – jeff Jan 25 '20 at 13:32
  • @RafaGuillermo - In my code I first call my server-side uploadFiles() function and when I get the callback from that I call sendNotification() about the new uploaded file. In the log I see my output from sendNotification() but not from uploadFiles(). Is this is expected behavior then? – jeff Jan 25 '20 at 13:46
  • 1
    Save yourself the trouble and switch to Stackdriver logging (replace `Logger` with `console`). There, the log doesn't clear for each execution, and can accurately log the shape of things like arrays or objects. – tehhowch Jan 27 '20 at 12:29

3 Answers3

8

Answer:

The Google Apps Script Log which is written to with Logger.log() gets cleared each time the script is run, and so after multiple invokations only the most recent call's logs will be shown.

Avoidance & proper logging:

As well as the regular log which can be written to with Logger.log(), Google Apps Script has two other methods of logging - Stackdriver logging and Stackdriver Error reporting. As per the Apps Script documentation:

Apps Script provides three different mechanisms for logging:

  • The built-in Apps Script Logger, which is lightweight but persists only for a short time.
  • The Stackdriver Logging interface in the Developer Console, which provides logs that persist for many days after their creation.
  • The Stackdriver Error Reporting interface in the Developer Console, which collects and records errors that occur while your script is running.

Stackdriver Logging:

When you require logging that persists for a longer time than per-run, Stackdriver logs are preferred. These are attached to the GCP project that is associated with the Apps Script project, and a simplified version can be found in the Apps Script dashboard. Exception logging can also be done via the Stackdriver logs. This log can be written to by using the console.log() method rather than Logger.log().

Stackdriver Error Reporting:

You can view your Stackdriver error reports in the GCP console.

References:


This answer was updated after other information came to light. As there are known issues with the Logging method of Google Apps Script, the original answer has been kept below.


This appears to be a bug!

The Logger.log() function should log everything passed to the method, and regardless of how many functions are run in a single call, all logs from all functions from within the call should be viewable in the logger. The only exception is if there are too many Logger.log() calls, and the logs are truncated.

As mentioned above, There is already a report on Google's Issue Tracker which detail the same kind of behaviour:

Google does seem to know about this issue but if it's causing problems you can file your own bug about it here.

You can also hit the ☆ next to the issue number in the top left on the aforementioned pages which lets Google know more people are encountering this and so it is more likely to be seen to faster.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • 1
    Rafa per my comment above where I said "In my code I first call my server-side uploadFiles() function and when I get the callback from that I call sendNotification() about the new uploaded file," I didn't explain that those are 2 separate calls from browser to server. Given that Logger appears to log all calls from a SINGLE server-side invocation, I think that's why I'm seeing nothing from the first invocation, only the 2nd. I will look at Stackdriver logging per tehhowch. Maybe that will retain all logging calls. – jeff Jan 29 '20 at 02:46
  • 1
    Ahh yes this is correct then, there have been many issues with logging in the past but it appears this is likely it. I updated my answer to include this information as well. – Rafa Guillermo Jan 29 '20 at 08:27
2

I had a similar issue. For me, it was knowing where to look for the logs varies by how I start off my test run.

If I am running an AppScript initiated by the Run button in the IDE, it runs the open code module's function titled myFunction() and then the results are visible in the IDE's Execution log area next to the Run and Debug buttons.

Google App Script IDE Execution Log

If I am running an AppScript initiated directly by a Google Sheet then I have to look at the logs in the left nav's Executions area.

Google App Script Executions Area

Ajay Sindwani
  • 71
  • 1
  • 3
0

Had same issue - what haven't I tried The best thing that helped me is to save somewhere locally your .gs files, delete forever the bound script project, create a new one and add those files manually back

vstepaniuk
  • 667
  • 6
  • 14
  • I haven't tried it in a while, but at some point I realized that if my client code calls the server more than once during the same frame of execution, only one of the server executions will be logged. The other is lost. – jeff Jul 23 '23 at 15:12