Skip to content

Commit

Permalink
Fix logging of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
NeKzor committed Apr 5, 2024
1 parent 37585ed commit 299099e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
24 changes: 20 additions & 4 deletions src/bot/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,28 @@ const formatDatetime = (datetime: Date) => {
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

const consoleFormatter: _log.FormatterFunction = ({ datetime, level, levelName, msg }) => {
return `${formatDatetime(datetime)} ${formatLevel(level, levelName)} ${msg}`;
// deno-lint-ignore no-explicit-any
const formatArgs = (args: string | any | any[]): string => {
if (typeof args === 'string') {
return args;
}
// deno-lint-ignore no-explicit-any
return args.map((arg: any) => {
if (typeof arg === 'string') {
return arg;
}
return JSON.stringify(arg);
}).join(' ');
};

const consoleFormatter: _log.FormatterFunction = ({ datetime, level, levelName, msg, args }) => {
return `${formatDatetime(datetime)} ${formatLevel(level, levelName)} ${msg}${
args.length ? ' ' + formatArgs(args) : ''
}`;
};

const fileFormatter: _log.FormatterFunction = ({ datetime, levelName, msg }) => {
return `${formatDatetime(datetime)} ${levelName} ${msg}`;
const fileFormatter: _log.FormatterFunction = ({ datetime, levelName, msg, args }) => {
return `${formatDatetime(datetime)} ${levelName} ${msg}${args.length ? ' ' + formatArgs(args) : ''}`;
};

_log.setup({
Expand Down
22 changes: 19 additions & 3 deletions src/client/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,29 @@ const formatDatetime = (datetime: Date) => {
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

const consoleFormatter: _log.FormatterFunction = ({ datetime, level, levelName, msg }) => {
return `${formatDatetime(datetime)} ${formatLevel(level, levelName)} ${msg}`;
// deno-lint-ignore no-explicit-any
const formatArgs = (args: string | any | any[]): string => {
if (typeof args === 'string') {
return args;
}
// deno-lint-ignore no-explicit-any
return args.map((arg: any) => {
if (typeof arg === 'string') {
return arg;
}
return JSON.stringify(arg);
}).join(' ');
};

const consoleFormatter: _log.FormatterFunction = ({ datetime, level, levelName, msg, args }) => {
return `${formatDatetime(datetime)} ${formatLevel(level, levelName)} ${msg}${
args.length ? ' ' + formatArgs(args) : ''
}`;
};

_log.setup({
handlers: {
default: new _log.ConsoleHandler('DEBUG', {
console: new _log.ConsoleHandler('DEBUG', {
useColors: false,
formatter: consoleFormatter,
}),
Expand Down
24 changes: 20 additions & 4 deletions src/server/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,28 @@ const formatDatetime = (datetime: Date) => {
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

const consoleFormatter: _log.FormatterFunction = ({ datetime, level, levelName, msg }) => {
return `${formatDatetime(datetime)} ${formatLevel(level, levelName)} ${msg}`;
// deno-lint-ignore no-explicit-any
const formatArgs = (args: string | any | any[]): string => {
if (typeof args === 'string') {
return args;
}
// deno-lint-ignore no-explicit-any
return args.map((arg: any) => {
if (typeof arg === 'string') {
return arg;
}
return JSON.stringify(arg);
}).join(' ');
};

const consoleFormatter: _log.FormatterFunction = ({ datetime, level, levelName, msg, args }) => {
return `${formatDatetime(datetime)} ${formatLevel(level, levelName)} ${msg}${
args.length ? ' ' + formatArgs(args) : ''
}`;
};

const fileFormatter: _log.FormatterFunction = ({ datetime, levelName, msg }) => {
return `${formatDatetime(datetime)} ${levelName} ${msg}`;
const fileFormatter: _log.FormatterFunction = ({ datetime, levelName, msg, args }) => {
return `${formatDatetime(datetime)} ${levelName} ${msg}${args.length ? ' ' + formatArgs(args) : ''}`;
};

export const installLogger = (moduleName: string) => {
Expand Down

0 comments on commit 299099e

Please sign in to comment.