Skip to content

Commit 606c9b9

Browse files
authored
adding list logs snippet (GoogleCloudPlatform#830)
1 parent 7071817 commit 606c9b9

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.logging;
18+
19+
import com.google.api.gax.paging.Page;
20+
import com.google.cloud.logging.LogEntry;
21+
import com.google.cloud.logging.Logging;
22+
import com.google.cloud.logging.Logging.EntryListOption;
23+
import com.google.cloud.logging.LoggingOptions;
24+
25+
/**
26+
* List logs programmatically using the StackDriver Logging API.
27+
*/
28+
public class ListLogs {
29+
30+
/** Expects an existing Stackdriver log name as an argument. */
31+
public static void main(String... args) throws Exception {
32+
// [START listlogs]
33+
// Instantiates a client
34+
LoggingOptions options = LoggingOptions.getDefaultInstance();
35+
36+
String logName = args[0];
37+
38+
try (Logging logging = options.getService()) {
39+
40+
String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName;
41+
42+
// List log entries
43+
Page<LogEntry> entries = logging.listLogEntries(
44+
EntryListOption.filter(logFilter));
45+
for (LogEntry logEntry : entries.iterateAll()) {
46+
System.out.println(logEntry);
47+
}
48+
// Use entries.getNextPage() to paginate
49+
}
50+
// [END listlogs]
51+
}
52+
}

logging/jul/src/main/resources/logging.properties

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ com.google.cloud.logging.LoggingHandler.level=FINE
2020
# default : ERROR
2121
com.google.cloud.logging.LoggingHandler.flushLevel=ERROR
2222

23+
# default : auto-detected, fallback "global"
24+
com.google.cloud.logging.LoggingHandler.resourceType=container
25+
2326
# custom formatter
2427
com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter
2528
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s

logging/logback/src/main/resources/logback.xml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<level>INFO</level>
2222
</filter>
2323
<log>application.log</log> <!-- Optional : default java.log -->
24+
<resourceType>gae_app</resourceType> <!-- Optional : default: auto-detected, fallback: global -->
2425
<enhancer>com.example.logging.logback.enhancers.ExampleEnhancer</enhancer> <!-- Optional -->
2526
<flushLevel>WARN</flushLevel> <!-- Optional : default ERROR -->
2627
</appender>

0 commit comments

Comments
 (0)