@node-cli/logger
Advanced tools
+157
| # Node CLI Logger | ||
|  | ||
| > Logger is a dead-simple console logger for nodejs command-line applications. | ||
| ## Installation | ||
| ```sh | ||
| > cd your-project | ||
| > npm install --save-dev @node-cli/logger | ||
| ``` | ||
| ## Usage | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger(); | ||
| log.info("this is an informational log"); | ||
| log.warn("this is a warning log"); | ||
| log.error("this is an error log"); | ||
| ``` | ||
| ## API | ||
| ### Methods | ||
| Logger relies on `console` behind the scenes, and therefore supports the same [string substitution](https://developer.mozilla.org/en-US/docs/Web/API/console#Using_string_substitutions) capabilities and uses the following methods: | ||
| | Method | Description | Output color | | ||
| | ------ | --------------------------------------------------------- | ------------ | | ||
| | debug | Outputs a message to the console with the log level debug | grey | | ||
| | log | For general output of logging information. | white | | ||
| | info | Informative logging of information. | blue | | ||
| | warn | Outputs a message to the console with the log level debug | yellow | | ||
| | error | Outputs an error message. | red | | ||
| ### Options | ||
| #### Disabling logging | ||
| You can disable logging with `silent`: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger(); | ||
| log.info("this will be logged"); | ||
| // disabling logs in production for example | ||
| log.silent = process.env.NODE_ENV === "production"; | ||
| log.info("but this will not"); | ||
| log.silent = false; | ||
| log.info("this will be logged again!"); | ||
| ``` | ||
| This option can also be passed to the constructor: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger({ silent: true }); | ||
| log.info("this will not be logged"); | ||
| log.silent = false; | ||
| log.info("this will be logged again!"); | ||
| ``` | ||
| ### Disabling colors | ||
| You can disable colors with `boring`: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger(); | ||
| log.info("this will be logged in the default [info] color"); | ||
| // disabling colors in test mode for example | ||
| log.boring = process.env.NODE_ENV === "test"; | ||
| log.info("but this will not have any colors :/"); | ||
| log.boring = false; | ||
| log.info("colors are back!"); | ||
| ``` | ||
| This option can also be passed to the constructor: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger({ boring: true }); | ||
| log.info("this will not be logged in color"); | ||
| log.boring = false; | ||
| log.info("this will be logged again!"); | ||
| ``` | ||
| ### Adding a prefix | ||
| You can add a prefix to the logs with `prefix`: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger(); | ||
| log.info("this will be logged with no prefix"); | ||
| log.prefix = "[INFO]"; | ||
| log.info("this will have a prefix!"); | ||
| ``` | ||
| The output of that last line would be: | ||
| ```sh | ||
| > [INFO] this will have a prefix! | ||
| ``` | ||
| This option can also be passed to the constructor: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger({ prefix: "Log:" }); | ||
| log.info("this will be logged with a prefix"); | ||
| log.prefix = false; | ||
| log.info("this will be NOT be logged with a prefix"); | ||
| ``` | ||
| ### Adding a local timestamp | ||
| You can add a timestamp to the logs with `timestamp`: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger(); | ||
| log.info("this will be logged with no timestamp"); | ||
| log.timestamp = true; | ||
| log.info("this will have a timestamp!"); | ||
| ``` | ||
| The output of that last line would look like: | ||
| ```sh | ||
| > [ Tue Feb 02 2021 8:32:58 PM ] this will have a timestamp! | ||
| ``` | ||
| This option can also be passed to the constructor: | ||
| ```js | ||
| import { Logger } from "@node-cli/logger"; | ||
| const log = new Logger({ timestamp: true }); | ||
| log.info("this will be logged with a timestamp"); | ||
| log.timestamp = false; | ||
| log.info("this will be NOT be logged with a timestamp"); | ||
| ``` | ||
| ## License | ||
| MIT © Arno Versini |
+6
-17
| export declare class Logger { | ||
| shouldLog: boolean; | ||
| globalPrefix: string; | ||
| showTimestamp: boolean; | ||
| printOptions: { | ||
| colors: boolean; | ||
| compact: boolean; | ||
| depth: number; | ||
| }; | ||
| #private; | ||
| constructor({ boring, silent, prefix, timestamp, }?: { | ||
@@ -20,11 +13,7 @@ boring?: boolean; | ||
| set timestamp(flag: boolean); | ||
| _log(type: { | ||
| method: string | number; | ||
| color: (arg0: any) => any; | ||
| }, ...args: string[]): void; | ||
| info(...args: any): void; | ||
| log(...args: any): void; | ||
| debug(...args: any): void; | ||
| warn(...args: any): void; | ||
| error(...args: any): void; | ||
| info(...arguments_: any): void; | ||
| log(...arguments_: any): void; | ||
| debug(...arguments_: any): void; | ||
| warn(...arguments_: any): void; | ||
| error(...arguments_: any): void; | ||
| } |
+38
-38
| import kleur from "kleur"; | ||
| import util from "node:util"; | ||
| export class Logger { | ||
| shouldLog; | ||
| globalPrefix; | ||
| showTimestamp; | ||
| printOptions; | ||
| #shouldLog; | ||
| #globalPrefix; | ||
| #showTimestamp; | ||
| #printOptions; | ||
| constructor({ boring =false , silent =false , prefix ="" , timestamp =false } = {}){ | ||
| this.shouldLog = !silent; | ||
| this.globalPrefix = prefix; | ||
| this.showTimestamp = timestamp; | ||
| this.printOptions = { | ||
| this.#shouldLog = !silent; | ||
| this.#globalPrefix = prefix; | ||
| this.#showTimestamp = timestamp; | ||
| this.#printOptions = { | ||
| colors: !boring, | ||
@@ -19,61 +19,61 @@ compact: false, | ||
| set silent(flag) { | ||
| this.shouldLog = !flag; | ||
| this.#shouldLog = !flag; | ||
| } | ||
| set boring(flag) { | ||
| this.printOptions.colors = !flag; | ||
| this.#printOptions.colors = !flag; | ||
| } | ||
| set prefix(prefix) { | ||
| this.globalPrefix = prefix; | ||
| this.#globalPrefix = prefix; | ||
| } | ||
| set timestamp(flag) { | ||
| this.showTimestamp = flag; | ||
| this.#showTimestamp = flag; | ||
| } | ||
| _log(type, ...args) { | ||
| if (this.shouldLog) { | ||
| let msg; | ||
| if (!this.showTimestamp && !this.globalPrefix) { | ||
| msg = util.formatWithOptions(this.printOptions, ...args); | ||
| #_log(type, ...arguments_) { | ||
| if (this.#shouldLog) { | ||
| let message; | ||
| if (!this.#showTimestamp && !this.#globalPrefix) { | ||
| message = util.formatWithOptions(this.#printOptions, ...arguments_); | ||
| } else { | ||
| const prefix = this.globalPrefix ? [ | ||
| this.globalPrefix | ||
| const prefix = this.#globalPrefix ? [ | ||
| this.#globalPrefix | ||
| ] : []; | ||
| if (this.showTimestamp) { | ||
| if (this.#showTimestamp) { | ||
| const now = new Date(); | ||
| prefix.push(this.printOptions.colors ? `${kleur.grey(`[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`)}` : `[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`); | ||
| prefix.push(this.#printOptions.colors ? `${kleur.grey(`[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`)}` : `[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`); | ||
| } | ||
| msg = util.formatWithOptions(this.printOptions, prefix.join(" "), ...args); | ||
| message = util.formatWithOptions(this.#printOptions, prefix.join(" "), ...arguments_); | ||
| } | ||
| console[type.method](this.printOptions.colors ? `${type.color(msg)}` : msg); | ||
| console[type.method](this.#printOptions.colors ? `${type.color(message)}` : message); | ||
| } | ||
| } | ||
| info(...args) { | ||
| this._log({ | ||
| info(...arguments_) { | ||
| this.#_log({ | ||
| method: "info", | ||
| color: kleur.blue | ||
| }, ...args); | ||
| }, ...arguments_); | ||
| } | ||
| log(...args) { | ||
| this._log({ | ||
| log(...arguments_) { | ||
| this.#_log({ | ||
| method: "log", | ||
| color: kleur.white | ||
| }, ...args); | ||
| }, ...arguments_); | ||
| } | ||
| debug(...args) { | ||
| this._log({ | ||
| debug(...arguments_) { | ||
| this.#_log({ | ||
| method: "debug", | ||
| color: kleur.grey | ||
| }, ...args); | ||
| }, ...arguments_); | ||
| } | ||
| warn(...args) { | ||
| this._log({ | ||
| warn(...arguments_) { | ||
| this.#_log({ | ||
| method: "warn", | ||
| color: kleur.yellow | ||
| }, ...args); | ||
| }, ...arguments_); | ||
| } | ||
| error(...args) { | ||
| this._log({ | ||
| error(...arguments_) { | ||
| this.#_log({ | ||
| method: "error", | ||
| color: kleur.red | ||
| }, ...args); | ||
| }, ...arguments_); | ||
| } | ||
| } |
+7
-5
| { | ||
| "name": "@node-cli/logger", | ||
| "version": "0.0.4", | ||
| "version": "0.0.5", | ||
| "license": "MIT", | ||
@@ -13,2 +13,3 @@ "author": "Arno Versini", | ||
| ], | ||
| "node": ">=16", | ||
| "dependencies": { | ||
@@ -18,9 +19,10 @@ "kleur": "4.1.5" | ||
| "scripts": { | ||
| "build": "npm-run-all --serial clean build:types build:js build:copy", | ||
| "build:copy": "copyfiles -f types/* dist/", | ||
| "build": "npm-run-all --serial clean build:types build:js", | ||
| "build:js": "swc --out-dir dist src", | ||
| "build:types": "tsc", | ||
| "clean": "rimraf dist types coverage", | ||
| "lint": "eslint \"src/*.ts\"", | ||
| "test": "jest", | ||
| "test:coverage": "npm run test -- --coverage" | ||
| "test:coverage": "npm run test -- --coverage", | ||
| "watch": "swc --watch --out-dir dist src" | ||
| }, | ||
@@ -30,3 +32,3 @@ "publishConfig": { | ||
| }, | ||
| "gitHead": "2b5ec875535713efa7352e93a4be142381c6c830" | ||
| "gitHead": "6e7950590077d5934bf84c676f533696139f8eb9" | ||
| } |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
8627
80.1%5
25%1
-50%158
Infinity%96
-10.28%