Skip to content

Commit 4fd3cdc

Browse files
authored
fix: describe authorization required for service object (#582)
* fix: adds authorization description Adds description of the authorization requirements for a service object. Fixes markdown warnings in the document. Fixes #570 * Improve description and update autogen template fix the wording for "creating an authorized service object" section update the readme template so the next autgen event will keep changes in * make authorization section match synthool template update the authorization section to make it more generic following other blocks in the synthool template.
1 parent 8d51267 commit 4fd3cdc

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

.readme-partials.yaml

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
custom_content: |
22
#### Creating an authorized service object
3-
To make authenticated requests to Cloud Logging, you must create a service object with
4-
credentials. You can then make API calls by calling methods on the Logging service object. The
5-
simplest way to authenticate is to use
6-
[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
7-
These credentials are automatically inferred from your environment, so you only need the following
8-
code to create your service object:
9-
3+
4+
To make requests to Cloud Logging, you must create a service object with valid credentials.
5+
You can then make API calls by calling methods on the Logging service object.
6+
You can obtain credentials by using [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
7+
Or you can use a [Service Account](https://cloud.google.com/iam/docs/service-accounts) which is a recommended way to obtain credentials.
8+
The credentials can be automatically inferred from your [environment](https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable).
9+
Then you only need the following code to create your service object:
10+
1011
```java
1112
import com.google.cloud.logging.Logging;
1213
import com.google.cloud.logging.LoggingOptions;
13-
14+
1415
LoggingOptions options = LoggingOptions.getDefaultInstance();
1516
try(Logging logging = options.getService()) {
1617
// use logging here
1718
}
1819
```
19-
20-
For other authentication options, see the
21-
[Authentication](https://github.com/googleapis/google-cloud-java#authentication) page.
20+
21+
For other options, see the [Authentication](https://github.com/googleapis/google-cloud-java#authentication) page.
22+
The service object should be granted permissions to make API calls.
23+
Each API call describes the permissions under Authorized Scopes section.
24+
See [Logging API](https://cloud.google.com/logging/docs/reference/v2/rest) to find the required list of permissions or consult with [Access control guide](https://cloud.google.com/logging/docs/access-control) for predefined IAM roles that can be granted to the Logging service object.
2225
2326
#### Creating a metric
2427
With Logging you can create logs-based metrics. Logs-based metrics allow to keep track of the number
@@ -28,6 +31,7 @@ custom_content: |
2831
import com.google.cloud.logging.Metric;
2932
import com.google.cloud.logging.MetricInfo;
3033
```
34+
3135
Then, to create the metric, use the following code:
3236
3337
```java
@@ -38,8 +42,10 @@ custom_content: |
3842
```
3943
4044
#### Writing log entries
45+
4146
With Logging you can also write custom log entries. Add the following imports at the top of your
4247
file:
48+
4349
```java
4450
import com.google.cloud.MonitoredResource;
4551
import com.google.cloud.logging.LogEntry;
@@ -48,7 +54,9 @@ custom_content: |
4854
4955
import java.util.Collections;
5056
```
57+
5158
Then, to write the log entries, use the following code:
59+
5260
```java
5361
LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message"))
5462
.setLogName("test-log")
@@ -60,13 +68,16 @@ custom_content: |
6068
```
6169
6270
#### Listing log entries
71+
6372
With Logging you can also list log entries that have been previously written. Add the following
6473
imports at the top of your file:
74+
6575
```java
6676
import com.google.cloud.Page;
6777
import com.google.cloud.logging.LogEntry;
6878
import com.google.cloud.logging.Logging.EntryListOption;
6979
```
80+
7081
Then, to list the log entries, use the following code:
7182
7283
``` java
@@ -79,22 +90,29 @@ custom_content: |
7990
```
8091
8192
#### Add a Cloud Logging handler to a logger
93+
8294
You can also register a `LoggingHandler` to a `java.util.logging.Logger` that publishes log entries
8395
to Cloud Logging. Given the following logger:
96+
8497
```java
8598
private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName());
8699
```
100+
87101
You can register a `LoggingHandler` with the code:
102+
88103
```java
89104
LoggingHandler.addHandler(LOGGER, new LoggingHandler());
90105
```
106+
91107
After that, logs generated using `LOGGER` will be also directed to Cloud Logging.
92108
93109
Notice that you can also register a `LoggingHandler` via the `logging.properties` configuration
94110
file. Adding, for instance, the following line:
111+
95112
```
96113
com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler
97114
```
115+
98116
#### Complete source code
99117
100118
In

README.md

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Java idiomatic client for [Cloud Logging][product-docs].
1111
## Quickstart
1212

1313
If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file
14+
1415
```xml
1516
<dependencyManagement>
1617
<dependencies>
@@ -45,17 +46,21 @@ If you are using Maven without BOM, add this to your dependencies:
4546
```
4647

4748
If you are using Gradle 5.x or later, add this to your dependencies
49+
4850
```Groovy
4951
implementation platform('com.google.cloud:libraries-bom:20.8.0')
5052
5153
compile 'com.google.cloud:google-cloud-logging'
5254
```
55+
5356
If you are using Gradle without BOM, add this to your dependencies
57+
5458
```Groovy
5559
compile 'com.google.cloud:google-cloud-logging:2.3.2'
5660
```
5761

5862
If you are using SBT, add this to your dependencies
63+
5964
```Scala
6065
libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "2.3.2"
6166
```
@@ -64,6 +69,10 @@ libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "2.3.2"
6469

6570
See the [Authentication][authentication] section in the base directory's README.
6671

72+
## Authorization
73+
74+
A GCP account credentials that are used in API calls should be granted [Authorization Scopes](https://developers.google.com/identity/protocols/oauth2/scopes) to call these APIs. You can also review existing [predefined IAM roles](https://cloud.google.com/iam/docs/understanding-roles#predefined_roles) that can be granted to GCP account which is used for authentication.
75+
6776
## Getting Started
6877

6978
### Prerequisites
@@ -81,20 +90,19 @@ to add `google-cloud-logging` as a dependency in your code.
8190

8291
## About Cloud Logging
8392

84-
8593
[Cloud Logging][product-docs] allows you to store, search, analyze, monitor, and alert on log data and events from Google Cloud and Amazon Web Services. Using the BindPlane service, you can also collect this data from over 150 common application components, on-premises systems, and hybrid cloud systems. BindPlane is included with your Google Cloud project at no additional cost.
8694

8795
See the [Cloud Logging client library docs][javadocs] to learn how to
8896
use this Cloud Logging Client Library.
8997

90-
9198
#### Creating an authorized service object
92-
To make authenticated requests to Cloud Logging, you must create a service object with
93-
credentials. You can then make API calls by calling methods on the Logging service object. The
94-
simplest way to authenticate is to use
95-
[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
96-
These credentials are automatically inferred from your environment, so you only need the following
97-
code to create your service object:
99+
100+
To make requests to Cloud Logging, you must create a service object with valid credentials.
101+
You can then make API calls by calling methods on the Logging service object.
102+
You can obtain credentials by using [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
103+
Or you can use a [Service Account](https://cloud.google.com/iam/docs/service-accounts) which is a recommended way to obtain credentials.
104+
The credentials can be automatically inferred from your [environment](https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable).
105+
Then you only need the following code to create your service object:
98106

99107
```java
100108
import com.google.cloud.logging.Logging;
@@ -106,17 +114,21 @@ try(Logging logging = options.getService()) {
106114
}
107115
```
108116

109-
For other authentication options, see the
110-
[Authentication](https://github.com/googleapis/google-cloud-java#authentication) page.
117+
For other options, see the [Authentication](https://github.com/googleapis/google-cloud-java#authentication) page.
118+
The service object should be granted permissions to make API calls.
119+
Each API call describes the permissions under Authorized Scopes section.
120+
See [Logging API](https://cloud.google.com/logging/docs/reference/v2/rest) to find the required list of permissions or consult with [Access control guide](https://cloud.google.com/logging/docs/access-control) for predefined IAM roles that can be granted to the Logging service object.
111121

112122
#### Creating a metric
123+
113124
With Logging you can create logs-based metrics. Logs-based metrics allow to keep track of the number
114125
of log messages associated to specific events. Add the following imports at the top of your file:
115126

116127
```java
117128
import com.google.cloud.logging.Metric;
118129
import com.google.cloud.logging.MetricInfo;
119130
```
131+
120132
Then, to create the metric, use the following code:
121133

122134
```java
@@ -127,8 +139,10 @@ logging.create(metricInfo);
127139
```
128140

129141
#### Writing log entries
142+
130143
With Logging you can also write custom log entries. Add the following imports at the top of your
131144
file:
145+
132146
```java
133147
import com.google.cloud.MonitoredResource;
134148
import com.google.cloud.logging.LogEntry;
@@ -137,7 +151,9 @@ import com.google.cloud.logging.Payload.StringPayload;
137151

138152
import java.util.Collections;
139153
```
154+
140155
Then, to write the log entries, use the following code:
156+
141157
```java
142158
LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message"))
143159
.setLogName("test-log")
@@ -149,13 +165,16 @@ logging.write(Collections.singleton(firstEntry));
149165
```
150166

151167
#### Listing log entries
168+
152169
With Logging you can also list log entries that have been previously written. Add the following
153170
imports at the top of your file:
171+
154172
```java
155173
import com.google.cloud.Page;
156174
import com.google.cloud.logging.LogEntry;
157175
import com.google.cloud.logging.Logging.EntryListOption;
158176
```
177+
159178
Then, to list the log entries, use the following code:
160179

161180
``` java
@@ -168,22 +187,29 @@ while (entryIterator.hasNext()) {
168187
```
169188

170189
#### Add a Cloud Logging handler to a logger
190+
171191
You can also register a `LoggingHandler` to a `java.util.logging.Logger` that publishes log entries
172192
to Cloud Logging. Given the following logger:
193+
173194
```java
174195
private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName());
175196
```
197+
176198
You can register a `LoggingHandler` with the code:
199+
177200
```java
178201
LoggingHandler.addHandler(LOGGER, new LoggingHandler());
179202
```
203+
180204
After that, logs generated using `LOGGER` will be also directed to Cloud Logging.
181205

182206
Notice that you can also register a `LoggingHandler` via the `logging.properties` configuration
183207
file. Adding, for instance, the following line:
184-
```
208+
209+
```java
185210
com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler
186211
```
212+
187213
#### Complete source code
188214

189215
In
@@ -194,8 +220,6 @@ and
194220
we put together all the code shown above into three programs. The programs assume that you are
195221
running on Compute Engine or from your own desktop.
196222

197-
198-
199223
## Samples
200224

201225
Samples are in the [`samples/`](https://github.com/googleapis/java-logging/tree/master/samples) directory. The samples' `README.md`
@@ -210,8 +234,6 @@ has instructions for running the samples.
210234
| Quickstart | [source code](https://github.com/googleapis/java-logging/blob/master/samples/snippets/src/main/java/com/example/logging/jul/Quickstart.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-logging&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/logging/jul/Quickstart.java) |
211235
| Example Enhancer | [source code](https://github.com/googleapis/java-logging/blob/master/samples/snippets/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-logging&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java) |
212236

213-
214-
215237
## Troubleshooting
216238

217239
To get help, follow the instructions in the [shared Troubleshooting document][troubleshooting].
@@ -266,13 +288,10 @@ and on [google-cloud-java][g-c-j].
266288

267289
## Versioning
268290

269-
270291
This library follows [Semantic Versioning](http://semver.org/).
271292

272-
273293
## Contributing
274294

275-
276295
Contributions to this library are always welcome and highly encouraged.
277296

278297
See [CONTRIBUTING][contributing] for more information how to get started.

0 commit comments

Comments
 (0)