@0xcryptomag/winston-plus
v1.0.1
Published
Winston@3 console format aimed to improve development UX
Downloads
7
Maintainers
Readme
winston-dev-console
My personal additions to @epegzz's winston-dev-console (based on winston-console-format)
- adding the source of the logging statement to the log output
- optimizing readability of each log statement through log statement separation, output colorization and arg pretty printing
- log outputs in util.inspection format, or as tables
Demo
Real world screenshot:
Install
npm install winston @0xcryptomag/winston-plus
or
yarn add winston @0xcryptomag/winston-plus
Usage TypeScript
import { createLogger, format, transports, config } from "winston";
import winstonDevConsole from "@epegzz/winston-dev-console";
import util from "util";
let log = createLogger({
levels: config.syslog.levels,
level: "debug", // or use process.env.LOG_LEVEL
});
// Note: You probably only want to use winstonDevConsole during development
log = winstonDevConsole.init(log);
log.add(
winstonDevConsole.transport({
showTimestamps: false,
addLineSeparation: true,
logLevels: config.syslog.levels
})
);
log.info("Logging initialized");
log.debug("Debug an object", { make: "Ford", model: "Mustang", year: 1969 });
log.notice("Returned value", { value: util.format });
log.alert("Information", {
options: ["Lorem ipsum", "dolor sit amet"],
values: ["Donec augue eros, ultrices."],
});
log.warn("Warning");
log.emerg(new Error("Unexpected error"));
Usage JavaScript
const { createLogger, format, transports, config } = require("winston");
const winstonDevConsole = require("@epegzz/winston-dev-console").default;
const util = require("util");
let log = createLogger({
levels: config.syslog.levels,
level: "debug", // or use process.env.LOG_LEVEL
});
// Note: You probably only want to use winstonDevConsole during development
log = winstonDevConsole.init(log);
log.add(
winstonDevConsole.transport({
showTimestamps: false,
addLineSeparation: true,
logLevels: config.cli.levels,
})
);
log.silly("Logging initialized");
log.prompt("Debug an object", { make: "Ford", model: "Mustang", year: 1969 });
log.verbose("Returned value", { value: util.format });
log.info("Information", {
options: ["Lorem ipsum", "dolor sit amet"],
values: ["Donec augue eros, ultrices."],
});
log.warn("Warning");
log.emerg(new Error("Unexpected error"));
Usage Tables
let tableLog = createLogger({
level: "debug", // or use process.env.LOG_LEVEL
levels: config.syslog.levels
});
// Note: You probably only want to use winstonDevConsole during development
tableLog = winstonDevConsole.init(tableLog);
tableLog.add(
winstonDevConsole.transport({
showTimestamps: true,
addLineSeparation: true,
logLevels: config.syslog.levels,
table: true
})
);
// The first item of the array is reserved for the Table constructor argument which takes in an options object
// {}, [], '', or null can be used to forego a header/formatting
tableLog.info("Horizontal Table", [
{head: ['TH 1 label', 'TH 2 label'], colWidths: [25, 25]},
['First value', 'Second value'],
['First value', 'Second value']
]);
tableLog.notice("Vertical Table", [
{},
{ 'Some key': 'Some value' },
{ 'Another key': 'Another value' }
]);
tableLog.debug("Cross Table", [
{ head: [ '', 'Top Header 1', 'Top Header 2' ] },
{ 'Left Header 1': [ 'Value Row 1 Col 1', 'Value Row 1 Col 2' ] },
{ 'Left Header 2': [ 'Value Row 2 Col 1', 'Value Row 2 Col 2' ] }
]);
API
winstonDevConsole.format(options)
options
Configuration object.Type: DevConsoleFormatOptions
options.inspectOptions
util.inspect()
configuration object. Type: Object
options.basePath
Used to remove the base path of the project when showing the file path of the log statement.
By default anything in the path before (and including) the src
folder will be removed.
Type: String
options.addLineSeparation
Wheather or not to separate each log statement with a blank line.
Type: Boolean
Default: true
options.showTimestamps
Wheather or not to show timestamps
During development the timestamps are usually more noise then helpful, therefore disabled by default.
Type: Boolean
Default: false
options.logLevels
Used to select the log level severity schema from the three options provided by triple-beam: npm, syslog, and cli
By default this is set to npm log levels
Type: {[k: string]: number}
Default: winston.config.npm.levels
options.showMeta
Wheather or not to show meta after the message, callee path, and optional timestamp
Type: boolean
Default: true
options.table
Wheather or not the meta lines after the message is outputted as a table rather than a string literal of an object
Use only when the output is expected to always be tablature. A second logger may be required for tables and non tables
Type: boolean
Default: false
Acknowledgements
This is a fork of @epegzz's winston-dev-console
His project based off of @duccio's winston-console-format