Overview
The Logs API provides access to the application and request logs for your application. You can also access the logs for your application in the Logs Viewer provided in the Google Developers Console by clicking Monitoring > Logs in the left navigation panel
The Logs PHP API is invoked whenever the built-in PHP function syslog() is called. For example:
if (authorizedUser()) {
echo '<p>Welcome authorized user</p>';
syslog(LOG_INFO, 'Authorized access');
} else {
echo 'Go away unauthorized user<p />';
syslog(LOG_WARNING, "Unauthorized access");
}
You do not need to call openlog() or closelog() before writing to syslog. Calls to these functions will be ignored.
Log categories: request logs and app logs
There are two categories of log data: request logs and application logs. A request log is written for each request handled by your app, and contains information such as the app ID, HTTP version, and so forth.
Getting log data
The general process of getting logs is as follows:
- Use
fetch()to return an iterator for the request logs. - In each iteration, process each
RequestLogas desired. - Optionally, use
getAppLogs()to get the list of related AppLogs. - If you retrieved the app logs list, for each
AppLogLine, process theAppLogproperty data as desired.
Sample code
The following sample reads request logs between given start and end times and verifies that the given messages and levels are contained sequentially in the app logs. The response will be 'PASS' if all expected messages and levels are matched against app logs.
use google\appengine\api\log\LogService;
// LogService API usage sample to display application logs for last 24 hours.
$options = [
// Fetch last 24 hours of log data
'start_time' => (time() - (24 * 60 * 60)) * 1e6,
// End time is Now
'end_time' => time() * 1e6,
// Include all Application Logs (i.e. your debugging output)
'include_app_logs' => true,
// Filter out log records based on severity
'minimum_log_level' => LogService::LEVEL_INFO,
];
$logs = LogService::fetch($options);
foreach ($logs as $log) {
echo '<ul>REQUEST LOG';
echo '<li>IP: ', $log->getIp(), '</li>',
'<li>Status: ', $log->getStatus(), '</li>',
'<li>Method: ', $log->getMethod(), '</li>',
'<li>Resource: ', $log->getResource(), '</li>';
$end_date_time = $log->getEndDateTime();
echo '<li>Date: ',$end_date_time->format('c'), '</li>';
$app_logs = $log->getAppLogs();
foreach ($app_logs as $app_log) {
echo '<ul>APP LOG';
echo '<li>Message: ', $app_log->getMessage(), '</li>';
$app_log_date_time = $app_log->getDateTime();
echo '<li>Date: ', $app_log_date_time->format('c'), '</li></ul>';
}
echo '</ul>';
}
Log URL formats in App Engine and developer consoles
The log URL format is different in the App Engine Admin Console compared to the
Google Developers console. In particular, the Google Developers console does not
have the filter_type, as can be seen in the following sample log URLs:
App Engine Admin Console format:
https://appengine.google.com/logs?app_id=s~blablas1&filter_type=labels&filter=request_id%000000efdb00ff00ff827e493472570001737e73686966746361727331000168656164000100
Google Developer Console format:
https://console.developers.google.com/project/blablas1/logs?filters=request_id:000000db00ff00ff827e493472570001737e73686966746361727331000168656164000100
How to read a log
To view logs using the Log Viewer:
-
Visit the developer console in your browser.
-
Open the project whose logs you wish to see and select Compute > App Engine > Logs.
-
Use the desired filter to retrieve the logs you want to see. You can filter by various combinations of time, log level, module, and log filter label or regular expression.
Notice that labels are regular expressions for filtering the logs by logging fields. Valid labels include the following:
- day
- month
- year
- hour
- minute
- second
- tzone
- remotehost
- identd_user
- user
- status
- bytes
- referrer
- useragent
- method
- path
- querystring
- protocol
- request_id
For example,
path:/foo.* useragent:.*Chrome.*gets logs for all requests to a path starting with/foothat were issued from a Chrome browser.
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.19
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: 192.0.2.0 |
| 2 | RFC1413 identity | No | RFC1413 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 test@example.com, the nickname that is logged in this field is test. |
| 4 | Timestamp | Yes | Request timestamp. Example: [27/Jun/2014:09:11:47 -0700] |
| 5 | Request querystring | Yes | First line of the request, containing method, path, and HTTP version. Example: GET / HTTP/1.1 |
| 6 | HTTP Status Code | Yes | Returned HTTP status code. Example: 200 |
| 7 | Response size | Yes | Response size in bytes. Example: 414 |
| 8 | Referrer path | No | If there is no referrer, the log contains no path, but only -. Example referrer path: "http://www.example.com/index.html". |
| 9 | User-agent | Yes | Identifies the browser and operating system to the web server. Example: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 |
| 10 | Hostname | Yes | The hostname used by the client to connect to the App Engine application. Example : (1-dot-calm-sylph-602.appspot.com) |
| 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: ms=195. |
| 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: cpu_ms=42 |
| 13 | Exit code | No | Only present if the instance shut down after getting the request. In the format exit_code=XXX where XXX is a 3 digit number corresponding to the reason the instance shut down. The exit codes are not documented since they are primarily intended to help Google spot and fix issues. |
| 14 | Estimated cost | Yes | Estimated cost of 1000 requests just like this one, in USD. Example: cpm_usd=0.000046 |
| 15 | Queue name | No | The name of the task queue used. Only present if request used a task queue. Example: queue_name=default |
| 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: task_name=7287390692361099748 |
| 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: pending_ms=195 |
| 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: loading_request=1. |
| 19 | Instance | Yes | Unique identifier for the instance that handles the request. Example: instance=00c61b117cfeb66f973d7df1b7f4ae1f064d |
| 20 | Version | Yes | The current App Engine release version used in production App Engine: 1.9.19 |
Quotas and limits
Your application is affected by the following logs-related quotas:
- Logs data retrieved via the Logs API.
- Log storage, also called logs 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 storage
You can control how much log data your application stores by means of its log retention settings in the Admin Console. By default, logs are stored for an application free of charge with the following per-application limits: a maximum of 1 gigabyte for a maximum of up to 90 days. If either limit is exceeded, more recent logs will be shown and older logs will be deleted to stay within the size limit. Logs older than the maximum retention time are also deleted.
If your app has billing enabled, you can pay for higher log size limits by specifying the desired maximum log size in gigabytes in the Admin Console. You can also set the retention time by specifying the desired number of days to keep logs, up to a maximum of 365 days. The cost of this extra log storage is $0.026 per gigabyte utilized per month.
| Limit | Amount | Cost past free threshold |
|---|---|---|
| Maximum days storage per log | 90 days free, 365 days if paid | $0.026 per gigabyte utilized per month |
| Maximum total logs storage | 1 gigabyte free, unlimited if paid | $0.026 per gigabyte utilized per month |
