See #136
Code
requirements.txt:
google-auth==1.27.0
google-cloud-logging==2.2.0
And main.py (derived from the Google published sample)
Using client.list_entries:
importosfromgoogle.cloudimportloggingfromgoogle.cloud.loggingimportDESCENDINGclient=logging.Client()
PROJECT=os.getenv("PROJECT")
resource="projects/{project}".format(project=PROJECT)
log_name="{resource}/logs/cloudaudit.googleapis.com%2Factivity".format(
resource=resource)
filter="logName=\"{log_name}\"".format(log_name=log_name)
entries=client.list_entries(filter_=filter,
order_by=DESCENDING,
page_size=3,
resource_names=[resource])
print("Listing entries for logger {}:".format(log_name))
forentryinentries:
timestamp=entry.timestamp.isoformat()
print("{}".format(timestamp))Using logger.list_entries:
importosfromgoogle.cloudimportloggingfromgoogle.cloud.loggingimportDESCENDINGclient=logging.Client()
log_name="cloudaudit.googleapis.com%2Factivity"logger=client.logger(log_name)
entries=logger.list_entries(
order_by=DESCENDING,
page_size=3,
)
print("Listing entries for logger {}:".format(log_name))
forentryinentries:
timestamp=entry.timestamp.isoformat()
print("{}".format(timestamp))Steps to reproduce
Running the Python:
python3 cloud/main.py Listing entries for logger projects/dazwilkin-210302-66371557/logs/cloudaudit.googleapis.com%2Factivity:
2021-03-02T17:39:09.421835+00:00
2021-03-02T17:39:12.121825+00:00
2021-03-02T17:39:12.933201+00:00
2021-03-02T17:39:16.011850+00:00
2021-03-02T17:39:17.114823+00:00
2021-03-02T17:39:18.699000+00:00
2021-03-02T17:41:29.759709+00:00
2021-03-02T17:41:30.937346+00:00
2021-03-02T17:41:32.653000+00:00
NOTE Neither DESCENDING nor limited
Running the equivalent (!) gcloud yields the expected result:
gcloud logging read"logName=\"projects/${PROJECT}/logs/cloudaudit.googleapis.com%2Factivity\"" \
--project=${PROJECT} \
--order=desc \
--limit=3 \
--format="value(timestamp)" \
--log-http
...
== body start ==
{
"filter": "timestamp>=\"2021-03-01T19:14:21.929877Z\" AND logName=\"projects/${PROJECT{/logs/cloudaudit.googleapis.com%2Factivity\"","orderBy": "timestamp desc","pageSize": 3,"resourceNames": ["projects/${PROJECT}"]}== body end ==...2021-03-02T17:41:32.653Z2021-03-02T17:41:30.937346481Z2021-03-02T17:41:29.759709770ZAdditionally:
- The
list_entries result should be an interator that can be paged; it is not - The library 'encourages' repetition of resource|project (
logging_client.logger, filter and resource_names) and it's unclear how I can only specify this once
See #136
Code
requirements.txt:And
main.py(derived from the Google published sample)Using
client.list_entries:Using
logger.list_entries:Steps to reproduce
Running the Python:
python3 cloud/main.py Listing entries for logger projects/dazwilkin-210302-66371557/logs/cloudaudit.googleapis.com%2Factivity: 2021-03-02T17:39:09.421835+00:00 2021-03-02T17:39:12.121825+00:00 2021-03-02T17:39:12.933201+00:00 2021-03-02T17:39:16.011850+00:00 2021-03-02T17:39:17.114823+00:00 2021-03-02T17:39:18.699000+00:00 2021-03-02T17:41:29.759709+00:00 2021-03-02T17:41:30.937346+00:00 2021-03-02T17:41:32.653000+00:00Running the equivalent (!)
gcloudyields the expected result:Additionally:
list_entriesresult should be an interator that can be paged; it is notlogging_client.logger,filterandresource_names) and it's unclear how I can only specify this once