-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add logs function * Fix problems with logs * Fix nits * Fix indentation
- Loading branch information
1 parent
72002d5
commit e3c9b9b
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ import * as url from 'url'; | |
const readline = require('readline'); | ||
import * as Promise from 'bluebird'; | ||
import { Server } from "http"; | ||
const logging = require('@google-cloud/logging'); | ||
const chalk = require('chalk'); | ||
|
||
// Debug | ||
const DEBUG = false; | ||
|
@@ -84,6 +86,7 @@ interface ClaspSettings { | |
interface ProjectSettings { | ||
scriptId: string; | ||
rootDir: string; | ||
projectId: string; | ||
} | ||
|
||
// An Apps Script API File | ||
|
@@ -97,6 +100,11 @@ interface LoginOptions { | |
localhost: boolean; | ||
} | ||
|
||
interface LogOptions { | ||
json: boolean; | ||
open: boolean; | ||
} | ||
|
||
// Used to receive files tracked by current project | ||
interface FilesCallback { | ||
( | ||
|
@@ -200,8 +208,13 @@ Forgot ${PROJECT_NAME} commands? Get help:\n ${PROJECT_NAME} --help`, | |
LOGGED_OUT: `\nCommand failed. Please login. (${PROJECT_NAME} login)`, | ||
OFFLINE: 'Error: Looks like you are offline.', | ||
ONE_DEPLOYMENT_CREATE: 'Currently just one deployment can be created at a time.', | ||
NO_FUNCTION_NAME: 'N/A', | ||
NO_GCLOUD_PROJECT: `\nPlease set your projectId in your .clasp.json file to your Google Cloud project ID. \n | ||
You can find your projectId by following the instructions in the README here: \n | ||
https://github.com/google/clasp#get-project-id`, | ||
NO_NESTED_PROJECTS: '\nNested clasp projects are not supported.', | ||
READ_ONLY_DELETE: 'Unable to delete read-only deployment.', | ||
PAYLOAD_UNKNOWN: 'Unknown StackDriver payload.', | ||
PERMISSION_DENIED: `Error: Permission denied. Enable the Apps Script API: | ||
https://script.google.com/home/usersettings`, | ||
SCRIPT_ID: '\n> Did you provide the correct scriptId?\n', | ||
|
@@ -1048,6 +1061,64 @@ commander | |
}); | ||
}); | ||
|
||
/** | ||
* Prints out 5 most recent the StackDriver logs | ||
* Use --json for output in json format | ||
* Use --open to open logs in StackDriver | ||
*/ | ||
commander | ||
.command('logs') | ||
.description('Shows the StackDriver Logs') | ||
.option('--json', "Show logs in JSON form") | ||
.option('--open', 'Open the StackDriver logs in browser') | ||
.action((cmd: LogOptions) => { | ||
function printLogs([entries]) { | ||
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; | ||
functionName = functionName ? functionName.padEnd(15) : ERROR.NO_FUNCTION_NAME; | ||
let payloadData = ''; | ||
if (cmd.json) { | ||
payloadData = JSON.stringify(entries[i], null, 2); | ||
} else { | ||
payloadData = ({ | ||
textPayload: metadata.textPayload, | ||
jsonPayload: metadata.jsonPayload ? metadata.jsonPayload.fields.message.stringValue : '', | ||
protoPayload: metadata.protoPayload | ||
})[payload] || ERROR.PAYLOAD_UNKNOWN; | ||
|
||
if (payloadData && typeof(payloadData) === 'string') { | ||
payloadData = payloadData.padEnd(20); | ||
} | ||
} | ||
let coloredSeverity = ({ | ||
ERROR: chalk.red(severity), | ||
INFO: chalk.blue(severity), | ||
DEBUG: chalk.yellow(severity) | ||
})[severity] || severity; | ||
coloredSeverity = String(coloredSeverity).padEnd(15); | ||
console.log(`${coloredSeverity} ${timestamp} ${functionName} ${payloadData}`); | ||
} | ||
} | ||
|
||
getProjectSettings().then(({ projectId }: ProjectSettings) => { | ||
if (!projectId) { | ||
console.error(ERROR.NO_GCLOUD_PROJECT); | ||
return process.exit(-1); | ||
} | ||
if (cmd.open) { | ||
const stackdriverURL = `https://console.cloud.google.com/logs/viewer?project=${projectId}&resource=app_script_function`; | ||
open(stackdriverURL); | ||
process.exit(0); | ||
} | ||
const logger = new logging({ projectId }); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
campionfellin
Author
Collaborator
|
||
logger.getEntries().then(printLogs); | ||
}); | ||
|
||
}); | ||
|
||
/** | ||
* Displays the help function | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@campionfellin
For actually getting logs, I've never been able to get this to work:
(node:6279) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.
Are you sure this logger setup works?
Did you use
GOOGLE_APPLICATION_CREDENTIALS
?