Skip to content

Commit

Permalink
Merge branch 'trunk' into add/inactive-token-error-support
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-sdl authored Nov 14, 2024
2 parents 778bedc + 607af95 commit cf30175
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
16 changes: 14 additions & 2 deletions __tests__/bin/vip-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,26 @@ describe( 'getLogs', () => {
],
} ) );

await getLogs( [], opts );
const isTTY = process.stdout.isTTY;
try {
process.stdout.isTTY = false;
await getLogs( [], opts );
} finally {
process.stdout.isTTY = isTTY;
}

expect( logsLib.getRecentLogs ).toHaveBeenCalledTimes( 1 );
expect( logsLib.getRecentLogs ).toHaveBeenCalledWith( 1, 3, 'app', 500 );

expect( console.log ).toHaveBeenCalledTimes( 1 );
expect( console.log ).toHaveBeenCalledWith(
'2021-11-05T20:18:36.234041811Z My container message 1\n2021-11-09T20:47:07.301221112Z My container message 2'
'┌────────────────────────────────┬────────────────────────┐\n' +
'│ Timestamp │ Message │\n' +
'├────────────────────────────────┼────────────────────────┤\n' +
'│ 2021-11-05T20:18:36.234041811Z │ My container message 1 │\n' +
'├────────────────────────────────┼────────────────────────┤\n' +
'│ 2021-11-09T20:47:07.301221112Z │ My container message 2 │\n' +
'└────────────────────────────────┴────────────────────────┘'
);

const trackingParams = {
Expand Down
32 changes: 29 additions & 3 deletions src/bin/vip-logs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import chalk from 'chalk';
import CliTable3 from 'cli-table3';
import { setTimeout } from 'timers/promises';

import * as logsLib from '../lib/app-logs/app-logs';
Expand Down Expand Up @@ -144,11 +145,36 @@ function printLogs( logs, format ) {

let output = '';
if ( format && 'table' === format ) {
const rows = [];
const options = {
wordWrap: true,
wrapOnWordBoundary: true,
head: [ 'Timestamp', 'Message' ],
style: {
head: [ 'cyan' ],
border: [ 'grey' ],
},
};

if ( process.stdout.isTTY && process.stdout.columns ) {
options.colWidths = [
'YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ'.length + 2 /* padding */,
Math.max(
process.stdout.columns - '│ │ │'.length - 'YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ'.length,
20
),
];
} else {
options.style.head = [];
options.style.border = [];
}

const table = new CliTable3( options );
for ( const { timestamp, message } of logs ) {
rows.push( `${ timestamp } ${ message }` );
output = rows.join( '\n' );
const msg = message.trimRight().replace( /\t/g, ' ' );
table.push( [ timestamp, msg ] );
}

output = table.toString();
} else {
output = formatData( logs, format );
}
Expand Down

0 comments on commit cf30175

Please sign in to comment.