use-term 1.1.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -35,6 +35,57 @@ npm install use-term
35
35
 
36
36
  ---
37
37
 
38
+ ## ⚙️ Configuration
39
+
40
+ You can customize the logger behavior by passing options when creating a new instance:
41
+
42
+ ```javascript
43
+ import { Logger } from "use-term";
44
+
45
+ // Disable all logs (useful for testing or production overrides)
46
+ const silentLogger = new Logger({ silent: true });
47
+ silentLogger.info("This won't be logged");
48
+
49
+ // Disable logs in specific environments (defaults to ["production"])
50
+ const logger = new Logger({
51
+ silentEnvironments: ["production", "staging"],
52
+ environment: process.env.NODE_ENV,
53
+ });
54
+
55
+ // Custom configuration
56
+ const customLogger = new Logger({
57
+ silent: false,
58
+ silentEnvironments: ["prod"],
59
+ environment: process.env.APP_ENV || "development",
60
+ });
61
+ ```
62
+
63
+ ### Configuration Options
64
+
65
+ | Option | Type | Default | Description |
66
+ |--------|------|---------|-------------|
67
+ | `silent` | boolean | `false` | If `true`, all logs are disabled |
68
+ | `silentEnvironments` | string[] | `["production"]` | Array of environments where logging is disabled |
69
+ | `environment` | string | `process.env.NODE_ENV \|\| "development"` | Current environment name |
70
+
71
+ **Example: Disable logs only in production**
72
+
73
+ ```javascript
74
+ const logger = new Logger(); // By default, logs are disabled when NODE_ENV=production
75
+ logger.info("This won't appear in production"); // Silent in production
76
+ ```
77
+
78
+ **Example: Disable logs in multiple environments**
79
+
80
+ ```javascript
81
+ const logger = new Logger({
82
+ silentEnvironments: ["production", "staging", "testing"],
83
+ });
84
+ // Logs will be silenced in any of these environments
85
+ ```
86
+
87
+ ---
88
+
38
89
  ## 🚀 Usage
39
90
 
40
91
  Since `use-term` is built with universal Node.js compatibility in mind, it works seamlessly in both modern **ES Modules (ESM)** and legacy **CommonJS** environments out-of-the-box without requiring separate bundles or transpilation.
@@ -44,26 +95,36 @@ Since `use-term` is built with universal Node.js compatibility in mind, it works
44
95
  For modern Node.js environments (projects with `"type": "module"` in `package.json`), TypeScript, Next.js, or Vite:
45
96
 
46
97
  ```javascript
47
- import term from "use-term";
98
+ import { Logger } from "use-term";
99
+
100
+ // Create a logger instance with default options
101
+ // Logs are disabled in production by default
102
+ const logger = new Logger();
103
+
104
+ // For custom environments:
105
+ const customLogger = new Logger({
106
+ environment: process.env.NODE_ENV,
107
+ silentEnvironments: ["production", "staging"],
108
+ });
48
109
 
49
110
  // --- A. Using inside a class (automatically gets class name) ---
50
111
  class AuthService {
51
112
  login() {
52
- term.info("Starting login process...", undefined, this);
113
+ logger.info("Starting login process...", undefined, this);
53
114
 
54
115
  const user = { id: 42, name: "Alex", roles: ["admin", "billing"] };
55
- term.success("Credentials validated successfully", user, this);
116
+ logger.success("Credentials validated successfully", user, this);
56
117
  }
57
118
  }
58
119
 
59
120
  // --- B. Using inside a function (automatically gets function name) ---
60
121
  function processPayment() {
61
- term.warn(
122
+ logger.warn(
62
123
  "Payment gateway is responding slowly",
63
124
  { delayMs: 1500 },
64
125
  processPayment,
65
126
  );
66
- term.error(
127
+ logger.error(
67
128
  "Payment gateway connection timeout",
68
129
  { code: "ETIMEDOUT" },
69
130
  processPayment,
@@ -71,14 +132,14 @@ function processPayment() {
71
132
  }
72
133
 
73
134
  // --- C. Using custom string scopes ---
74
- term.info("Server initialized on port 3000", { env: "production" }, "Server");
135
+ logger.info("Server initialized on port 3000", { env: "production" }, "Server");
75
136
 
76
137
  // --- D. Visual titles ---
77
- term.title("Authentication Flow");
138
+ logger.title("Authentication Flow");
78
139
  const auth = new AuthService();
79
140
  auth.login();
80
141
 
81
- term.title("Payment Flow");
142
+ logger.title("Payment Flow");
82
143
  processPayment();
83
144
  ```
84
145
 
@@ -89,20 +150,30 @@ processPayment();
89
150
  For traditional Node.js applications:
90
151
 
91
152
  ```javascript
92
- const term = require("use-term");
153
+ const { Logger } = require("use-term");
154
+
155
+ // Create a logger instance with default options
156
+ // Logs are disabled in production by default
157
+ const logger = new Logger();
158
+
159
+ // For custom environments:
160
+ const customLogger = new Logger({
161
+ environment: process.env.NODE_ENV,
162
+ silentEnvironments: ["prod"],
163
+ });
93
164
 
94
165
  // Easily log with CommonJS:
95
- term.info("Logging using standard require() syntax!", undefined, "CJS");
166
+ logger.info("Logging using standard require() syntax!", undefined, "CJS");
96
167
 
97
168
  class AuthService {
98
169
  login() {
99
- term.info("Starting login process...", undefined, this);
170
+ logger.info("Starting login process...", undefined, this);
100
171
  const user = { id: 42, name: "Alex", roles: ["admin", "billing"] };
101
- term.success("Credentials validated successfully", user, this);
172
+ logger.success("Credentials validated successfully", user, this);
102
173
  }
103
174
  }
104
175
 
105
- term.title("CommonJS Demo");
176
+ logger.title("CommonJS Demo");
106
177
  const auth = new AuthService();
107
178
  auth.login();
108
179
  ```
@@ -133,7 +204,7 @@ When you run your scripts, your terminal will light up with premium, high-visibi
133
204
 
134
205
  ## 🛠️ API Reference
135
206
 
136
- ### `term.info(message, [data], [context])`
207
+ ### `logger.info(message, [data], [context])`
137
208
 
138
209
  Logs an informational message.
139
210
 
@@ -141,19 +212,19 @@ Logs an informational message.
141
212
  - `data` `(any, optional)`: Any object, array, or metadata to inspect and format below.
142
213
  - `context` `(any, optional)`: `this` inside a class, a function name, or a plain string.
143
214
 
144
- ### `term.error(message, [data], [context])`
215
+ ### `logger.error(message, [data], [context])`
145
216
 
146
217
  Logs an error message. Same signature as `info()`.
147
218
 
148
- ### `term.success(message, [data], [context])`
219
+ ### `logger.success(message, [data], [context])`
149
220
 
150
221
  Logs a success message. Same signature as `info()`.
151
222
 
152
- ### `term.warn(message, [data], [context])`
223
+ ### `logger.warn(message, [data], [context])`
153
224
 
154
225
  Logs a warning message. Same signature as `info()`.
155
226
 
156
- ### `term.title(message)`
227
+ ### `logger.title(message)`
157
228
 
158
229
  Prints a highlighted, uppercase section divider to demarcate different stages in your runtime.
159
230
 
@@ -166,9 +237,11 @@ Prints a highlighted, uppercase section divider to demarcate different stages in
166
237
  Autocompletion works out of the box in VS Code and modern IDEs. If you are using TypeScript:
167
238
 
168
239
  ```typescript
169
- import term from "use-term";
240
+ import { Logger } from "use-term";
241
+
242
+ const logger = new Logger();
170
243
 
171
- term.info("Fully typed logger out of the box!", undefined, "TypeScript");
244
+ logger.info("Fully typed logger out of the box!", undefined, "TypeScript");
172
245
  ```
173
246
 
174
247
  ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-term",
3
- "version": "1.1.0",
3
+ "version": "2.1.0",
4
4
  "description": "A zero-dependency, ultra-lightweight, and beautiful terminal logging library for Node.js with smart context detection, colored outputs, and automatic timestamps.",
5
5
  "main": "src/term.js",
6
6
  "types": "src/term.d.ts",
@@ -37,5 +37,8 @@
37
37
  "license": "MIT",
38
38
  "engines": {
39
39
  "node": ">=12.0.0"
40
+ },
41
+ "scripts": {
42
+ "test": "node test/test.js"
40
43
  }
41
44
  }
package/src/term.d.ts CHANGED
@@ -1,7 +1,43 @@
1
+ /**
2
+ * Configuration options for the Logger.
3
+ */
4
+ export interface LoggerOptions {
5
+ /**
6
+ * If true, no logs will be printed regardless of environment.
7
+ * @default false
8
+ */
9
+ silent?: boolean;
10
+ /**
11
+ * Array of environments where logging should be disabled.
12
+ * @default ["production"]
13
+ */
14
+ silentEnvironments?: string[];
15
+ /**
16
+ * Current environment (defaults to NODE_ENV or "development").
17
+ * @default process.env.NODE_ENV || "development"
18
+ */
19
+ environment?: string;
20
+ }
21
+
1
22
  /**
2
23
  * A beautiful, simple, and dependency-free terminal logger for Node.js.
24
+ * Instantiate with `new Logger()` to create a logger instance with optional configuration.
25
+ *
26
+ * @example
27
+ * const logger = new Logger();
28
+ * logger.info("Hello world");
29
+ *
30
+ * @example
31
+ * const logger = new Logger({ silent: true });
32
+ * logger.info("This won't be logged");
33
+ *
34
+ * @example
35
+ * const logger = new Logger({ silentEnvironments: ["production", "staging"] });
36
+ * // Logs will be disabled if NODE_ENV is "production" or "staging"
3
37
  */
4
- declare class Term {
38
+ export class Logger {
39
+ constructor(options?: LoggerOptions);
40
+
5
41
  /**
6
42
  * Logs an informational message.
7
43
  * @param msg The message to log.
@@ -44,6 +80,3 @@ declare class Term {
44
80
  */
45
81
  title(msg: string): void;
46
82
  }
47
-
48
- declare const term: Term;
49
- export = term;
package/src/term.js CHANGED
@@ -1,5 +1,11 @@
1
1
  const util = require("util");
2
2
 
3
+ const DEFAULT_OPTIONS = {
4
+ silent: false,
5
+ silentEnvironments: ["production"],
6
+ environment: process.env.NODE_ENV || "development",
7
+ };
8
+
3
9
  const COLORS = {
4
10
  info: "\x1b[36m",
5
11
  error: "\x1b[31m",
@@ -12,7 +18,22 @@ const COLORS = {
12
18
  bold: "\x1b[1m",
13
19
  };
14
20
 
15
- class Term {
21
+ class Logger {
22
+ constructor(options = {}) {
23
+ this.config = {
24
+ ...DEFAULT_OPTIONS,
25
+ ...options,
26
+ };
27
+ }
28
+
29
+ _shouldLog() {
30
+ if (this.config.silent) return false;
31
+ if (this.config.silentEnvironments.includes(this.config.environment)) {
32
+ return false;
33
+ }
34
+ return true;
35
+ }
36
+
16
37
  _timestamp() {
17
38
  const now = new Date();
18
39
  const time = `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}`;
@@ -46,6 +67,8 @@ class Term {
46
67
  }
47
68
 
48
69
  _log(type, color, icon, context, msg, data) {
70
+ if (!this._shouldLog()) return;
71
+
49
72
  const ctxName = this._getContextName(context);
50
73
  const timestamp = this._timestamp();
51
74
  const tag = `${color}${icon} ${type}${COLORS.reset}`;
@@ -100,10 +123,14 @@ class Term {
100
123
  }
101
124
 
102
125
  title(msg) {
126
+ if (!this._shouldLog()) return;
127
+
103
128
  console.log(
104
129
  `\n${COLORS.magenta}${COLORS.bold}━━━ ${msg.toUpperCase()} ━━━${COLORS.reset}`,
105
130
  );
106
131
  }
107
132
  }
108
133
 
109
- module.exports = new Term();
134
+ module.exports = Logger;
135
+ module.exports.Logger = Logger;
136
+ module.exports.default = Logger;