Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/commands/tail-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export const command = new Command('tail-logs')

assertGcpProjectConfigured(clasp);

console.log('PAST', clasp.project.projectId);
await fetchAndPrintLogs();

if (watch) {
Expand Down Expand Up @@ -116,23 +115,22 @@ function formatEntry(entry: loggingV2.Schema$LogEntry, options: FormatOptions):
return undefined;
}

if (options.json) {
return JSON.stringify(entry);
}

let functionName = resource.labels?.['function_name'] ?? 'N/A';
let payloadData = '';

if (options.json) {
payloadData = JSON.stringify(entry, null, 2);
const {jsonPayload, textPayload} = entry;

if (textPayload) {
payloadData = textPayload;
} else if (jsonPayload && jsonPayload.message) {
payloadData = jsonPayload.message;
} else if (jsonPayload) {
payloadData = JSON.stringify(jsonPayload);
} else {
const {jsonPayload, textPayload} = entry;

if (textPayload) {
payloadData = textPayload;
} else if (jsonPayload && jsonPayload.message) {
payloadData = jsonPayload.message;
} else if (jsonPayload) {
payloadData = JSON.stringify(jsonPayload);
} else {
return undefined;
}
return undefined;
}

const colorizer = severityColor[severity!];
Expand Down
31 changes: 27 additions & 4 deletions test/commands/tail-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,37 @@ describe('Tail logs command', function () {
expect(out.stdout).to.contain('test log');
});

it('should print logs in json', async function () {
it('should print logs as valid JSON', async function () {
mockListLogEntries({
projectId: 'mock-gcp-project',
});
const out = await runCommand(['tail-logs', '--json']);
expect(out.stdout).to.contain('"severity": "INFO"');
expect(out.stdout).to.contain('"function_name": "myFunction"');
expect(out.stdout).to.contain('"textPayload": "test log"');
expect(out.stdout).to.not.contain('PAST');
const lines = out.stdout.trim().split('\n');
expect(lines).to.have.length(1);
const entry = JSON.parse(lines[0]);
expect(entry.severity).to.equal('INFO');
expect(entry.resource.labels.function_name).to.equal('myFunction');
expect(entry.textPayload).to.equal('test log');
});

it('should print one JSON object per log entry', async function () {
const entries = ['first log', 'second log'].map((textPayload, index) => ({
timestamp: `2023-10-27T10:00:0${index}Z`,
logName: 'projects/mock-gcp-project/logs/stdout',
severity: 'INFO',
insertId: `test-insert-id-${index}`,
resource: {
type: 'app_script_function',
labels: {project_id: 'mock-gcp-project', function_name: 'myFunction'},
},
textPayload,
}));
mockListLogEntries({projectId: 'mock-gcp-project', entries});
const out = await runCommand(['tail-logs', '--json']);
const lines = out.stdout.trim().split('\n');
expect(lines).to.have.length(2);
expect(lines.map(line => JSON.parse(line).textPayload)).to.have.members(['first log', 'second log']);
});

it('should print logs without timestamps', async function () {
Expand Down