Skip to content

Commit

Permalink
Switch clasp logs from using google-cloud library to googleapis libra…
Browse files Browse the repository at this point in the history
…ry (google#192)

This gets rid of `@google-cloud/logging` in favor of `googleapis`.

There were not many changes to be made, though the response format is a bit different, so parsing the logs had to be changed. Additionally, new scopes are needed for reading the logs. Finally, `@google-cloud/logging` and `grpc` are removed.

Thanks to @grant for finding a simple solution to the `grpc` problem!
 
Signed-off-by: campionfellin <[email protected]>

Fixes google#190 

- [ ] `npm run test` succeeds. (no tests really for `clasp logs`, I manually tested and it seems to work fine.
- [x] `npm run lint` succeeds.
- [ ] Appropriate changes to README are included in PR.
  • Loading branch information
campionfellin authored and grant committed May 28, 2018
1 parent 439772a commit 38b2773
Show file tree
Hide file tree
Showing 5 changed files with 766 additions and 2,863 deletions.
36 changes: 21 additions & 15 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const path = require('path');
const commander = require('commander');
const readMultipleFiles = require('read-multiple-files');
import * as recursive from 'recursive-readdir';
const logging = require('@google-cloud/logging');
import { Logging } from 'googleapis/build/src/apis/logging/v2';
const chalk = require('chalk');
const { prompt } = require('inquirer');
import * as pluralize from 'pluralize';
Expand Down Expand Up @@ -63,6 +63,11 @@ const script = google.script({
auth: oauth2Client,
});

const logger = google.logging({
version: 'v2',
auth: oauth2Client,
}) as Logging;

/**
* Recursively finds all files that are part of the current project, and those that are ignored
* by .claspignore and calls the passed callback function with the file lists.
Expand Down Expand Up @@ -697,22 +702,21 @@ commander
open: boolean,
}) => {
await checkIfOnline();
function printLogs([entries]: any[]) {
for (let i = 0; i < 5; ++i) {
const metadata = entries[i].metadata;
const { severity, timestamp, payload } = metadata;
let functionName = entries[i].metadata.resource.labels.function_name;
function printLogs(entries: any[]) {
for (let i = 0; i < Math.min(50,entries.length); ++i) {
const { severity, timestamp, resource, textPayload, protoPayload, jsonPayload } = entries[i];
let functionName = resource.labels.function_name;
functionName = functionName ? functionName.padEnd(15) : ERROR.NO_FUNCTION_NAME;
let payloadData: any = '';
if (cmd.json) {
payloadData = JSON.stringify(entries[i], null, 2);
} else {
const data: any = {
textPayload: metadata.textPayload,
jsonPayload: metadata.jsonPayload ? metadata.jsonPayload.fields.message.stringValue : '',
protoPayload: metadata.protoPayload,
textPayload,
jsonPayload: jsonPayload ? jsonPayload.fields.message.stringValue : '',
protoPayload,
};
payloadData = data[payload] || ERROR.PAYLOAD_UNKNOWN;
payloadData = data.textPayload || data.jsonPayload || data.protoPayload || ERROR.PAYLOAD_UNKNOWN;
if (payloadData && typeof(payloadData) === 'string') {
payloadData = payloadData.padEnd(20);
}
Expand Down Expand Up @@ -740,11 +744,13 @@ commander
open(url);
process.exit(0);
}
const logger = new logging({
projectId,
});
return logger.getEntries().then(printLogs).catch((err: any) => {
console.error(ERROR.LOGS_UNAVAILABLE);
getAPICredentials(async () => {
const { data } = await logger.entries.list({
resourceNames: [
`projects/${projectId}`,
],
});
printLogs(data.entries);
});
});

Expand Down
Loading

0 comments on commit 38b2773

Please sign in to comment.