Skip to content

Commit

Permalink
Read log entries from jsonPayload and protoPayload. (firebase#3539)
Browse files Browse the repository at this point in the history
Today, function logs with structured data (e.g. those generated using firebase-functions SDK's logger api) do not show up in `firebase functions:log` command:

```js
// index.js
exports.hello = functions.https.onRequest((request, response) => {
  info("Hello logs!", {structuredData: true});
  info("hello world!");
  response.send("Hello from Firebase!");
});
```

```bash
$ firebase functions:log
2021-06-30T12:47:42.955491814Z D hello: Function execution started
2021-06-30T12:47:43.050Z I hello: # LOOK HERE: where's the message?
2021-06-30T12:47:43.050Z I hello: hello world!
2021-06-30T12:47:43.061418239Z D hello: Function execution took 107 ms, finished with status code: 200
```

This happens because structured log entries are stored in [`jsonPayload` field](https://cloud.google.com/logging/docs/structured-logging), not `textPayload` field.

In addition to correctly parsing `jsonPayload` field, we will also correctly parse log entries with `protoPayload` fields (in most cases, these log entries come from audit logs).

*After*
```bash
2021-06-30T12:47:42.955491814Z D hello: Function execution started
2021-06-30T12:47:43.050Z I hello: {"message":"Hello logs!","structuredData":true}
2021-06-30T12:47:43.050Z I hello: hello world!
2021-06-30T12:47:43.061418239Z D hello: Function execution took 107 ms, finished with status code: 200
```
  • Loading branch information
taeold authored and devpeerapong committed Dec 14, 2021
1 parent 43a0231 commit 7433427
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Firestore Emulator UI now supports deleting documents and collections recursively.
- Fixes some Storage Emulator UI errors.
- Fixes some issues when using Emulator UI on a different device.
- Fixes issues where functions:log command did not showing some log entries. (#3539)
5 changes: 4 additions & 1 deletion src/commands/functions-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ module.exports = new Command("functions:log")
entry.timestamp,
_.get(entry, "severity", "?").substring(0, 1),
_.get(entry, "resource.labels.function_name") + ":",
_.get(entry, "textPayload", "")
entry.textPayload ||
JSON.stringify(entry.jsonPayload) ||
JSON.stringify(entry.protoPayload) ||
""
);
}
if (_.isEmpty(entries)) {
Expand Down

0 comments on commit 7433427

Please sign in to comment.