gc_batch.gcs_logging
Read Google Cloud Batch job logs from a GCS bucket.
When a job is created with --logs-bucket the Batch API writes its logs to a
mounted GCS bucket (LogsPolicy.destination = PATH) instead of Cloud Logging.
This module reads those objects back, which is the only log route available in
environments where the caller cannot read Cloud Logging at all (for example the
All of Us Researcher Workbench, where the workspace pet service account gets
403 Permission denied for all log views).
Classes:
| Name | Description |
|---|---|
GCSLogReader |
Reads and parses Batch log objects out of a GCS bucket. |
GCSLogLocation |
The bucket, prefix and billing project a job's logs live at. |
SEVERITY_ORDER = {'DEFAULT': 0, 'DEBUG': 100, 'INFO': 200, 'NOTICE': 300, 'WARNING': 400, 'ERROR': 500, 'CRITICAL': 600, 'ALERT': 700, 'EMERGENCY': 800}
module-attribute
GCSLogLocation
dataclass
Where a Batch job's logs are stored in GCS.
Attributes:
| Name | Type | Description |
|---|---|---|
bucket |
str
|
The GCS bucket name. |
prefix |
str
|
The object prefix within the bucket (may be empty). |
billing_project |
str | None
|
Billing project for requester-pays buckets, if the job was created with one. |
Source code in src/gc_batch/gcs_logging.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
billing_project = None
class-attribute
instance-attribute
bucket
instance-attribute
prefix
instance-attribute
uri
property
Return the gs:// URI of the log directory.
__init__(bucket, prefix, billing_project=None)
GCSLogReader
Read Google Cloud Batch logs written to a GCS bucket.
The bucket and prefix are recovered from the job resource itself: the logs
bucket is mounted at :attr:Constants.LOGS_MOUNT_POINT, so the volume's
gcs.remote_path is the full bucket/prefix path Batch wrote to. No user
input is needed beyond the job.
Entries are returned in the same list[dict] shape that
BatchLogging._query_cloud_logs produces, so the existing printing and
downloading code paths work unchanged.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
The BatchClientConfig with project settings. |
Example
from gc_batch import BatchClientConfig
from gc_batch.gcs_logging import GCSLogReader
config = BatchClientConfig(project_id="my-project", location="us-central1")
reader = GCSLogReader(config)
job = client.get_job("my-job-1234567890")
reader.print_logs_for_job(job)
Source code in src/gc_batch/gcs_logging.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
config = config
instance-attribute
logging = logger or get_logger(name='GCSLogReader', level=log_level)
instance-attribute
storage_client
property
Return the GCS client, creating it on first access.
__init__(config, storage_client=None, logger=None, log_level=logging.INFO)
Initialize the GCSLogReader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BatchClientConfig
|
Configuration with project_id and location settings. |
required |
storage_client
|
Client | None
|
Optional pre-built GCS client. When omitted, a client is created lazily on first use so that constructing the reader never requires credentials. |
None
|
logger
|
Logger | None
|
Optional logger to use instead of creating one. |
None
|
log_level
|
int
|
Level for the logger created when |
INFO
|
Source code in src/gc_batch/gcs_logging.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
download_logs_for_job(job, download_dir, severity='DEFAULT', include_agent_logs=False)
Download a job's GCS-stored logs into a single local file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The job to download logs for. |
required |
download_dir
|
str
|
Directory to write the log file into. Created if needed. |
required |
severity
|
str
|
Minimum severity level to include. |
'DEFAULT'
|
include_agent_logs
|
bool
|
Whether to read the agent's |
False
|
Returns:
| Type | Description |
|---|---|
int
|
The number of entries written. Zero means the read succeeded but the |
int
|
job produced no matching log entries, and no file is written. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the job does not write logs to GCS, or the severity is not recognised. |
GoogleAPIError
|
If the read fails. |
OSError
|
If the log file cannot be written. |
Source code in src/gc_batch/gcs_logging.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
has_log_objects(job)
Report whether Batch wrote any log objects for the job.
Zero objects for a failed job means Batch never got as far as running the container, which is the signal used to diagnose pre-container failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The job to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the job does not write logs to GCS. |
GoogleAPIError
|
If the listing fails. |
Source code in src/gc_batch/gcs_logging.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
list_log_objects(job)
List the names of the job's Batch log objects in GCS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The job whose log objects should be listed. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted object names, including the prefix. Objects that do not match |
list[str]
|
the Batch log naming scheme are excluded. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the job does not write logs to GCS. |
GoogleAPIError
|
If the listing fails. |
Source code in src/gc_batch/gcs_logging.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
print_logs_for_job(job, severity='DEFAULT', include_agent_logs=False)
Print a job's GCS-stored logs to the console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The job to print logs for. |
required |
severity
|
str
|
Minimum severity level to include. |
'DEFAULT'
|
include_agent_logs
|
bool
|
Whether to read the agent's |
False
|
Returns:
| Type | Description |
|---|---|
int
|
The number of entries printed. Zero means the read succeeded but the |
int
|
job produced no matching log entries. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the job does not write logs to GCS, or the severity is not recognised. |
GoogleAPIError
|
If the read fails. |
Source code in src/gc_batch/gcs_logging.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
read_logs_for_job(job, severity='DEFAULT', include_agent_logs=False)
Read and parse all of a job's log entries from GCS.
Entries are returned in the order Batch wrote them — object order within a
task, tasks in index order, stdout before stderr — and are never
re-sorted by timestamp, because Batch timestamps have only one-second
resolution and re-sorting would scramble same-second output. No limit is
applied: whole objects are read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The job to read logs for. |
required |
severity
|
str
|
Minimum Cloud Logging severity to include ( |
'DEFAULT'
|
include_agent_logs
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
Log entries with |
list[dict]
|
|
list[dict]
|
Logging reader returns. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the job does not write logs to GCS, or |
GoogleAPIError
|
If listing or downloading the log objects fails. |
Source code in src/gc_batch/gcs_logging.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
resolve_log_location(job)
staticmethod
Resolve where a job's logs were written from the job resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The job to resolve the log location for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
GCSLogLocation
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the job has no logs volume, meaning it was not created
with |
Source code in src/gc_batch/gcs_logging.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
uses_gcs_logs(job)
staticmethod
Report whether a job writes its logs to a mounted GCS bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The job to inspect. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
attr: |
Source code in src/gc_batch/gcs_logging.py
180 181 182 183 184 185 186 187 188 189 190 191 | |