Use log4js Print Log
Regardless of doing any development when the print log is a very important function.
In Node.js, the most common way to fight the log is console.log.
If you want to write to a file, or keep a log advanced docking system, we need a special module, such as log4js.
Standard output
We want to achieve the same function console.log, you can simply use log4js.getLogger.
code show as below:
import {configure, getLogger} from "log4js";
const logger1 = getLogger();
logger1.level = 'debug';
logger1.debug('Hello,log4s!');
The words written with javascript code is as follows:
const log4js = require("log4js");
const logger1 = log4js.getLogger();
logger1.level = 'debug';
logger1.debug('Hello,log4s!')
Print to file
Let us illustrate Advanced usage, how to write to a file. We also hope that the time will log points based on file. Specify the output parameter called Appender, we use dateFile Appender type, specify a file name and time types, Appender give a name on it. code show as below:
import {configure, getLogger} from "log4js";
configure({
appenders: {
log_file: {
type: "dateFile",
filename: "log_file",
pattern: "yyyy-MM-dd.log",
alwaysIncludePattern: true,
},
},
categories: { default: { appenders: ["log_file"], level: "debug" } }
});
const logger = getLogger("log_file")
logger.info("Hello to file");
Eventually, log will be written to the file name log_file.2019-05-31.log the class.
js similar wording:
const log4js = require("log4js");
log4js.configure({
appenders: {
log_file: {
type: "dateFile",
filename: "log_file",
pattern: "yyyy-MM-dd.log",
alwaysIncludePattern: true,
},
},
categories: { default: { appenders: ["log_file"], level: "debug" } }
});
const logger = log4js.getLogger("log_file");