Skip to content

Commit d11ece4

Browse files
authored
Merge pull request #130 from microservices-suite/repo-engineering/universal-cli
feat: format logging for clean and elegant logs
2 parents 299f161 + 7d4c2ab commit d11ece4

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

.suite-cli/cli/scripts/assets/serverContent.asset.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ const { router } = require('./src/routes');
1010
// const app = require('./src/app');
1111
1212
mongoose.connect(config.db).then(() => {
13-
logger.info(\`📀 successfully connected to db: \${config.db}\`);
13+
logger.info(\`📀 successfully connected to db: \${config.db}\`);
1414
}).catch(err => {
15-
logger.error(\`failed to connect to db.exiting...\${err.message}\`);
16-
process.exit(0);
15+
logger.error(\`failed to connect to db. Exiting... \${err.message}\`);
16+
process.exit(0);
1717
});
1818
1919
const app = express();
2020
2121
app.use(express.json());
2222
2323
app.get('/', (req, res) => {
24-
res.json({ message: 'hello from ${answers.project_base}/${answers.service_name}' });
24+
res.json({ message: 'hello from ${answers.project_base}/${answers.service_name}' });
2525
});
2626
2727
const server = http.createServer(app);
2828
2929
server.on('error', (err) => {
30-
logger.error(err);
31-
if (err.code === 'EADDRINUSE') {
32-
logger.error('Address already in use, retrying...');
33-
setTimeout(() => {
34-
server.close();
35-
server.listen(config.port, 'localhost');
36-
}, 1000);
37-
errorHandler(err);
38-
}
39-
})
30+
logger.error(err);
31+
if (err.code === 'EADDRINUSE') {
32+
logger.error('Address already in use, retrying...');
33+
setTimeout(() => {
34+
server.close();
35+
server.listen(config.port, 'localhost');
36+
}, 1000);
37+
errorHandler(err);
38+
}
39+
});
4040
4141
server.listen(config.port, () => {
4242
logger.info(\`🚀 ${answers.project_base}/${answers.service_name} listening at: http://localhost:\${config.port}\`);
@@ -48,11 +48,11 @@ app.use(morgan.successHandler);
4848
4949
app.use('/api/v1', router);
5050
51-
// global error handler should come after all other middlewares
51+
// Global error handler should come after all other middlewares
5252
app.all('*', (req, res, next) => {
53-
const err = new APIError(404, \`requested resource not found on server: \${req.originalUrl}\`);
54-
next(err);
53+
const err = new APIError(404, \`requested resource not found on server: \${req.originalUrl}\`);
54+
next(err);
5555
});
5656
5757
app.use(errorHandler);
58-
`;
58+
`;

.suite-cli/cli/scripts/scripts.module.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,27 @@ const spinVanillaServices = async ({ serviceDirectories, microservicesDir, mode
383383
const child = spawn(command, args, { cwd: servicePath, shell: true });
384384

385385
child.stdout.on('data', (data) => {
386-
const output = data.toString();
386+
let output = data.toString();
387387
// Check if the output contains the "yarn run" message
388388
if (!output.includes('yarn run') && !output.includes('NODE_ENV')) {
389389
// Stop the spinner before printing the output
390-
console.log(output.trim());
390+
if (output.includes('info')) {
391+
const parts = output.trim().split(':');
392+
const formattedOutput = formatLog(parts[0], dir, parts.slice(1).join(':').trim());
393+
console.log(formattedOutput);
394+
} else {
395+
console.log(output.trim());
396+
}
391397
// Restart the spinner after printing the output
392398
}
393399
});
394400

395401
child.stderr.on('data', (data) => {
396402
let output = data.toString();
397-
output = output.split(':')
403+
const parts = output.trim().split(':');
404+
const formattedOutput = formatLog(parts[0], dir, parts.slice(1).join(':').trim());
398405
// Handle stderr output
399-
console.log(`${output[0]}: ${dir}: ${output[1]}`);
406+
console.log(formattedOutput);
400407
});
401408

402409
child.on('close', (code) => {
@@ -418,6 +425,15 @@ const spinVanillaServices = async ({ serviceDirectories, microservicesDir, mode
418425
}
419426
};
420427

428+
const padString = (str, length) => str.padEnd(length, ' ');
429+
430+
// Function to format the log messages
431+
const formatLog = (level, dir, message) => {
432+
const paddedLevel = padString(level, 5);
433+
const paddedDir = padString(dir, 8);
434+
return `${paddedLevel}: ${paddedDir}: ${message}`;
435+
};
436+
421437
/**
422438
*
423439
* @param {Object} options Environment to run the

0 commit comments

Comments
 (0)