1.0.8 • Published 3 years ago

jsloggers v1.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

jsloggers

A javascript logging library

Output

image

Usage

// if package is installed through "npm install jsloggers"
const { createLog, CONST: { FCRED, FCGREEN, FCYELLOW, FCBLUE, FCNORMAL, NEWLINE } } = require('jsloggers');

// or downloading the jsloggers.js file only
//const { createLog, CONST: { FCRED, FCGREEN, FCYELLOW, FCBLUE, FCNORMAL, NEWLINE } } 
//    = require(`${__dirname}/jsloggers.js`);

// if you want to put the log files into directory ${__dirname}/log, please make sure 
// ${__dirname}/log exists first. jsloggers is not responsible for creating any dirs.
const log = createLog({
    writer: process.stdout.write.bind(process.stdout),
    timestampFormat: "YYYY-MM-DD HH:mm:ss",
  }, {
    logFilePath: `${__dirname}/log/app.log`,
    timestampFormat: "YYYY-MM-DD HH:mm:ss.SSS",
  });

log.error("1.this error log is for both console (+newline) and file (+newline)");
log.debug("2.this debug log is for both console (+newline) and file (+newline)");

// By default, log.sync === false, and any logging operations will be put into microtasks 
// by process.nextTick() and be scheduled to execute in next tick.
// Here, enabling log.sync will switch over from async logging to sync logging. It means
// you will see the logging outputs immediately once any logging function is called after
// this time point.
// In production environment, async logging is sugggested because of higher throughput and
// performance.
// log.sync is an accessor property, and there is another method called log.setSync does the 
// same. Either one works.
log.sync = true;

log.console.fatal("3.this fatal log is for console only (no newline)");
log.consoleln.warn("4.this warn log is for console only (+newline)");
log.file.debug("5.this debug log is for file only (no newline)");
log.file.warn("6.this warn log is for file only (no newline)");
log.fileln.fatal("7.this fatal log is for file only (+newline)");
log.fileln.trace("8.this trace log is for file only (+newline)");

// enabling async logging again
log.sync = false;

log.write.info("9.this info log is for both console (no newline) and file (no newline)");
log.write.debug("10.this debug log is for both console (no newline) and file (no newline)");
log.writeln.warn("11.this warn log is for both console (+newline) and file (+newline)");
log.console.resetLine().info("12.reset console and output log (without adding a new line)");

// add an empty new line in file log only
log.file.newLine();

// reset console and output color-font logs
log.console.resetLine().info(FCRED + "RED text " + FCNORMAL + FCYELLOW + "YELLOW text" + FCNORMAL);

// for both console and file,
// in this case, the FONT COLOR escape characters only poses an effect on console, 
// and those escape characters will be removed automatically before being writing to file
log.info(FCRED + "RED text " + FCNORMAL + FCYELLOW + "YELLOW text" + FCNORMAL);

// close all log-file streams before app ends
(async () => {
  await log.closeFile();
  console.log("App ends.");
})();

Notes

Configuration for stdout when debugging in IDE environment

You might see nothing (no console output) when debugging jsloggers in an IDE environment like Visual Studio Code. The following configuration (in launch.json) works like a charm in VSCode:

"console": "integratedTerminal"

Other IDEs may have similar options.

Align output fields using String.prototype.padEnd() and String.prototype.padStart()

Arranging left or right aligned fields is a good way to produce visually-friendly outputs. String.prototype.padEnd() and String.prototype.padStart() satisfy this type of needs.

log.info(field1.padEnd(30) + field2.padEnd(28) + field3.padEnd(36));
log.info(field1.padStart(30) + field2.padStart(28) + field3.padStart(36));

Flexibility

jsloggers provides a bunch of logging and configuration functions to cope with different situations.

log.sync = true, log.sync = false
enabling sync logging or enabling async logging 
log.info, log.warn, log.fatal ...... 
produce logs ending with a new-line character, for both console and file.
log.console.info, log.console.warn, log.console.fatal ...... 
produce logs without ending new-line character, for console only.
log.consoleln.info, log.consoleln.warn, log.consoleln.fatal ...... 
produce logs ending with a new-line character, for console only.
log.file.info, log.file.warn, log.file.fatal ...... 
produce logs without ending new-line character, for file only.
log.fileln.info, log.fileln.warn, log.fileln.fatal ...... 
produce logs ending with a new-line character, for file only.
log.write.info, log.write.warn, log.write.fatal ...... 
produce logs without ending new-line character, for both console and file.
log.writeln.info(=log.info), log.writeln.warn(=log.warn), log.writeln.fatal(=log.fatal) ...... 
produce logs ending with a new-line character, for both console and file.

log.write.info(something + "\n")=log.writeln.info(something) ......
produce logs ending with a new-line character, for both console and file.
log.write.newLine() = log.writeln.newLine()
produce an empty line, for both console and file
log.info() without providing any parameters
produce an empty line, with leading level information goes like this: [INFO]
log.closeFile
close all log-file streams before app ends

Always one option fit for you. 总有一款适合你.

Features

1.支持日志分级管理(level,如trace, error, warn, debug, info等等)。simple-node-logger原有特性。

2.一条log语句同时输出到console和file(console and file),而不需要写两条语句来达到目的,后者很容易漏掉日志输出语句。另外,jsloggers也支持单方向输出(console or file)。

3.能分别定义输出到console和file的信息格式(如时间戳格式等),以满足console和file对日志的不同格式要求(如console日志有屏幕宽度限制,文件日志不带颜色属性等)。simple-node-logger原有特性。

4.能向console输出带颜色和格式控制等特殊escape字符串的内容,而同一份内容输出到file时要能过滤掉这些escape字符串,不至于打乱file日志。

5.能够指定日志文件名称,能支持按时间rolling文件名。simple-node-logger原有特性。

6.console和file两个方向都支持不换行和换行日志,都能输出带level信息的(如INFO)和不带level信息的空行。

7.console日志支持resetLine功能,即在控制台的同一行上不断刷新信息,而不增加新行。

8.支持创建、管理和获取多个不同的日志,比如说针对app全局设定一个global日志,针对网络管理设定一个network_log日志,针对文件管理设定一个file_log,针对业务逻辑设定一个business_log,这些log都可以输出到console,另外还可以分别输出到不同文件中,日志格式也可以单独定义。在node.js main app module中执行的时候可以使用global logger,而进入各分项module的时候可以使用各自的分项logger,当然各模块也可以交叉获取和使用所有这些logger。在整个app的生命周期中,这些logger都需要遵循一次创建(Create),无限次使用(Get)的原则,而不会被重复创建。

9.支持同步和异步日志。同步日志直观而迅速,但过多的同步日志操作会阻塞和影响整个应用的性能。异步日志没有那么直观,但将日志操作放入task queue或micro task queue,使得整个应用的执行更加顺畅,性能和整体吞吐率都能得到提升。jsloggers继承了simple-node-logger的异步日志功能,同时通过log.sync标志实现同步日志功能,并支持在同步与异步日志间自由切换。

10.可控结束。在整个app结束时,能够有序关闭各文件日志stream,从而将buffer的日志信息都flush到相应的文件中,以免丢失信息。

11.i18n国际化支持。完善中,目前还不具备i18n日志能力。

Acknowledgements

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago