loggol
v0.0.4
Published
Configurable logging module written in TypeScript
Downloads
1
Maintainers
Readme
Introduction
Simple logging module written in TypeScript.
Levels
There are 5 log levels:
const Logger = require("loggol")
Logger.Level.DEBUG
Logger.Level.INFO
Logger.Level.WARN
Logger.Level.ERROR
Logger.Level.OFF
Logging to the console
The following code creates a logger object that writes to stdout
and stderr in the format [dd/mm/yyyy hh:mm:ss][LEVEL][TAG]: message]
:
const Logger = require("loggol")
const logger = new Logger.Console({
tag: "Logger1",
level: Logger.Level.DEBUG,
format: Logger.Format.standard
});
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
If you would like colours and your terminal supports it, replace
Logger.Format.standard
with Logger.Format.standardColoured
Logging to a file
The following code creates a logger object that writes to a file stream of your choice, in the same format as above
const fs = require("fs");
const Logger = require("loggol")
const logger = new Logger.File({
tag: "Logger2",
level: Logger.Level.DEBUG,
format: Logger.Format.standard,
file: fs.createWriteStream("output.log", {
flags: "a",
encoding: "utf-8"
})
});
Chaining loggers
Often you could find yourself wanting to log the
same data to two or more different output, you can do this
by using the chain method of the Logger
prototype
const Logger = require("loggol");
const logger1 = new Logger.Console(...);
const logger2 = new Logger.File(...);
const logger3 = new Logger.File(...);
logger1
.chain(logger2)
.chain(logger3)
logger1.info("hello!");
// logs to logger2 and logger3 as well
logger2.info("wow");
// logs to logger3, but not logger1
Creating your own format
Format functions receive a LogData
object:
type LogData = {
date: Date;
tag: string;
level: number;
items: any[]
};
Just to clarify, since the others are self explanatory, items is just an array of whatever the calling code wanted to log.
Your function should look something like this:
function myFormat({ date, tag, level, items }) {
// create output here
// return string
}
Then you can pass this function around to all your loggers that need to use it.
Creating your own Logger
If the included implemenations don't quite fit your needs you can always implement your own and keep the same public interface.
const Logger = require("loggol");
class OtherLogger extends Logger.Abstract {
write(message) {
// whatever needs to happen to log 'message'
}
}
Logger factories
Sometimes you may not want to use a single logger with the same tag and level. A function that you can import everywhere a logger is needed is great for creating a bunch of similar logger objects.
For example, a lot of files I have only need Logger.Console
and
they should all have the same level and format, but a different tag.
const Logger = require("loggol");
const level = Logger.Level.INFO;
const format = Logger.Format.standardColoured;
function createLogger(tag) {
return new Logger.Console({
tag,
level,
format
});
}
module.exports = createLogger;
You could also do something similar to return different types of Logger
's