Ihacks-log - logging module (winston alternative)

ihacks-log

starter-ihacks-log project.

A winston alternative. iHack’s Log is a very simple yet advanced module that lets you do merely anything when it comes to logging!

ihacks-log is powerful, not only because it can do all these fancy things, but because it uses console.Console under the hood. Well, and then of course all the other fancy things it does. This means that whatever works in console.log also works in ihacks-log.

For example:

myLogger.log("This is %s, he is %s and %s.", "John", "fun", "kind");
// This is John, he is fun and kind.

Or you can even log objects:

myLogger.log({
  a: "b",
  c: "d",
  e: "f"
});
// {
//   a: "b",
//   c: "b",
//   c: "d",
//   e: "f"
// }

Or maybe an error message?

myLogger.error(new Error("Hello"));
// Error: Hello
//     ... stack trace ...

Here’s a simple example on how to use the module:

// Import
const {
  Logger,
  LogConsoleTransport,
  LogFileTransport
} = require("ihacks-log");

// Create a new logger.
const myLogger = new Logger();

// A formatting function.
const formatFn = data => `${data.timestamp.toString()} (${Logger.getLevelName(data.level)} ${data.message})`

// Add transports
myLogger.addTransport(
  new LogConsoleTransport(formatFn),
  new LogFileTransport("filename.log", formatFn)
);

// Log stuff
myLogger.debug("This is a debug message, intended to spot bugs or unexpected behaviour.");
myLogger.log("This is just a log entry, for uncategorized messages.");
myLogger.info("This is an informational message, intended to inform when something happens.");
myLogger.alert("This is an alert message, intended to alert the reader of something that is going on.");
myLogger.success("This is a success message, intended to indicate that something went well.");
myLogger.warn("This is a warning message, intended to indicate that something possibly can go wrong.");
myLogger.error("This is an error message, intended to indicate that something went horribly wrong!);

Although you’re not limited to the above, you can integrate colors using chalk (or any other coloring modules).

const consoleFormatFn = data => `[${yellowBright(data.timestamp.getDate().toString().padStart(2, "0"))}${blueBright("/")}${yellowBright((data.timestamp.getMonth() + 1).toString().padStart(2, "0"))}${blueBright("/")}${yellowBright(data.timestamp.getFullYear().toString())} ${yellowBright(data.timestamp.getHours().toString().padStart(2, "0"))}${blueBright(":")}${yellowBright(data.timestamp.getMinutes().toString().padStart(2, "0"))}${blueBright(":")}${yellowBright(data.timestamp.getSeconds().toString().padStart(2, "0"))}] (${Logger.getLevelColor(data.level)(Logger.getLevelName(data.level))}) ${data.message}`;

myLogger.addTransport(
  new LogConsoleTransport(consoleFormatFn)
);

myLogger.info("This message has colors!");

image

You can also do more advanced stuff like deciding what log levels to emit (or log).

// Imports
const {
  Logger,
  LogConsoleTransport,
  LogLevel
} = require("ihacks-log");
const {
  yellowBright,
  blueBright
} = require("chalk");

// Create Logger
const myLogger = new Logger();

// Console format function
const consoleFormatFn = data => `[${yellowBright(data.timestamp.getDate().toString().padStart(2, "0"))}${blueBright("/")}${yellowBright((data.timestamp.getMonth() + 1).toString().padStart(2, "0"))}${blueBright("/")}${yellowBright(data.timestamp.getFullYear().toString())} ${yellowBright(data.timestamp.getHours().toString().padStart(2, "0"))}${blueBright(":")}${yellowBright(data.timestamp.getMinutes().toString().padStart(2, "0"))}${blueBright(":")}${yellowBright(data.timestamp.getSeconds().toString().padStart(2, "0"))}] (${Logger.getLevelColor(data.level)(Logger.getLevelName(data.level))}) ${data.message}`;

// Log level
//   Don't log debug messages or uncategorized messages.
const emit = LogLevel.All ^ LogLevel.DEBUG ^ LogLevel.DEFAULT;

// Add console transport.
myLogger.addTransport(
  new LogConsoleTransport(consoleFormatFn, emit)
);

// Log stuff
myLogger.debug("This won't show");
myLogger.log("This won't show");
myLogger.info("This will show");
myLogger.alert("This will show");
myLogger.success("This will show");
myLogger.warn("This will show");
myLogger.error("This will show");

image

That adds an advantage if you want to log some things to the console but other things to a file.

Here are screenshots of my starter-ihacks-log project.

image

5 Likes

Will use this, thanks

2 Likes