GAE : how to output log messages to a file

By default, all logging messages will output to log console. To change the logging settings, find this file – {Google App Engine SDK directory}\google\appengine\tools\dev_appserver_main.py.

File : dev_appserver_main.py – Find following pattern

#...
import getopt
import logging
import os
import signal
import sys
import tempfile
import traceback
 
logging.basicConfig(
level=logging.INFO,
format='%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
#...

Output to File

In order to output log messages to a file, we can change the configuration of logging in dev_appserver_main.py like in below:

#...
import getopt
import logging
import os
import signal
import sys
import tempfile
import traceback
 
# default , comment out
#logging.basicConfig(
# level=logging.INFO,
# format='%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
 
# new log settings , output to a file
logging.basicConfig(
filename='/Users/lokjack/gae.log',
filemode='a',
level=logging.DEBUG,
format='%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
#...

Restart the dev_appserver.py after changed the dev_appserver_main.py.

Now, the log console would not show any log messages, instead all output to a file (In this example, all log messages will be output to “/Users/lokjack/gae.log“).

Note
This hacks only works on local GAE development environment.

Download Source Code

Download it – gae-logging-to-file.zip (11 kb)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s