Skip to content

Commit

Permalink
Merge pull request gremo#406 from jdG-/pretty-print-nestLikeConsoleFo…
Browse files Browse the repository at this point in the history
…rmat

Add prettyPrint option to nestLikeConsoleFormat
  • Loading branch information
gremo committed Oct 1, 2021
2 parents 5ef9893 + 71a9f0c commit d9340a8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ import * as winston from 'winston';
format: winston.format.combine(
winston.format.timestamp(),
winston.format.ms(),
nestWinstonModuleUtilities.format.nestLike(),
nestWinstonModuleUtilities.format.nestLike('MyApp', { prettyPrint: true }),
),
}),
// other transports...
Expand Down
4 changes: 4 additions & 0 deletions src/winston.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { Type } from '@nestjs/common';

export type WinstonModuleOptions = LoggerOptions;

export type NestLikeConsoleFormatOptions = {
prettyPrint: boolean;
};

export interface WinstonModuleOptionsFactory {
createWinstonModuleOptions(): Promise<WinstonModuleOptions> | WinstonModuleOptions;
}
Expand Down
54 changes: 35 additions & 19 deletions src/winston.utilities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Format } from 'logform';
import { NestLikeConsoleFormatOptions } from './winston.interfaces';
import bare from 'cli-color/bare';
import clc from 'cli-color';
import { format } from 'winston';
import { inspect } from 'util';
import safeStringify from 'fast-safe-stringify';

const nestLikeColorScheme: Record<string, bare.Format> = {
Expand All @@ -12,29 +14,43 @@ const nestLikeColorScheme: Record<string, bare.Format> = {
verbose: clc.cyanBright,
};

const nestLikeConsoleFormat = (appName = 'NestWinston'): Format => format.printf(({ context, level, timestamp, message, ms, ...meta }) => {
if ('undefined' !== typeof timestamp) {
// Only format the timestamp to a locale representation if it's ISO 8601 format. Any format
// that is not a valid date string will throw, just ignore it (it will be printed as-is).
try {
if (timestamp === (new Date(timestamp)).toISOString()) {
timestamp = (new Date(timestamp)).toLocaleString();
const nestLikeConsoleFormat = (
appName = 'NestWinston',
options?: NestLikeConsoleFormatOptions
): Format =>
format.printf(({ context, level, timestamp, message, ms, ...meta }) => {
if ('undefined' !== typeof timestamp) {
// Only format the timestamp to a locale representation if it's ISO 8601 format. Any format
// that is not a valid date string will throw, just ignore it (it will be printed as-is).
try {
if (timestamp === new Date(timestamp).toISOString()) {
timestamp = new Date(timestamp).toLocaleString();
}
} catch (error) {
// eslint-disable-next-line no-empty
}
} catch (error) { // eslint-disable-next-line no-empty
}
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const color = nestLikeColorScheme[level] || ((text: string): string => text);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const color = nestLikeColorScheme[level] || ((text: string): string => text);

return `${color(`[${appName}]`)} ` +
`${clc.yellow(level.charAt(0).toUpperCase() + level.slice(1))}\t` +
('undefined' !== typeof timestamp ? `${timestamp} ` : '') +
('undefined' !== typeof context ? `${clc.yellow('[' + context + ']')} ` : '') +
`${color(message)} - ` +
`${safeStringify(meta)}` +
('undefined' !== typeof ms ? ` ${clc.yellow(ms)}` : '');
});
const stringifiedMeta = safeStringify(meta);
const formattedMeta = options?.prettyPrint
? inspect(JSON.parse(stringifiedMeta), { colors: true, depth: null })
: stringifiedMeta;

return (
`${color(`[${appName}]`)} ` +
`${clc.yellow(level.charAt(0).toUpperCase() + level.slice(1))}\t` +
('undefined' !== typeof timestamp ? `${timestamp} ` : '') +
('undefined' !== typeof context
? `${clc.yellow('[' + context + ']')} `
: '') +
`${color(message)} - ` +
`${formattedMeta}` +
('undefined' !== typeof ms ? ` ${clc.yellow(ms)}` : '')
);
});

export const utilities = {
format: {
Expand Down

0 comments on commit d9340a8

Please sign in to comment.