Search code examples
pythongoogle-app-engineloggingpython-logging

Where does GoogleAppEngineLauncher keep the local log files?


GoogleAppEngineLauncher can display the local log file of my app while it is running on my Mac during development. However, I can't change the font size there so I would like to use the tail command to watch the log file myself.

It's a shame but I can't find the log files. They are not under /var/log/, ~/Library/Logs or /Library/Logs. Do you know where they are?

(Maybe there are no physical files, just the stdout of the python development environment and so the log is only available in the launcher application.)


Solution

  • As you surmise, and can confirm by studying the source file /usr/local/google_appengine/google/appengine/tools/dev_appserver.py, the logs are not being written to disk (a cStringIO.StringIO instance is used to keep them in memory, as the rest of the code is oriented to writing them "to a file-like object").

    What I would recommend is writing your own app server script, which imports dev_appserver, subclasses dev_appserver.ApplicationLoggingHandler, and overrides just one method:

    from google.appengine.tools import dev_appserver
    
    class MyHandler(dev_appserver.ApplicationLoggingHandler):
    
        def __init__(self, *a, **k):
            dev_appserver.ApplicationLoggingHandler.__init__(self, *a, **k)
            self.thefile = open('/tmp/mylog.txt', 'w')
    
        def emit(self, record):
            dev_appserver.ApplicationLoggingHandler(self, record)
            self.thefile.write(str(record) + '\n')
            self.thefile.flush()
    

    You also need to ensure this class is used instead of the standard one, e.g. by subclassing the dispatcher or ensuring you use its dependency-injection capability. (dev_appserver_main.py lets you control this better, I think).

    I think this customization approach is far more cumbersome than it should be (it's perfectly normal to want the logs written to file, after all -- either to display them differently, as you desire, or to process them later with some auxiliary script), and so I'd also recommend putting a feature request on app engine's tracker: dev_appserver.py should accept one more flag, which, if specified, gives the path on which to write logs to disk.

    And, to be honest, if I needed this feature right now, myself, I'd do it the dirty way: editing that .py file (and its related _main.py) to add said flag and its use. That should be a dozen lines in all, much easier than the "canonical" way I just outlined. Of course, it is dirty because every time there's a new SDK you'll have to apply the patch again, and again, and again... which is why one should also propose the patch on GAE's tracker, as part of the feature request I suggested, hoping it gets accepted soon!-)