Skip to content
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

fix(utils/log): Remove formatting of log messages in ConsoleLogger #2408

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/nine-knives-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@equinor/fusion-log': minor
---

Removed formatting of messages in the `log` function. Now the messages are printed as they are, without any additional formatting. This is done to make the messages more readable and to avoid any additional formatting that might be added by the user. Formatting objects would result in the console outputting `[[object,object]]` which was not very helpful.
18 changes: 6 additions & 12 deletions packages/utils/log/src/ConsoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,17 @@ export class ConsoleLogger extends Logger {
* @returns An array of unknown, representing the formatted log message.
*/
protected _createMessage(lvl: LogLevel, ...msg: unknown[]): unknown[] {
return this._formatMessage(lvl, this._formatTitle(lvl), ...msg);
return [this._formatTitle(lvl), ...msg];
}

protected _formatTitle(_lvl: LogLevel): string {
const title = [this.title, this.subtitle].filter((x) => !!x).join(' - ');
return chalk.magenta(title);
}

protected _formatMessage(lvl: LogLevel, ...msg: unknown[]): unknown[] {
switch (lvl) {
case LogLevel.Debug:
return [chalk.dim(...msg)];
const title = chalk.magenta([this.title, this.subtitle].filter((x) => !!x).join(' - '));
switch (_lvl) {
case LogLevel.Warning:
return [chalk.bold(...msg)];
default:
return msg;
case LogLevel.Error:
return chalk.bold(title);
}
return title;
}

/**
Expand Down
11 changes: 8 additions & 3 deletions packages/utils/log/tests/ConsoleLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('Console logger', () => {
logger.level = LogLevel.Debug;
logger.debug('This is a debug message');
expect(spy).toHaveBeenCalledWith(
chalk.dim(chalk.magenta('MainLogger'), 'This is a debug message'),
chalk(chalk.magenta('MainLogger')),
'This is a debug message',
);
});
it('should log info messages', () => {
Expand All @@ -28,7 +29,8 @@ describe('Console logger', () => {
logger.level = LogLevel.Warning;
logger.warn('This is a warning message');
expect(spy).toHaveBeenCalledWith(
chalk.bold(chalk.magenta('MainLogger'), 'This is a warning message'),
chalk.bold.magenta('MainLogger'),
'This is a warning message',
);
});

Expand All @@ -37,7 +39,10 @@ describe('Console logger', () => {
const logger = new ConsoleLogger('MainLogger');
logger.level = LogLevel.Error;
logger.error('This is an error message');
expect(spy).toHaveBeenCalledWith(chalk.magenta('MainLogger'), 'This is an error message');
expect(spy).toHaveBeenCalledWith(
chalk.bold.magenta('MainLogger'),
'This is an error message',
);
});

it('should log multiple messages', () => {
Expand Down