Minimal logger for nodejs
npm install just-enough-logger
or
yarn add just-enough-logger
const { Logger } = require("just-enough-logger");
// or
// [DEPRECATED] const Logger = require("just-enough-logger").default;
// Default export is available for backward compatibility. It'll be removed in next version.
let log = new Logger();
// let log = new Logger(options?); // Check options
// By default, prints to a file and console
// Default file is current_directory/log.log
// Check the options for more details
log.info("this is info message");
// 10/2/2019, 12:57:14 AM : [INFO] : this is info message
log.warn("this is warn message");
// 10/2/2019, 12:57:14 AM : [WARN] : this is warn message
log.error("this is error message");
// 10/2/2019, 12:57:14 AM : [ERROR] : this is error message
Pass the options object to Logger constructor.
- Where to print log message
- Type: Array
- Valid Values:
["file", "console"]
- Default:
["file", "console"]
Example:
let log = new Logger({
transports: ["console"], // Only prints on console
});
- Path of log file
- Type: Path String
- Valid Values:
/path/to/log.log
- Default:
[current_directory]/log.log
Example:
let log = new Logger({
file: path.resolve(__dirname, "logs", "my-log.log"),
});
- Custom formatter
- Type: Function
- Signature:
(message: string, level: "info" | "warn" | "error") => string
- Default: Returns log in format
${new Date().toLocaleString()} : [${level.toUpperCase()}] : ${message}
Example:
let log = new Logger({
formatter: (message, level) => {
return `${new Date()} ${level} ${message}`;
},
});
Methods work on instance of Logger
- Returns the path of log file.
let logger = new Logger();
console.log(logger.getLogFilePath()); // c:\Programming\my-project\log.log
- Returns
WriteStream
of log file or null if file transports is not present.
let logger = new Logger();
console.log(logger.getLogStream());