Skip to content

Instantly share code, notes, and snippets.

@yogithesymbian
Created November 23, 2022 23:32
Show Gist options
  • Save yogithesymbian/190c2d2f8e84610e6eadf3b1aaf61a1f to your computer and use it in GitHub Desktop.
Save yogithesymbian/190c2d2f8e84610e6eadf3b1aaf61a1f to your computer and use it in GitHub Desktop.
Utils Winston Node JS

usage

const winLogger = require('@utils/winston')

winLogger.info(`your message`)

output

info: /api/form_test?columnFilters=%7B%7D&searchTerm=&sort=%7B%22type%22:%22DESC%22,%22field%22:%22created_at%22%7D&page=1&perPage=10 {"timestamp":"24/11/2022 07.22.29"}
/* jslint node: true */
/* jshint esversion: 6 */
"use strict";
const { createLogger, format, transports, addColors } = require("winston");
const { combine, timestamp, label, prettyPrint } = format;
var logger;
const myCustomLevels = {
levels: {
error: 0,
warn: 1,
info: 2,
foo: 3,
},
// baz: 'italic yellow',
// foobar: 'bold red cyanBG'
// Possible options are below.
// Font styles: bold, dim, italic, underline, inverse, hidden, strikethrough.
// Font foreground colors: black, red, green, yellow, blue, magenta, cyan, white, gray, grey.
// Background colors: blackBG, redBG, greenBG, yellowBG, blueBG magentaBG, cyanBG, whiteBG
colors: {
error: "red",
warn: "yellow",
info: "green",
foo: "blue",
},
};
const timezoned = () => {
return new Date().toLocaleString("id-ID", {
timeZone: "Asia/Kuala_Lumpur",
});
};
logger = createLogger({
levels: myCustomLevels.levels,
format: combine(
// label({ label: 'right meow!' }),
timestamp({ format: timezoned }),
// format.splat(),
prettyPrint(),
// format.timestamp(),
format.simple(),
format.printf(
(info) => `${info.timestamp} - ${info.level}: ${info.message}\r\n`,
),
format.colorize({ all: true }),
format.json(),
),
transports: [
new transports.File({ filename: "error.log", level: "error" }),
new transports.File({ filename: "combined.log" }),
// new (transports.Console)({'timestamp':true})
new transports.Console({
format: format.combine(format.colorize(), format.simple()),
}),
],
});
addColors(myCustomLevels.colors);
module.exports = logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment