I recently “discovered” Google Apps Scripts as a great and simple way to script cloud services. I started to use it for a variety of small tasks where I wanted to work on data, store a bit here and there etc. After all, you can access a variety of services, including Databases via JDBC, and you can run your scripts based on triggers (time and event driven).

One of the first things I stumbled upon was the lack of a persistent and “headless” logging facility. While there is a Logger class, it’s only accessible in the Script Editor and can’t be used for logging triggered scripts. While shopping around for existing solutions, I was quite dissatisfied, as most existing code either was missing log levels or was otherwise not exactly living up to my expectations.

I ended up wrapping log4js into a Google Apps Script, extended by a special logger that writes log entries into a Google Drive file. The whole thing is still quite crude and simplistic, but then I think it’s quick and easy to use.

// Create a text document (.txt or .log) on Google drive and get it's document ID to use it as a log file
var docLogger = new Log4gs.docLogger('some_text_file_id');
var myLogger = new Log4gs.Log(Log4gs.Log.INFO, docLogger.log);

// Output a debug and a info message
// Only the info message will appear in the output,
// as the loglevel has been set to "INFO".
// You can change this during runtime with the
// setLevel method of the logger.
myLogger.debug("Debug message");
myLogger.info("Info message");

One of the great things about Google Apps script is that you can use libraries made by others. So, with no further ado, here’s the Library ID of my Log4gs library: MBYN4JZ76dD6wlzCD41fxa6cLgnzAPoti.

Use the library at your own risk and feel free to comment/suggest on the sourcecode.

As a side note: One of the rather “sad” things about Google Apps Script is the fact that getting code into and out of Projects is a bit cumbersome and either you decide to share your library (as I did) or you go and put all code into a github repository, ending up copying and pasting code forth and back.


No comments. Be the first to add one!
Add a comment.
We'll never share your email with anyone else. We use the Gravatar system to pull in pictures based on an anonymous hash.
Once you submit your comment, it will be moderated and then show up here shortly after.