When a request is sent to your app, a request log is automatically written by App Engine. During the handling of the request, your app can also write application logs. In this page, you'll learn how to write application logs from your application, how to read both application and request logs programmatically using the Logs API, how to view logging in the Google Cloud Platform Console, and how to understand the request log data that App Engine writes during the request.
Request logs vs application logs
There are two categories of log data: request logs and application logs. A request log is automatically written by App Engine for each request handled by your app, and contains information such as the project ID, HTTP version, and so forth. For a complete list of available properties for request logs, see RequestLog. See also the request log table for descriptions of the request log fields.
Each request log contains a list of application logs (AppLog) associated with that request, returned in the
RequestLog.app_logs property. Each app log contains the time the log was written, the log message, and the log level.
Writing application logsYou may find it useful to read the documentation for the standard Python logging module at Python.org.
The Python logging module allows a developer to log 5 levels of severity. :
The following example shows how to use the different logging levels:
import logging import webapp2 class MainPage(webapp2.RequestHandler): def get(self): logging.debug('This is a debug message') logging.info('This is an info message') logging.warning('This is a warning message') logging.error('This is an error message') logging.critical('This is a critical message') try: raise ValueError('This is a sample value error.') except ValueError: logging.exception('A example exception log.') self.response.out.write('Logging example.') app = webapp2.WSGIApplication([ ('/', MainPage) ], debug=True)
Log URL format in the Google Cloud Platform Console
See the following sample URL for an example of the log URL format in the GCP Console:
Reading logs in the console
To view logs written by apps running in the flexible environment, use the Logs Viewer.
To filter log entries by label or text search in Logs Viewer, see Basic Logs Filters. To write advanced logs filters using expressions specifying a set of log entries from any number of logs, see Advanced Logs Filters.
A typical App Engine log contains data in the Apache combined log format, along with some special App Engine fields, as shown in the following sample log:
192.0.2.0 - test [27/Jun/2014:09:11:47 -0700] "GET / HTTP/1.1" 200 414 "http://www.example.com/index.html" Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" "1-dot-calm-sylph-602.appspot.com" ms=195 cpu_ms=42 cpm_usd=0.000046 loading_request=1 instance=00c61b117cfeb66f973d7df1b7f4ae1f064d app_engine_release=1.9.64
Understanding request log fields
The following table lists the fields in order of occurrence along with a description:
|Field Order||Field Name||Always Present?||Description|
|1||Client address||Yes||Client IP address. Example:
|2||RFC 1413 identity||No||RFC 1413 identity of the client. This is nearly always the character
|3||User||No||Present only if the app uses the Users API and the user is logged in. This value is the "nickname" portion of the Google Account, for example, if the Google Account is
|4||Timestamp||Yes||Request timestamp. Example:
|5||Request querystring||Yes||First line of the request, containing method, path, and HTTP version. Example:
|6||HTTP Status Code||Yes||Returned HTTP status code. Example:
|7||Response size||Yes||Response size in bytes. Example:
|8||Referrer path||No||If there is no referrer, the log contains no path, but only
|9||User-agent||Yes||Identifies the browser and operating system to the web server. Example:
|10||Hostname||Yes||The hostname used by the client to connect to the App Engine application. Example : (
|11||Wallclock time||Yes||Total clock time in milliseconds spent by App Engine on the request. This time duration does not include time spent between the client and the server running the instance of your application. Example:
|12||CPU milliseconds||Yes||CPU milliseconds required to fulfill the request. This is the number of milliseconds spent by the CPU actually executing your application code, expressed in terms of a baseline 1.2 GHz Intel x86 CPU. If the CPU actually used is faster than the baseline, the CPU milliseconds can be larger than the actual clock time defined above. Example:
|13||Exit code||No||Only present if the instance shut down after getting the request. In the format
|14||Estimated cost||Yes||DEPRECATED. Estimated cost of 1000 requests just like this one, in USD. Example:
|15||Queue name||No||The name of the task queue used. Only present if request used a task queue. Example:
|16||Task name||No||The name of the task executed in the task queue for this request. Only present if the request resulted in the queuing of a task. Example:
|17||Pending queue||No||Only present if a request spent some time in a pending queue. If there are many of these in your logs and/or the values are high, it might be an indication that you need more instances to serve your traffic. Example:
|18||Loading request||No||Only present if the request is a loading request. This means an instance had to be started up. Ideally, your instances should be up and healthy for as long as possible, serving large numbers of requests before being recycled and needing to be started again. Which means you shouldn't see too many of these in your logs. Example:
|19||Instance||Yes||Unique identifier for the instance that handles the request. Example:
|20||Version||Yes||The current App Engine release version used in production App Engine: 1.9.64|
Quotas and limits
Your application is affected by the following logs-related quotas:
- Logs data retrieved via the Logs API.
- Log ingestion allotment and retention.
Quota for data retrieved
The first 100 megabytes of logs data retrieved per day via the Logs API calls are free. After this amount is exceeded, no further Logs API calls will succeed unless billing is enabled for your app. If billing is enabled for your app, data in excess of 100 megabytes results in charges of $0.12/GB.
Logs ingestion allotment
Logging for App Engine apps is provided by Stackdriver. By default, logs are stored for an application free of charge for up to 7 days and 5GB. Logs older than the maximum retention time are deleted, and attempts to store above the free ingestion limit of 5 gigabytes will result in an error. You can update to the Premium Tier for greater storage capacity and retention length. See Stackdriver pricing for more information on logging rates and limits. If you want to retain your logs for longer than what Stackdriver allows, you can export logs to Google Cloud Storage, Google BigQuery, or Google Cloud Pub/Sub.
The development server and Logs API
By default, logs are stored in memory only in the development server and are accessible if you wish to test the Logs API feature. If you wish to persist logs from the development server to disk at the default location
/tmp/dev_appserver.logs, supply the
--persist_logs command line option as follows:
dev_appserver.py --persist_logs your-app-directory
If you wish to persist the logs from the development server to disk at a location of your own choosing, supply the desired path and filename to the
--logs_path command line option as follows:
dev_appserver.py --logs_path=your-path/your-logfile-name your-app-directory