Multi user apps script logger introduced the concept of an apps script Logger that could write to an abstracted database. In order to set up your own Logging environment (the demos you may have looked at so far are using a public version of mine), there are a few one off tasks you need to take care of.
In order to keep the access and caches private to you, you need to clone, customize or create the items in pink below. For those in blue, you can use the libraries I provide (or fork if you prefer)
YourDoggerServer
This is a shared library in which you centralize the setting up your logging databases and credentials. if you want to use the Inspector client (or write your own), you’ll also need this to be published as a webapp to serve up JSON/JSONP of your log database contents.
Setting up the database handler
In this example I’m using DriverScratch as the DB, since I want the data to be self cleaning. You could choose another if you prefer. See Database abstraction with google apps script for examples.
yourDoggerServer
Create a script called yourDoggerServer, and add this to it – adapting the credentials as you wish.
/* globals * set up the kind of back end you want your logging to go to * This example uses a scratch DB with a lifetime of 2 hours * your client will need to know the siloid, dbid and cachecommunity to be able to access this * I'm using the cache associated with this script (not the library) * anyone who knows the keys above can access */ var doggerProfile = { driver:cDriverScratch , options : { siloid:'doggerlogger', dbid:'mcphercom', cachecommunity:'jesuischarlie', specificcache:yourCacheBucket.getScriptCache(), scratchexpiry:60*60*2, disablecache:true } }; /** * get a handler for the dogger - it'll use the global doggerProfile for defaults * @param {boolean} optClear whether to clear the logfile first * @param {string} optThread the thread id if required to report * @param {string} options optional override defaulat handle * @return {Dogger} a dogger handle */ function getDogger( optClear, optThread, optOptions) { return cDogger.getDogger(doggerProfile, optClear, optThread, optOptions); }
This profile will now become your default logging place and credentials. The credentials (siloid, cachecommunity and dbid) are all needed to be able to access this log database, and will match those you set up in Dogger settings if you are using the inspector
A note on caching
Every database backend can use caching for query results. By default it will. Query caching is at its most effective when you have multiple requests for the same data with no writes in between. However it does use up some resource so you should decide whether this is worthwhile for your case and set disablecache accordingly.
In the case of DriverScratch – which I’ve chosen to use in this example, cache is actually used for the database itself (as well as for caching query results). You’ll notice that I’ve asked it to use a specific cache here. By default it will use the cache associated with the cDogger library but you will probably want to use your own cache for this instead.
yourCacheBucket
I recommend centralizing your cache allocation through a specific library. This way you can share its contents across multiple apps. It’s an optional to do this (you could use the cDogger cache, or the cache of yourDoggerServer), but you’ll see the benefits for this approach later if you become a regular Database abstraction with google apps script user.
Create a script called yourCacheBucket, and add this code to it
/** * the purpose of this is simply to provide a common cache object for collaborating scripts */ function getUserCache() { return CacheService.getUserCache(); } function getDocumentCache() { return CacheService.getDocumentCache(); } function getScriptCache() { return CacheService.getScriptCache(); }
Adding libraries
Now go back to yourDoggerServer, and add the libraries you’ll need (adapting the driver to the specific database you’ll need, and applying the key for yourCacheBucket (instead of mine).
cDoggercDbAbstractioncDriverScratchcUsefulyourCacheBucket
The button below will get you info on where to get the library code, source code and other info on all these libraries and their dependencies.
Creating a webapp
If you are planning to use the Multi user apps script logger inspector (or some other app that needs JSON/JSONP API access), regardless of the backend you’ve chosen to use, you’ll need to add this to yourDoggerServer script and publish as a webapp. It is the key of this published webapp (along with the credentials you set up earlier), that you’ll need to provide in Dogger settings if you are using the Polymer Inspector app.
/** * web app api - you'll publish this to be able to get at your log file * @param {object} e parameters (callback, action, cachecommunity, query) * @return {ContentService} json/jsonp */ function doGet(e) { // do whatever this webapp is for return cDogger.webApp(e, doggerProfile); }
Adding dependency information.
When I create libraries, I like to add dependency info (as described in Get GAS library info) to them. If you want to you can add these to each of your libraries, substituting the detail for the information for your libraries.
/** * used for dependency management - update this to match proj properties etc for reporting (optional) * @return {LibraryInfo} the info about this library and its dependencies */ function getLibraryInfo () { return { info: { name:'yourDoggerServer', version:'0.0.1', key:'MEGLjTi8U0A6f6YU6uLgWiyz3TLx7pV4j', share:"https://script.google.com/d/1n4FMjgP3lW5Y7R4Ru6jtfLcgbxzShXQEJvGt4ZGPmsyBOGBYk66sp2l9/edit?usp=sharing", description:"create one of these to make a single Dogger data point" }, dependencies:[ cDogger.getLibraryInfo(), cDbAbstraction.getLibraryInfo(), cDriverScratch.getLibraryInfo(), cUseful.getLibraryInfo(), yourCacheBucket.getScriptCache() ] }; }
/** * used for dependency management - update this to match proj properties etc for reporting (optional) * @return {LibraryInfo} the info about this library and its dependencies */ function getLibraryInfo () { return { info: { name:'yourCacheBucket', version:'0.0.1', key:'MveRagx_AESQYZ7d3DtGFtiz3TLx7pV4j', share:"https://script.google.com/d/1y4e_wxqNjMiq5DggWa1iunHW4YZU7pg97NlAuwWK50CXVxkRKATuX9kb/edit?usp=sharing", description:"Bruces cachebucket - clone and make your own" }, dependencies:[ ] }; }
A note on libraries
It is possible to use the cDogger library directly from each script that you want to log in this way (avoiding creating these 2 libraries), but the minimal effort in doing this will save you a lot of time later on.
For more on this see Multi user apps script logger, Polymer and Database abstraction with google apps script
For help and more information join our community, follow the blog or follow me on Twitter