pino-princess
Version:
Prettiest Pino Prettifier in all the land
117 lines (114 loc) • 3.67 kB
JavaScript
#!/usr/bin/env node
import fs from 'node:fs';
import util from 'node:util';
import process from 'node:process';
import os from 'node:os';
import pump from 'pump';
import { cosmiconfigSync } from 'cosmiconfig';
import { build } from "./index.js";
const helpText = `
Pino Princess - Pretty print json logs with emojis and colors
Usage
$ node my-app-with-pino-logging | pino-princess
Options
--exclude, -e excluded log fields separated by comma. Is overriden by included fields.
--include, -i included log fields separated by comma. Overrides excluded fields.
--messageKey key for the message field, defaults to 'msg'
--errorLikeKeys additional keys for error fields, defaults to 'error' and 'err'
--timeKey key for the time field, defaults to 'time'
--timeFormat format for the time field, passed to date-fns format defaults to 'h:mm:ss.SSS aaa'
--singleLine format the output as a single line, defaults to false
--unicode force unicode emojis on, auto-detected by default
--no-unicode force unicode emojis off
--colors enable all color output, auto-detected by default
--no-colors disable all color output
`;
const cli = util.parseArgs({
options: {
help: {
type: 'boolean',
short: 'h',
},
exclude: {
type: 'string',
short: 'e',
},
include: {
type: 'string',
short: 'i',
},
messageKey: {
type: 'string',
},
// Keeping errorKey for backward compatibility
errorKey: {
type: 'string',
isMultiple: true,
},
errorLikeKeys: {
type: 'string',
isMultiple: true,
},
singleLine: {
type: 'boolean',
},
timeFormat: {
type: 'string',
},
timeKey: {
type: 'string',
},
// Set to string so we can detect if the user passed them or not
unicode: {
type: 'boolean',
},
// Set to string so we can detect if the user passed them or not
colors: {
type: 'boolean',
},
},
allowNegative: true,
});
const cliConfig = {};
if (cli.values.help) {
console.log(helpText);
process.exit(0);
}
if (cli.values.exclude) {
cliConfig.exclude = cli.values.exclude.split(',').map((str) => str.trim());
}
if (cli.values.include) {
cliConfig.include = cli.values.include.split(',').map((str) => str.trim());
}
if (cli.values.messageKey) {
cliConfig.messageKey = cli.values.messageKey;
}
if (cli.values.errorLikeKeys ?? cli.values.errorKey) {
cliConfig.errorLikeKeys = (cli.values.errorLikeKeys ?? cli.values.errorKey)
.split(',')
.map((str) => str.trim());
}
if (cli.values.timeFormat) {
cliConfig.timeFormat = cli.values.timeFormat;
}
if (cli.values.singleLine) {
cliConfig.singleLine = cli.values.singleLine;
}
if (cli.values.timeKey) {
cliConfig.timeKey = cli.values.timeKey;
}
cliConfig.unicode = cli.values.unicode;
cliConfig.colors = cli.values.colors;
const defaultConfig = {
messageKey: 'msg',
timeFormat: 'h:mm:ss.SSS aaa',
singleLine: false,
};
const explorer = cosmiconfigSync('pino-princess', { stopDir: os.homedir() });
const { config } = (explorer.search(process.cwd()) ?? {});
const res = build({ ...defaultConfig, ...config, ...cliConfig });
pump(process.stdin, res);
if (!process.stdin.isTTY && !fs.fstatSync(process.stdin.fd).isFile()) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
process.once('SIGINT', function () { });
}