| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8d51267 commit 4fd3cdc
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,24 +1,27 @@ | |||
| 1 | 1 | custom_content: | | |
| 2 | 2 | #### 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 | + | ||
| 10 | 11 | ```java | |
| 11 | 12 | import com.google.cloud.logging.Logging; | |
| 12 | 13 | import com.google.cloud.logging.LoggingOptions; | |
| 13 | - | ||
| 14 | + | ||
| 14 | 15 | LoggingOptions options = LoggingOptions.getDefaultInstance(); | |
| 15 | 16 | try(Logging logging = options.getService()) { | |
| 16 | 17 | // use logging here | |
| 17 | 18 | } | |
| 18 | 19 | ``` | |
| 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. | ||
| 22 | 25 | ||
| 23 | 26 | #### Creating a metric | |
| 24 | 27 | With Logging you can create logs-based metrics. Logs-based metrics allow to keep track of the number | |
@@ -28,6 +31,7 @@ custom_content: | | |||
| 28 | 31 | import com.google.cloud.logging.Metric; | |
| 29 | 32 | import com.google.cloud.logging.MetricInfo; | |
| 30 | 33 | ``` | |
| 34 | + | ||
| 31 | 35 | Then, to create the metric, use the following code: | |
| 32 | 36 | ||
| 33 | 37 | ```java | |
@@ -38,8 +42,10 @@ custom_content: | | |||
| 38 | 42 | ``` | |
| 39 | 43 | ||
| 40 | 44 | #### Writing log entries | |
| 45 | + | ||
| 41 | 46 | With Logging you can also write custom log entries. Add the following imports at the top of your | |
| 42 | 47 | file: | |
| 48 | + | ||
| 43 | 49 | ```java | |
| 44 | 50 | import com.google.cloud.MonitoredResource; | |
| 45 | 51 | import com.google.cloud.logging.LogEntry; | |
@@ -48,7 +54,9 @@ custom_content: | | |||
| 48 | 54 | ||
| 49 | 55 | import java.util.Collections; | |
| 50 | 56 | ``` | |
| 57 | + | ||
| 51 | 58 | Then, to write the log entries, use the following code: | |
| 59 | + | ||
| 52 | 60 | ```java | |
| 53 | 61 | LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message")) | |
| 54 | 62 | .setLogName("test-log") | |
@@ -60,13 +68,16 @@ custom_content: | | |||
| 60 | 68 | ``` | |
| 61 | 69 | ||
| 62 | 70 | #### Listing log entries | |
| 71 | + | ||
| 63 | 72 | With Logging you can also list log entries that have been previously written. Add the following | |
| 64 | 73 | imports at the top of your file: | |
| 74 | + | ||
| 65 | 75 | ```java | |
| 66 | 76 | import com.google.cloud.Page; | |
| 67 | 77 | import com.google.cloud.logging.LogEntry; | |
| 68 | 78 | import com.google.cloud.logging.Logging.EntryListOption; | |
| 69 | 79 | ``` | |
| 80 | + | ||
| 70 | 81 | Then, to list the log entries, use the following code: | |
| 71 | 82 | ||
| 72 | 83 | ``` java | |
@@ -79,22 +90,29 @@ custom_content: | | |||
| 79 | 90 | ``` | |
| 80 | 91 | ||
| 81 | 92 | #### Add a Cloud Logging handler to a logger | |
| 93 | + | ||
| 82 | 94 | You can also register a `LoggingHandler` to a `java.util.logging.Logger` that publishes log entries | |
| 83 | 95 | to Cloud Logging. Given the following logger: | |
| 96 | + | ||
| 84 | 97 | ```java | |
| 85 | 98 | private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName()); | |
| 86 | 99 | ``` | |
| 100 | + | ||
| 87 | 101 | You can register a `LoggingHandler` with the code: | |
| 102 | + | ||
| 88 | 103 | ```java | |
| 89 | 104 | LoggingHandler.addHandler(LOGGER, new LoggingHandler()); | |
| 90 | 105 | ``` | |
| 106 | + | ||
| 91 | 107 | After that, logs generated using `LOGGER` will be also directed to Cloud Logging. | |
| 92 | 108 | ||
| 93 | 109 | Notice that you can also register a `LoggingHandler` via the `logging.properties` configuration | |
| 94 | 110 | file. Adding, for instance, the following line: | |
| 111 | + | ||
| 95 | 112 | ``` | |
| 96 | 113 | com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler | |
| 97 | 114 | ``` | |
| 115 | + | ||
| 98 | 116 | #### Complete source code | |
| 99 | 117 | ||
| 100 | 118 | In | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,7 @@ Java idiomatic client for [Cloud Logging][product-docs]. | |||
| 11 | 11 | ## Quickstart | |
| 12 | 12 | ||
| 13 | 13 | If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file | |
| 14 | + | ||
| 14 | 15 | ```xml | |
| 15 | 16 | <dependencyManagement> | |
| 16 | 17 | <dependencies> | |
@@ -45,17 +46,21 @@ If you are using Maven without BOM, add this to your dependencies: | |||
| 45 | 46 | ``` | |
| 46 | 47 | ||
| 47 | 48 | If you are using Gradle 5.x or later, add this to your dependencies | |
| 49 | + | ||
| 48 | 50 | ```Groovy | |
| 49 | 51 | implementation platform('com.google.cloud:libraries-bom:20.8.0') | |
| 50 | 52 | ||
| 51 | 53 | compile 'com.google.cloud:google-cloud-logging' | |
| 52 | 54 | ``` | |
| 55 | + | ||
| 53 | 56 | If you are using Gradle without BOM, add this to your dependencies | |
| 57 | + | ||
| 54 | 58 | ```Groovy | |
| 55 | 59 | compile 'com.google.cloud:google-cloud-logging:2.3.2' | |
| 56 | 60 | ``` | |
| 57 | 61 | ||
| 58 | 62 | If you are using SBT, add this to your dependencies | |
| 63 | + | ||
| 59 | 64 | ```Scala | |
| 60 | 65 | libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "2.3.2" | |
| 61 | 66 | ``` | |
@@ -64,6 +69,10 @@ libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "2.3.2" | |||
| 64 | 69 | ||
| 65 | 70 | See the [Authentication][authentication] section in the base directory's README. | |
| 66 | 71 | ||
| 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 | + | ||
| 67 | 76 | ## Getting Started | |
| 68 | 77 | ||
| 69 | 78 | ### Prerequisites | |
@@ -81,20 +90,19 @@ to add `google-cloud-logging` as a dependency in your code. | |||
| 81 | 90 | ||
| 82 | 91 | ## About Cloud Logging | |
| 83 | 92 | ||
| 84 | - | ||
| 85 | 93 | [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. | |
| 86 | 94 | ||
| 87 | 95 | See the [Cloud Logging client library docs][javadocs] to learn how to | |
| 88 | 96 | use this Cloud Logging Client Library. | |
| 89 | 97 | ||
| 90 | - | ||
| 91 | 98 | #### 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: | ||
| 98 | 106 | ||
| 99 | 107 | ```java | |
| 100 | 108 | import com.google.cloud.logging.Logging; | |
@@ -106,17 +114,21 @@ try(Logging logging = options.getService()) { | |||
| 106 | 114 | } | |
| 107 | 115 | ``` | |
| 108 | 116 | ||
| 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. | ||
| 111 | 121 | ||
| 112 | 122 | #### Creating a metric | |
| 123 | + | ||
| 113 | 124 | With Logging you can create logs-based metrics. Logs-based metrics allow to keep track of the number | |
| 114 | 125 | of log messages associated to specific events. Add the following imports at the top of your file: | |
| 115 | 126 | ||
| 116 | 127 | ```java | |
| 117 | 128 | import com.google.cloud.logging.Metric; | |
| 118 | 129 | import com.google.cloud.logging.MetricInfo; | |
| 119 | 130 | ``` | |
| 131 | + | ||
| 120 | 132 | Then, to create the metric, use the following code: | |
| 121 | 133 | ||
| 122 | 134 | ```java | |
@@ -127,8 +139,10 @@ logging.create(metricInfo); | |||
| 127 | 139 | ``` | |
| 128 | 140 | ||
| 129 | 141 | #### Writing log entries | |
| 142 | + | ||
| 130 | 143 | With Logging you can also write custom log entries. Add the following imports at the top of your | |
| 131 | 144 | file: | |
| 145 | + | ||
| 132 | 146 | ```java | |
| 133 | 147 | import com.google.cloud.MonitoredResource; | |
| 134 | 148 | import com.google.cloud.logging.LogEntry; | |
@@ -137,7 +151,9 @@ import com.google.cloud.logging.Payload.StringPayload; | |||
| 137 | 151 | ||
| 138 | 152 | import java.util.Collections; | |
| 139 | 153 | ``` | |
| 154 | + | ||
| 140 | 155 | Then, to write the log entries, use the following code: | |
| 156 | + | ||
| 141 | 157 | ```java | |
| 142 | 158 | LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message")) | |
| 143 | 159 | .setLogName("test-log") | |
@@ -149,13 +165,16 @@ logging.write(Collections.singleton(firstEntry)); | |||
| 149 | 165 | ``` | |
| 150 | 166 | ||
| 151 | 167 | #### Listing log entries | |
| 168 | + | ||
| 152 | 169 | With Logging you can also list log entries that have been previously written. Add the following | |
| 153 | 170 | imports at the top of your file: | |
| 171 | + | ||
| 154 | 172 | ```java | |
| 155 | 173 | import com.google.cloud.Page; | |
| 156 | 174 | import com.google.cloud.logging.LogEntry; | |
| 157 | 175 | import com.google.cloud.logging.Logging.EntryListOption; | |
| 158 | 176 | ``` | |
| 177 | + | ||
| 159 | 178 | Then, to list the log entries, use the following code: | |
| 160 | 179 | ||
| 161 | 180 | ``` java | |
@@ -168,22 +187,29 @@ while (entryIterator.hasNext()) { | |||
| 168 | 187 | ``` | |
| 169 | 188 | ||
| 170 | 189 | #### Add a Cloud Logging handler to a logger | |
| 190 | + | ||
| 171 | 191 | You can also register a `LoggingHandler` to a `java.util.logging.Logger` that publishes log entries | |
| 172 | 192 | to Cloud Logging. Given the following logger: | |
| 193 | + | ||
| 173 | 194 | ```java | |
| 174 | 195 | private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName()); | |
| 175 | 196 | ``` | |
| 197 | + | ||
| 176 | 198 | You can register a `LoggingHandler` with the code: | |
| 199 | + | ||
| 177 | 200 | ```java | |
| 178 | 201 | LoggingHandler.addHandler(LOGGER, new LoggingHandler()); | |
| 179 | 202 | ``` | |
| 203 | + | ||
| 180 | 204 | After that, logs generated using `LOGGER` will be also directed to Cloud Logging. | |
| 181 | 205 | ||
| 182 | 206 | Notice that you can also register a `LoggingHandler` via the `logging.properties` configuration | |
| 183 | 207 | file. Adding, for instance, the following line: | |
| 184 | - ``` | ||
| 208 | + | ||
| 209 | + ```java | ||
| 185 | 210 | com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler | |
| 186 | 211 | ``` | |
| 212 | + | ||
| 187 | 213 | #### Complete source code | |
| 188 | 214 | ||
| 189 | 215 | In | |
@@ -194,8 +220,6 @@ and | |||
| 194 | 220 | we put together all the code shown above into three programs. The programs assume that you are | |
| 195 | 221 | running on Compute Engine or from your own desktop. | |
| 196 | 222 | ||
| 197 | - | ||
| 198 | - | ||
| 199 | 223 | ## Samples | |
| 200 | 224 | ||
| 201 | 225 | 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. | |||
| 210 | 234 | | 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) | | |
| 211 | 235 | | 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) | | |
| 212 | 236 | ||
| 213 | - | ||
| 214 | - | ||
| 215 | 237 | ## Troubleshooting | |
| 216 | 238 | ||
| 217 | 239 | To get help, follow the instructions in the [shared Troubleshooting document][troubleshooting]. | |
@@ -266,13 +288,10 @@ and on [google-cloud-java][g-c-j]. | |||
| 266 | 288 | ||
| 267 | 289 | ## Versioning | |
| 268 | 290 | ||
| 269 | - | ||
| 270 | 291 | This library follows [Semantic Versioning](http://semver.org/). | |
| 271 | 292 | ||
| 272 | - | ||
| 273 | 293 | ## Contributing | |
| 274 | 294 | ||
| 275 | - | ||
| 276 | 295 | Contributions to this library are always welcome and highly encouraged. | |
| 277 | 296 | ||
| 278 | 297 | See [CONTRIBUTING][contributing] for more information how to get started. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments