Skip to content

Commit f0fc663

Browse files
docs(samples): Adding code samples for log reading (#56)
* docs(samples): Adding code samples for log reading Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 88a0f4f commit f0fc663

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

compute/batch/requirements-test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pytest==7.2.0
22
google-cloud-compute==1.6.1
33
google-cloud-resource-manager==1.6.3
4-
google-cloud-storage==2.5.0
4+
google-cloud-storage==2.5.0

compute/batch/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
isort==5.10.1
22
black==22.10.0
33
google-cloud-batch==0.4.0
4+
google-cloud-logging==3.2.5
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START batch_job_logs]
17+
from typing import NoReturn
18+
19+
from google.cloud import batch_v1
20+
from google.cloud import logging
21+
22+
23+
def print_job_logs(project_id: str, job: batch_v1.Job) -> NoReturn:
24+
"""
25+
Prints the log messages created by given job.
26+
27+
Args:
28+
project_id: name of the project hosting the job.
29+
job: the job which logs you want to print.
30+
"""
31+
# Initialize client that will be used to send requests across threads. This
32+
# client only needs to be created once, and can be reused for multiple requests.
33+
log_client = logging.Client(project=project_id)
34+
logger = log_client.logger("batch_task_logs")
35+
36+
for log_entry in logger.list_entries(filter_=f"labels.job_uid={job.uid}"):
37+
print(log_entry.payload)
38+
39+
# [END batch_job_logs]

compute/batch/snippets/tests/test_basics.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ..get.get_task import get_task
2828
from ..list.list_jobs import list_jobs
2929
from ..list.list_tasks import list_tasks
30+
from ..logs.read_job_logs import print_job_logs
3031

3132
PROJECT = google.auth.default()[1]
3233
REGION = 'europe-north1'
@@ -82,11 +83,18 @@ def _check_tasks(job_name):
8283
print('Tasks tested')
8384

8485

85-
def test_script_job(job_name):
86+
def _check_logs(job, capsys):
87+
print_job_logs(PROJECT, job)
88+
output = [line for line in capsys.readouterr().out.splitlines(keepends=False) if line != ""]
89+
assert len(output) == 4
90+
assert all(log_msg.startswith("STDOUT") for log_msg in output)
91+
92+
93+
def test_script_job(job_name, capsys):
8694
job = create_script_job(PROJECT, REGION, job_name)
87-
_test_body(job, additional_test=lambda: _check_tasks(job_name))
95+
_test_body(job, additional_test=lambda: _check_logs(job, capsys))
8896

8997

9098
def test_container_job(job_name):
9199
job = create_container_job(PROJECT, REGION, job_name)
92-
_test_body(job)
100+
_test_body(job, additional_test=lambda: _check_tasks(job_name))

0 commit comments

Comments
 (0)