No output from Logger.log(...) in Google Apps Script .gs file - what's wrong?

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?


Solution 1:

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:

  • Google Apps Script Logging
  • Basic Logging
  • Stackdriver Logging
  • Exception Logging
  • Stackdriver Error Reporting
  • Logging Requirements
  • Google Apps Script Class Logger

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:

  • 36764984 Visible to Public Logger.log() sometimes does not work

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.