sfn-logger v0.5.4
SFN-Logger
Simple and friendly NodeJS file logger.
This documentation is for sfn-logger 0.4.x, old versions are deprecated.
Install
npm install sfn-logger --saveExample
const Logger = require("sfn-logger");
var logger = new Logger("example.log");
logger.log("Hello, World!");Create Logger
new Logger(filename: string)Creates a logger instance with a filename.new Logger(options: Logger.Options)Creates a logger instance with options.Logger.Optionsttl?: numberHow much time should the output buffer keep contents before flushing, default value is1000ms.size?: numberHow much size should the output buffer keep contents before flushing. This option conflicts withttl, set only one of them. For data integrity, the real size of flushing data may be smaller than the setting value.filename: stringWrites the contents to the target file.fileSize?: numberThe size of the log file, when up to limit, logs will be compressed or sent via e-mail, default value is2097152bytes (2 Mb). For data integrity, the real size of the file may be smaller than the setting value.dateFormat?: stringThe format of prefix date-time value, default value isYYYY-MM-DDTHH:mm:ss, used by moment.trace?: booleanThe log message should contain the filename and position of where triggers the logging operation,falseby default.toConsole?: booleanThe log should also be output to the console,falseby default.outputLevel?: numberSets the minimum level of logs that should be output to the file, default value isLogger.Levels.DEBUG.mailAn object configures a new Mail instance (from sfn-mail) or a existing Mail instance.
Logger.LevelsAn enum object contains number from1-4DEBUGINFOWARNERROR
// Simplest way to create a logger:
var logger = new Logger("example.log");
// Or specify more details:
var logger = new Logger({
size: 1024 * 4,
filename: "example.log",
fileSize: 1024 * 1024 * 2,
});
// Send log file contents as an email when the file size up to limit:
var logger = new Logger({
size: 1024 * 4,
filename: "example.log",
fileSize: 1024 * 1024 * 2,
mail: { // must set all neccesary properties.
subject: "Logs of My Website"
host: "smtp.mail.qq.com",
port: 25,
from: "xxxxxxxx@qq.com",
to: "xxxxxxxx@qq.com",
auth: {
username: "xxxxxxxx@qq.com",
password: "xxxxxxxx",
}
}
});If you don't set the mail option, when file's size up to limit, its contents
will be compressed to a GZip file and stored in a directory named according to
date.
Familiar Methods
logger.log()Logs a message on DEBUG level (aliaslogger.debug()).logger.info()Logs a message on INFO level.logger.warn()Logs a message on WARN level.logger.error()Logs a message on ERROR level.
These methods' usage are exactly the same as console's, if you're not
familiar with them, please check
https://nodejs.org/dist/latest-v8.x/docs/api/console.html.
Multi-Processing Scenario
Powered by open-channel, this package is safe in multi-processing scenario, and automatically prevent concurrency conflicts.
Close Logger
You can called the method logger.close() to close the logger, however, due to
using open-channel, which serves an internal IPC server that cannot be closed,
the program will not be able to exit automatically as usual, you have to
explicitly calling process.exit() in case to terminate the program.
// close the logger and terminate the program.
logger.close(() => {
process.exit();
});5 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago