Skip to content

Commit 0d6226a

Browse files
committed
feat: Add functions_log_stackdriver
1 parent c9aab34 commit 0d6226a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
// [START functions_log_stackdriver]
18+
import java.io.IOException;
19+
import java.util.Base64;
20+
import java.util.Map;
21+
import java.util.logging.Logger;
22+
23+
public class LogEntry {
24+
25+
private static Logger LOGGER = Logger.getLogger(LogEntry.class.getName());
26+
27+
public void helloPubSub(PubSubMessage message) throws IOException {
28+
String data = new String(Base64.getDecoder().decode(message.data.getBytes("UTF-8")));
29+
if (data.isEmpty()) {
30+
data = "World";
31+
}
32+
LOGGER.info(String.format("Hello, %s", data));
33+
}
34+
}
35+
36+
class PubSubMessage {
37+
String data;
38+
Map<String, String> attributes;
39+
String messageId;
40+
String publishTime;
41+
}
42+
// [END functions_log_stackdriver]

functions/snippets/src/test/java/SnippetsTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ public void filesTest() throws IOException {
179179
assertThat(responseOut.toString(), containsString("Files:"));
180180
}
181181

182+
@Test
183+
public void logEntry() throws IOException {
184+
PubSubMessage message = new PubSubMessage();
185+
message.data = "data";
186+
message.messageId = "id";
187+
new LogEntry().helloPubSub(message);
188+
assertThat(responseOut.toString(), containsString(""));
189+
}
190+
182191
@Test
183192
public void envTest() throws IOException {
184193
environmentVariables.set("FOO", "BAR");

0 commit comments

Comments
 (0)