This document describes how to enable, generate, and view logs from Cloud Logging for a Batch job.
You can use logs to get information that is useful for analyzing your jobs. For example, logs can help you debug failed jobs.
Notably, logs are only generated after a job starts running and only if logging was enabled for the job. If you need to analyze a job without logs, view status events instead.
Before you begin
- If you haven't used Batch before, review Get started with Batch and enable Batch by completing the prerequisites for projects and users.
-
To get the permissions that you need to analyze a job using logs, ask your administrator to grant you the following IAM roles:
-
To create a job:
-
Batch Job Editor (
roles/batch.jobsEditor
) on the project -
Service Account User (
roles/iam.serviceAccountUser
) on the job's service account, which by default is the default Compute Engine service account
-
Batch Job Editor (
-
To view logs:
Logs Viewer (
roles/logging.viewer
) on the project
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
-
To create a job:
Enable logging for a job
To allow logs to be generated for a job, enable logs from Cloud Logging when you create the job:
- If you create a job using the Google Cloud console, logs from Cloud Logging are always enabled.
If you create a job using the gcloud CLI or the Batch API, logs from Cloud Logging are disabled by default. To enable logs from Cloud Logging, include the following configuration for the
logsPolicy
field while creating the job:{ ... "logsPolicy": { "destination": "CLOUD_LOGGING" } ... }
Write and generate logs for a job
When logs from Cloud Logging are enabled for a job, Cloud Logging automatically generates any of the logs that are written for the job. Specifically, Batch jobs can have the following log types:
agent logs (
batch_agent_logs
): logs for activities from the Batch service agent.Batch automatically writes agent logs for every job that has enabled logging.
task logs (
batch_task_logs
): logs for any data that you've configured a job's runnables to write to the standard output (stdout
) stream or standard error (stderr
) stream.You can optionally write task logs for each job that has enabled logging.
View logs for a job
You can view a job's logs using the Google Cloud console, the gcloud CLI, Logging API, Go, Java, Python, or C++.
Console
To view a job's logs using the Google Cloud console, do the following:
In the Google Cloud console, go to the Job list page.
In the Job name column, click the name of a job. The Job details page opens.
Click the Logs tab. Batch displays all logs associated with the job.
Optional: To filter the logs do any of the following:
Enter a filter in the Filter field.
In the Severity list, select a severity.
Click build a query in the Logs Explorer using the Batch filter parameters.
View in Logs Explorer to
gcloud
To view logs using the gcloud CLI, use the
gcloud logging read
command:
gcloud logging read "QUERY"
where QUERY
is a
query for Batch logs that contains
Batch filter parameters.
API
To view logs using the Logging API, use the
entries.list
method:
POST https://logging.googleapis.com/v2/entries:list
{
"resourceNames": [
"projects/PROJECT_ID"
],
"filter": "QUERY"
"orderBy": "timestamp desc"
}
Replace the following:
PROJECT_ID
: the project ID of your project.QUERY
: a query for Batch logs that contains Batch filter parameters.
Go
Go
For more information, see the Batch Go API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Java
For more information, see the Batch Java API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Python
For more information, see the Batch Python API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C++
C++
For more information, see the Batch C++ API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Filter Batch logs
You can filter for Batch logs by writing a query
that includes one or more of the following filter parameters and zero or more
boolean operators (AND
, OR
and NOT
).
To filter for logs from a specific job, specify the job's unique ID (UID):
labels.job_uid=JOB_UID
where
JOB_UID
is the UID of the job. To get a job's UID, view the details the job.To filter for a specific type of Batch logs, specify the log type:
logName=projects/PROJECT_ID/logs/BATCH_LOG_TYPE
Replace the following:
PROJECT_ID
: the project ID of the project that you want to view logs for.BATCH_LOG_TYPE
: the type of Batch logs you want to view, eitherbatch_task_logs
for task logs orbatch_agent_logs
for agent logs.
To filter for logs with custom status events, specify that the log must define the
jsonPayload.batch/custom/event
field:jsonPayload.batch"/"custom"/"event!=NULL_VALUE
To filter for logs of one or more specific severities, specify the following comparison:
severityCOMPARISON_OPERATORSEVERITY_ENUM
Replace the following:
COMPARISON_OPERATOR
: a comparison operator—for example,>=
.SEVERITY_ENUM
: aLogSeverity
enum, which describes the severity of a log—for example,ERROR
.
For more filter options, see the Cloud Logging query language documentation.
What's next
- Learn more about troubleshooting.
- Learn more about Cloud Logging.
- Learn how to write task logs.
- Learn how to export job information.
- Learn how to delete jobs.