Skip to content

Update project logs #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 7, 2021
2 changes: 1 addition & 1 deletion packages/cli/commands/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const endpointLog = async (accountId, options) => {
};

exports.command = 'logs [endpoint]';
exports.describe = 'get logs for a function';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this logs command only work for project functions now?

exports.describe = 'get logs for a function within a project';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this only used to get logs for functions within a project? What about functions outside of projects?


exports.handler = async options => {
await loadAndValidateOptions(options);
Expand Down
91 changes: 49 additions & 42 deletions packages/cli/commands/project/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ const {
getProjectAppFunctionLogs,
getLatestProjectAppFunctionLog,
} = require('@hubspot/cli-lib/api/functions');
const {
getFunctionLogs,
getLatestFunctionLog,
} = require('@hubspot/cli-lib/api/results');
const { getProjectConfig } = require('../../lib/projects');
const { loadAndValidateOptions } = require('../../lib/validation');
const { tailLogs } = require('../../lib/serverlessLogs');

const handleLogsError = (e, accountId, projectName, appPath, functionName) => {
if (e.statusCode === 404) {
logger.error(
`No logs were found for the function name '${functionName}' in the app path '${appPath}' within the project '${projectName}' in account ${accountId}.`
appPath
? `No logs were found for the function name '${functionName}' in the app path '${appPath}' within the project '${projectName}' in account ${accountId}.`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to rephrase this? It's four prepositional phrases in a row, I'm getting a little lost. Perhaps we could consult Erin.

: `No logs were found for the function name '${functionName}' within the project '${projectName}' in account ${accountId}.`
);
}
};

const appFunctionLog = async (accountId, options) => {
const functionLog = async (accountId, options) => {
const {
latest,
follow,
Expand All @@ -37,28 +43,40 @@ const appFunctionLog = async (accountId, options) => {

let logsResp;

const tailCall = async after => {
try {
return appPath
? getProjectAppFunctionLogs(
accountId,
functionName,
projectName,
appPath,
{
after,
}
)
: getFunctionLogs(accountId, functionName, { after });
} catch (e) {
handleLogsError(e, accountId, projectName, appPath, functionName);
}
};
const fetchLatest = async () => {
return appPath
? getLatestProjectAppFunctionLog(
accountId,
functionName,
projectName,
appPath
)
: getLatestFunctionLog(accountId, functionName);
};

if (follow) {
const spinnies = new Spinnies();

spinnies.add('tailLogs', {
text: `Waiting for log entries for '${functionName}' on account '${accountId}'.\n`,
});
const tailCall = after =>
getProjectAppFunctionLogs(accountId, functionName, projectName, appPath, {
after,
});
const fetchLatest = () => {
try {
return getLatestProjectAppFunctionLog(
accountId,
functionName,
projectName,
appPath
);
} catch (e) {
handleLogsError(e, accountId, projectName, appPath, functionName);
}
};

await tailLogs({
accountId,
Expand All @@ -69,24 +87,13 @@ const appFunctionLog = async (accountId, options) => {
});
} else if (latest) {
try {
logsResp = await getLatestProjectAppFunctionLog(
accountId,
functionName,
projectName,
appPath
);
logsResp = await fetchLatest();
} catch (e) {
handleLogsError(e, accountId, projectName, appPath, functionName);
}
} else {
try {
logsResp = await getProjectAppFunctionLogs(
accountId,
functionName,
projectName,
appPath,
{}
);
logsResp = await tailCall();
} catch (e) {
handleLogsError(e, accountId, projectName, appPath, functionName);
}
Expand All @@ -97,7 +104,7 @@ const appFunctionLog = async (accountId, options) => {
}
};

exports.command = 'logs [functionName]';
exports.command = 'logs';
exports.describe = 'get logs for a function within a project';

exports.handler = async options => {
Expand All @@ -110,7 +117,7 @@ exports.handler = async options => {
logger.error('You must pass a function name to retrieve logs for.');
process.exit(1);
} else if (!projectName) {
const projectConfig = await getProjectConfig(getCwd());
const { projectConfig } = await getProjectConfig(getCwd());
if (projectConfig.name) {
projectName = projectConfig.name;
} else {
Expand All @@ -128,22 +135,22 @@ exports.handler = async options => {

trackCommandUsage('project-logs', { latest }, accountId);

appFunctionLog(accountId, { ...options, projectName });
functionLog(accountId, { ...options, projectName });
};

exports.builder = yargs => {
yargs.positional('functionName', {
describe: 'Serverless app function name',
type: 'string',
demandOption: true,
});
yargs
.options({
appPath: {
describe: 'path to the app',
functionName: {
alias: 'function',
describe: 'Serverless app function name or endpoint route',
type: 'string',
demandOption: true,
},
appPath: {
describe: 'Path to app folder, relative to project',
type: 'string',
},
projectName: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be required? I get a TypeError when running hs project logs and only specifying --functionName...
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

describe: 'name of the project',
type: 'string',
Expand Down Expand Up @@ -172,7 +179,7 @@ exports.builder = yargs => {

yargs.example([
[
'$0 project logs my-function --appName="app" --projectName="my-project"',
'$0 project logs --function=my-function --appPath="app" --projectName="my-project"',
'Get 5 most recent logs for function named "my-function" within the app named "app" within the project named "my-project"',
],
]);
Expand Down