I've tried several methods to do this, including sending direct API request as mentioned in https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/write, but there are a few problems:
1. I'm able to send data only in the past and not in the future while using API directly - (whereas according to the documentation the timestamp value can be maximum 24 hours in the future)
2. I'm passing the timestamp value while calling the struct_log method and that timestamp is passed internally in the library functions to where API is called, but that doesn't work at all and as a result no log is created.
Hello,
Yes, you are correct. Based on the documentation, incoming log entries must have timestamps that don't exceed the logs retention period in the past, and that don't exceed 24 hours in the future. Log entries outside those time boundaries aren't ingested by Logging. It would be really helpful if you can inform me how you are implementing the struct_log, to see if we can find any issue with the implementation.
Regards.
Hello,
This is the implementation of the struct_log method:
import google.cloud.logging
client = google.cloud.logging.Client(project="project-id")
logger = client.logger(name="cloudLogger")
logger.log_struct(
{'message': "test2"},
timestamp=(datetime.datetime.now() - datetime.timedelta(minutes=3))
)
Here, I've tried to send the data 3 minutes in the past but no error response is shown and no log is created.
Also, while using the API directly, no log in the future is created even if the timestamp is within 24 hours in the future, But a log for past time is created.
Here you have a tested example code for what you want to accomplish. It was tested up to 1400 minutes in the future, and I can see the logs correctly populated with the desired timestamps in Cloud Logging.
import google.cloud.logging
import logging
import datetime
# Instantiates a client
client = google.cloud.logging.Client()
# Configure the default logger to use the logging client
client.setup_logging()
logger = client.logger(name="cloudLogger")
def write_entry():
logger.log_struct(
{
"name": "King Arthur",
"quest": "Find the Holy Grail",
"favorite_color": "Blue",
}
, timestamp=(datetime.datetime.now() + datetime.timedelta(minutes=1400)))
write_entry()
I see you are using the Logging client libraries, so it is not necessary to add the project-id in your code since you need to authenticate using the service account JSON key first.
Hi grobledo, thanks for the solution. I tried to send the logs using your code but still I'm not able to send the logs. Can you please tell me which version of google cloud logging library are you using? Also, does this solution work for past timestamp as well?
hi @simranchanna , would you mind describing how you verify that
no log is created.
Do you run a query in Log Explorer? There is a possibility that you do not see the logs due to invalid query or because Log Explorer does not show future logs by default.
hi @leoy
Yes, I'm using logs explorer to check the logs. It isn't showing logs that are sent using past or future timestamps.
hi @simranchanna ,
A problem might be in the way you query the logs in Log Explorer. For example, to see "future" logs you have to explicitly define time range. Otherwise, all displayed logs will be "top bound" by now time.
Would you mind to describe how you do it?