From 95f0e835f1a0169560034a7ac3f414aebd1506e8 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Tue, 6 Jun 2023 14:54:22 -0400 Subject: [PATCH] 20613: Start PySDK logging module with existing constants Arvados-DCO-1.1-Signed-off-by: Brett Smith --- sdk/python/arvados/__init__.py | 17 +++++++++-------- sdk/python/arvados/logging.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 sdk/python/arvados/logging.py diff --git a/sdk/python/arvados/__init__.py b/sdk/python/arvados/__init__.py index c8c7029807..39fdb11003 100644 --- a/sdk/python/arvados/__init__.py +++ b/sdk/python/arvados/__init__.py @@ -13,7 +13,7 @@ import hashlib import http.client import httplib2 import json -import logging +import logging as stdliblog import os import pprint import re @@ -33,20 +33,21 @@ from .collection import CollectionReader, CollectionWriter, ResumableCollectionW from arvados.keep import * from arvados.stream import * from .arvfile import StreamFileReader +from .logging import log_format, log_date_format, log_handler from .retry import RetryLoop import arvados.errors as errors import arvados.util as util +# Override logging module pulled in via `from ... import *` +# so users can `import arvados.logging`. +logging = sys.modules['arvados.logging'] + # Set up Arvados logging based on the user's configuration. # All Arvados code should log under the arvados hierarchy. -log_format = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s' -log_date_format = '%Y-%m-%d %H:%M:%S' -log_handler = logging.StreamHandler() -log_handler.setFormatter(logging.Formatter(log_format, log_date_format)) -logger = logging.getLogger('arvados') +logger = stdliblog.getLogger('arvados') logger.addHandler(log_handler) -logger.setLevel(logging.DEBUG if config.get('ARVADOS_DEBUG') - else logging.WARNING) +logger.setLevel(stdliblog.DEBUG if config.get('ARVADOS_DEBUG') + else stdliblog.WARNING) def task_set_output(self, s, num_retries=5): for tries_left in RetryLoop(num_retries=num_retries, backoff_start=0): diff --git a/sdk/python/arvados/logging.py b/sdk/python/arvados/logging.py new file mode 100644 index 0000000000..41f64b95f9 --- /dev/null +++ b/sdk/python/arvados/logging.py @@ -0,0 +1,11 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +"""Logging utilities for Arvados clients""" + +import logging + +log_format = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s' +log_date_format = '%Y-%m-%d %H:%M:%S' +log_handler = logging.StreamHandler() +log_handler.setFormatter(logging.Formatter(log_format, log_date_format)) -- 2.30.2