Skip to content

Commit

Permalink
📝 (logger.ts): refactor custom logging format to include stack trace …
Browse files Browse the repository at this point in the history
…and meta information for better
  • Loading branch information
romantech committed Apr 6, 2024
1 parent 31cea5a commit 4943ab4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/config/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@ const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const ignorePrivate = format((info) => (info.private ? false : info));

/** Custom logging format */
const customFormat = printf(({ level, message, timestamp, ...meta }) => {
let result = `[${level}] ${timestamp}: ${message}`;
if (Object.keys(meta).length) {
result += ` ${JSON.stringify(meta)}`;
const formatStackTrace = (stack: string, limit = 3) => {
const [errorMessage, ...restLines] = stack.split('\n');
return [
errorMessage.replace(': ', '-'),
...restLines.slice(0, limit).map((line) => `-> ${line}`),
].join('\n');
};

const customFormat = printf(({ level, message, timestamp, stack, ...meta }) => {
let logMessage = `[${timestamp}] [${level}]: ${message}`;

if (stack?.trim()) logMessage += `\nStack: ${formatStackTrace(stack)}`;

const metaKeys = Object.keys(meta);
if (metaKeys.length > 0) {
const metaInfo = JSON.stringify(meta, null, 2);
logMessage += `\nMeta: ${metaInfo}`;
}
return result;

return logMessage;
});

/** Transform log level to uppercase */
Expand Down

0 comments on commit 4943ab4

Please sign in to comment.