Skip to content

Commit

Permalink
fix(logging): Add pino-pretty programatically
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Dec 23, 2023
1 parent d82d3c9 commit 68968aa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 60 deletions.
3 changes: 2 additions & 1 deletion docs/api-reports/packages/logging/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
```ts
import { DestinationStream } from "pino";
import { Logger } from "pino";
import { LoggerOptions as LoggerOptions_2 } from "pino";
import { StormConfig } from "@storm-software/config-tools";
import { Temporal } from "@js-temporal/polyfill";
Expand All @@ -19,7 +20,7 @@ export const getLogLevelLabel: (logLevel: number) => LogLevelLabel;
const getTransports: (
config: StormConfig<"logging", LoggingConfig>,
name?: string
) => any;
) => Logger<any>;
export { getTransports };
export { getTransports as getTransports_alias_1 };
export { getTransports as getTransports_alias_2 };
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reports/packages/logging/documents-model.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
},
{
"kind": "Content",
"text": ">, name?: string) => any"
"text": ">, name?: string) => import(\"pino\").Logger<any>"
}
],
"fileUrlPath": "packages/logging/src/utilities/get-transports.ts",
Expand Down
8 changes: 1 addition & 7 deletions packages/logging/src/storm-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ export class StormLog implements IStormLog {
config: StormConfig<"logging", LoggingConfig>,
name?: string
): pino.BaseLogger => {
let pinoLogger!: pino.BaseLogger;

const transports = getTransports(config, name);
if (transports) {
pinoLogger = pino(transports);
}

const pinoLogger: pino.BaseLogger = getTransports(config, name);
pinoLogger.debug("The Storm log has ben initialized");

return pinoLogger;
Expand Down
97 changes: 46 additions & 51 deletions packages/logging/src/utilities/get-transports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import pino, {
LoggerOptions as PinoLoggerOptions,
TransportTargetOptions
} from "pino";
import "pino-pretty";
import pretty from "pino-pretty";
import { LoggingConfig } from "../types";
import { LogLevel, getLogLevel } from "./get-log-level";

Expand Down Expand Up @@ -76,50 +76,40 @@ export const getTransports = (
}
};

let transports: TransportTargetOptions[] = [
{
target: "pino-pretty",
options: {
...baseOptions,
msgPrefix: "STORM",
destination: 1, // 1 = stdout
colorize: true,
colorizeObjects: true,
errorLikeObjectKeys: ["err", "error", "exception"],
minimumLevel: config.logLevel,
messageKey: "msg",
errorKey: "error",
timestampKey: "time",
messageFormat:
"[{time}] {levelLabel} ({if pid}{pid} - {end}{req.url}: {msg}",
singleLine: false,
hideObject: false,
customColors: {}
}
}
];
if (config.colors) {
transports = transports.map(transport => {
if (transport.options) {
transport.options.customColors = {
exception: config.colors.error,
err: config.colors.error,
error: config.colors.error,
fatal: config.colors.fatal,
warn: config.colors.warning,
info: config.colors.info,
debug: config.colors.primary,
trace: config.colors.primary,
"req.url": config.colors.primary,
success: config.colors.success
};
transport.options.msgPrefix = chalk.bold.hex(config.colors.primary)(
"STORM"
);
}
let transports: TransportTargetOptions[] = [];

const prettyOptions = {
...baseOptions,
msgPrefix: "STORM",
destination: 1, // 1 = stdout
colorize: true,
colorizeObjects: true,
errorLikeObjectKeys: ["err", "error", "exception"],
minimumLevel: config.logLevel,
messageKey: "msg",
errorKey: "error",
timestampKey: "time",
messageFormat:
"[{time}] {levelLabel} ({if pid}{pid} - {end}{req.url}: {msg}",
singleLine: false,
hideObject: false,
customColors: {}
};

return transport;
});
if (config.colors) {
prettyOptions.customColors = {
exception: config.colors.error,
err: config.colors.error,
error: config.colors.error,
fatal: config.colors.fatal,
warn: config.colors.warning,
info: config.colors.info,
debug: config.colors.primary,
trace: config.colors.primary,
"req.url": config.colors.primary,
success: config.colors.success
};
prettyOptions.msgPrefix = chalk.bold.hex(config.colors.primary)("STORM");
}

if (isRuntimeServer()) {
Expand Down Expand Up @@ -169,11 +159,12 @@ Message: {msg}
loggingConfig.fileExtension
? loggingConfig.fileExtension.replaceAll(".", EMPTY_STRING)
: "log"
}}`
}`
),
minLength: 4096,
sync: false,
mkdir: true
mkdir: true,
append: true
})
}
},
Expand All @@ -198,11 +189,12 @@ Message: {msg}
loggingConfig.fileExtension
? loggingConfig.fileExtension.replaceAll(".", EMPTY_STRING)
: "log"
}}`
}`
),
minLength: 4096,
sync: false,
mkdir: true
mkdir: true,
append: true
})
}
}
Expand All @@ -229,7 +221,10 @@ Message: {msg}
}
}

return pino.transport({
targets: transports
});
return pino(
pino.transport({
targets: transports
}),
pretty(prettyOptions)
);
};

0 comments on commit 68968aa

Please sign in to comment.