GeezerVison is showing github.com — Reading Stack Driver Logging - java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment · Issue #2379 · googleapis/google-cloud-java
Reading Stack Driver Logging - java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment #2379DescriptionHi,
I am trying to read logs from Stack Driver Logging. I have logged into gccloud.
Below is my program to read:
`public class App
{
Logging logging = LoggingOptions.getDefaultInstance().getService();
public static void main( String[] args ){
System.out.println( "Hello World!" );
App obj = new App();
obj.run();
}
private void run() {
try {
LoggingOptions.Builder optionsBuilder = LoggingOptions.newBuilder();
logging = optionsBuilder.build().getService();
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter("logName=projects/<<PROJECT_ID>>/logs/<<LOGNAME>>"));
for(LogEntry entry :entries.iterateAll()) {
System.out.println(entry.toString());
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
}`
Getting below exception:
Hello World! Exception in thread "main" java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment. Please set a project ID using the builder. at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122) at com.google.cloud.ServiceOptions.<init>(ServiceOptions.java:240) at com.google.cloud.logging.LoggingOptions.<init>(LoggingOptions.java:102) at com.google.cloud.logging.LoggingOptions$Builder.build(LoggingOptions.java:96) at com.google.cloud.logging.LoggingOptions.getDefaultInstance(LoggingOptions.java:55)
Though Project Id is passed, still i am getting the above exception. Please suggest. Thanks Activitymichaelbausor commented on Aug 24, 2017 This is happening because you are not setting the project ID on the LoggingOptions.Builder, and there is no default project id set in your environment.
Try replacing the line creating the logging object with this:
logging = optionsBuilder.setProjectId("<<PROJECT_ID>>").build().getService();NagarajP commented on Aug 25, 2017 Thanks @michaelbausor . This problem is solved now and ending up other issue(io.grpc.StatusRuntimeException: PERMISSION_DENIED: The request is missing a valid API key) . You want me to create to new issue for this problem?
My updated Code is:
`private void run() {
try {
LoggingOptions.Builder optionsBuilder = LoggingOptions.newBuilder();
System.out.println("optionsBuilder:::"+optionsBuilder);
Logging logging = optionsBuilder.setProjectId("<<PROJECT_ID>>").build().getService();
System.out.println("logging:::"+logging);
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter("logName=projects/<<PROJECT_ID>>/logs/<<LOG NAME>>"));
System.out.println("entries:::"+entries);
for(LogEntry entry :entries.iterateAll()) {
System.out.println(entry.toString());
}
}catch(Exception ex) {
ex.printStackTrace();
}
}`
michaelbausor commented on Aug 25, 2017 @NagarajP We can try to solve it here, as it looks related.
Have you tried running the following gcloud command:
gcloud auth application-default login
This will create the application default credentials file that will be used. If you have done this and are still getting the same error, I would check to make sure that whichever user account you are using has permission to write to the project you have set.
Another option you could try is to create service account credentials for your project, and use them instead of the application default credentials. NagarajP commented on Aug 25, 2017 Hi @michaelbausor , Thanks for your input. Could you please tell me what is the exact permission( or role/policy/access) i needed, so that i can read ( and request to admin to tag with my account)Stack Driver logging using my corporate account? michaelbausor commented on Aug 25, 2017 Looking at the available roles under Logging, there are three that you might need, depending on what you need to do:
- Logs viewer (you will definitely need this)
- Private logs viewer (if you need to view logs containing private content)
- Logs writer (if you need to write logs)
NagarajP commented on Aug 29, 2017 Thanks @michaelbausor . This issue got resolved now after associating proper permissions. Hence i will close this issue.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|