@ecopages/logger
Advanced tools
+15
-0
@@ -96,3 +96,18 @@ declare const Level: { | ||
| private logInternal; | ||
| /** | ||
| * Checks if debug mode is currently enabled. | ||
| * @returns {boolean} - True if debug mode is enabled, false otherwise. | ||
| */ | ||
| isDebugEnabled(): boolean; | ||
| /** | ||
| * Starts a timer with a label only if debug mode is enabled. | ||
| * @param label The label for the timer. | ||
| */ | ||
| debugTime(label: string): void; | ||
| /** | ||
| * Ends a timer with a label only if debug mode is enabled. | ||
| * @param label The label for the timer. | ||
| */ | ||
| debugTimeEnd(label: string): void; | ||
| } | ||
| export {}; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| var z={INFO:"INFO",ERROR:"ERROR",WARN:"WARN",DEBUG:"DEBUG",TIMER:"TIMER"},V={level:z.INFO},X={level:z.ERROR},Y={level:z.WARN},Z={level:z.DEBUG},J=typeof window!=="undefined",_={INFO:"color: #00ff00",ERROR:"color: #ff0000",WARN:"color: #ffff00",DEBUG:"color: #00ffff",TIMER:"color: #ff00ff"},$={INFO:"\x1B[32m",ERROR:"\x1B[31m",WARN:"\x1B[33m",DEBUG:"\x1B[36m",TIMER:"\x1B[35m"};class y{prefix;colors;options={debug:!1,color:!0,timestampFormat:"time",verboseTimer:!1};timers=new Map;constructor(j,k){if(this.prefix=j,k)this.options={...this.options,...k};this.colors={...J?_:$,...k?.colors}}info(...j){return this.logInternal(V,...j),this}warn(...j){return this.logInternal(Y,...j),this}error(...j){return this.logInternal(X,...j),this}debug(...j){if(this.options.debug)this.logInternal(Z,...j);return this}time(j,k=z.TIMER){if(!this.options.color){console.time(`${this.prefix} ${j}`);return}if(this.timers.set(j,performance.now()),this.options.verboseTimer)if(J)console.log(`%c${this.prefix} ${j}: start`,this.getColor(k));else{let H=this.getColor(k);console.log(`${H}${this.prefix} ${j}: start\x1B[0m`)}}timeEnd(j,k=z.TIMER){if(!this.options.color){console.timeEnd(`${this.prefix} ${j}`);return}let H=this.timers.get(j);if(H===void 0){console.warn(`Timer '${this.prefix} ${j}' does not exist`);return}let q=performance.now()-H;if(this.timers.delete(j),J)console.log(`%c${this.prefix} ${j}: ${q.toFixed(2)}ms`,this.getColor(k));else{let K=this.getColor(k);console.log(`${K}${this.prefix} ${j}: ${q.toFixed(2)}ms\x1B[0m`)}}getTimestamp(){if(!this.options.timestamp)return"";let j=new Date,k;switch(this.options.timestampFormat){case"full":k={year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1};break;case"short":k={month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1};break;default:k={hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1}}return`[${j.toLocaleString("en-US",k)}] `}getColor(j){return this.colors[j]??""}getFormatSpecifier(j){if(j===null)return"%s";if(j===void 0)return"%s";switch(typeof j){case"object":return"%o";case"number":return"%d";case"boolean":return"%s";default:return"%s"}}logInternal(j,...k){let q=this.getTimestamp()+this.prefix;if(!this.options.color){console.log(q,...k);return}if(J){let P=j?this.getColor(j.level):"",Q=k.map((S)=>this.getFormatSpecifier(S));console.log(`%c${q} ${Q.join(" ")}`,P,...k);return}let K=j?this.getColor(j.level):"",M="\x1B[0m";console.log(`${K}${q}`,...k,M)}}export{y as Logger}; | ||
| var q={INFO:"INFO",ERROR:"ERROR",WARN:"WARN",DEBUG:"DEBUG",TIMER:"TIMER"},V={level:q.INFO},X={level:q.ERROR},Y={level:q.WARN},Z={level:q.DEBUG},J=typeof window!=="undefined",_={INFO:"color: #00ff00",ERROR:"color: #ff0000",WARN:"color: #ffff00",DEBUG:"color: #00ffff",TIMER:"color: #ff00ff"},$={INFO:"\x1B[32m",ERROR:"\x1B[31m",WARN:"\x1B[33m",DEBUG:"\x1B[36m",TIMER:"\x1B[35m"};class A{prefix;colors;options={debug:!1,color:!0,timestampFormat:"time",verboseTimer:!1};timers=new Map;constructor(j,k){if(this.prefix=j,k)this.options={...this.options,...k};this.colors={...J?_:$,...k?.colors}}info(...j){return this.logInternal(V,...j),this}warn(...j){return this.logInternal(Y,...j),this}error(...j){return this.logInternal(X,...j),this}debug(...j){if(this.options.debug)this.logInternal(Z,...j);return this}time(j,k=q.TIMER){if(!this.options.color){console.time(`${this.prefix} ${j}`);return}if(this.timers.set(j,performance.now()),this.options.verboseTimer)if(J)console.log(`%c${this.prefix} ${j}: start`,this.getColor(k));else{let H=this.getColor(k);console.log(`${H}${this.prefix} ${j}: start\x1B[0m`)}}timeEnd(j,k=q.TIMER){if(!this.options.color){console.timeEnd(`${this.prefix} ${j}`);return}let H=this.timers.get(j);if(H===void 0){console.warn(`Timer '${this.prefix} ${j}' does not exist`);return}let z=performance.now()-H;if(this.timers.delete(j),J)console.log(`%c${this.prefix} ${j}: ${z.toFixed(2)}ms`,this.getColor(k));else{let K=this.getColor(k);console.log(`${K}${this.prefix} ${j}: ${z.toFixed(2)}ms\x1B[0m`)}}getTimestamp(){if(!this.options.timestamp)return"";let j=new Date,k;switch(this.options.timestampFormat){case"full":k={year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1};break;case"short":k={month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1};break;default:k={hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1}}return`[${j.toLocaleString("en-US",k)}] `}getColor(j){return this.colors[j]??""}getFormatSpecifier(j){if(j===null)return"%s";if(j===void 0)return"%s";switch(typeof j){case"object":return"%o";case"number":return"%d";case"boolean":return"%s";default:return"%s"}}logInternal(j,...k){let z=this.getTimestamp()+this.prefix;if(!this.options.color){console.log(z,...k);return}if(J){let P=j?this.getColor(j.level):"",Q=k.map((S)=>this.getFormatSpecifier(S));console.log(`%c${z} ${Q.join(" ")}`,P,...k);return}let K=j?this.getColor(j.level):"",M="\x1B[0m";console.log(`${K}${z}`,...k,M)}isDebugEnabled(){return!!this.options.debug}debugTime(j){if(this.isDebugEnabled())this.time(j,q.DEBUG)}debugTimeEnd(j){if(this.isDebugEnabled())this.timeEnd(j,q.DEBUG)}}export{A as Logger}; |
+1
-1
| { | ||
| "name": "@ecopages/logger", | ||
| "version": "0.2.1", | ||
| "version": "0.2.2", | ||
| "license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "./dist/logger.js", |
+71
-35
@@ -11,2 +11,3 @@ # @ecopages/logger | ||
| - **Timer Support**: Built-in timer functionality for performance measurements | ||
| - **Debug Utilities**: Helper methods for conditional debug operations | ||
| - **Prefixed Messages**: Customizable prefix for all log messages | ||
@@ -47,11 +48,12 @@ - **Environment Detection**: Automatically adapts output format for browser or Node.js environments | ||
| const logger = new Logger("[my-app]", { | ||
| debug: true, // Enable debug messages | ||
| color: true, // Enable colored output | ||
| timestamp: true, // Add timestamps to messages | ||
| timestampFormat: 'full', // Configure timestamp format | ||
| verboseTimer: true, // Show timer start messages | ||
| colors: { // Custom colors | ||
| INFO: 'color: purple', | ||
| ERROR: 'color: darkred' | ||
| } | ||
| debug: true, // Enable debug messages | ||
| color: true, // Enable colored output | ||
| timestamp: true, // Add timestamps to messages | ||
| timestampFormat: "full", // Configure timestamp format | ||
| verboseTimer: true, // Show timer start messages | ||
| colors: { | ||
| // Custom colors | ||
| INFO: "color: purple", | ||
| ERROR: "color: darkred", | ||
| }, | ||
| }); | ||
@@ -62,10 +64,10 @@ ``` | ||
| | Option | Type | Default | Description | | ||
| |--------|------|---------|-------------| | ||
| | `debug` | boolean | `false` | Enable debug level messages | | ||
| | `color` | boolean | `true` | Enable colored output | | ||
| | `timestamp` | boolean | `false` | Add timestamps to messages | | ||
| | `timestampFormat` | 'full' \| 'time' \| 'short' | 'time' | Timestamp format | | ||
| | `verboseTimer` | boolean | `false` | Show timer start messages | | ||
| | `colors` | Partial<ColorConfig> | - | Custom colors for log levels | | ||
| | Option | Type | Default | Description | | ||
| | ----------------- | --------------------------- | ------- | ---------------------------- | | ||
| | `debug` | boolean | `false` | Enable debug level messages | | ||
| | `color` | boolean | `true` | Enable colored output | | ||
| | `timestamp` | boolean | `false` | Add timestamps to messages | | ||
| | `timestampFormat` | "full" \| "time" \| "short" | "time" | Timestamp format | | ||
| | `verboseTimer` | boolean | `false` | Show timer start messages | | ||
| | `colors` | Partial<ColorConfig> | - | Custom colors for log levels | | ||
@@ -90,2 +92,21 @@ ### Timestamp Formats | ||
| ## Debug Utilities | ||
| The logger provides several debug-specific methods: | ||
| ```ts | ||
| // Check if debug mode is enabled | ||
| if (logger.isDebugEnabled()) { | ||
| // Perform debug-only operations | ||
| } | ||
| // Start a timer only when debug is enabled | ||
| logger.debugTime("debug-operation"); | ||
| // ... some operations ... | ||
| // End the debug timer (only logs when debug is enabled) | ||
| logger.debugTimeEnd("debug-operation"); | ||
| ``` | ||
| ## Custom Colors | ||
@@ -98,9 +119,9 @@ | ||
| const logger = new Logger("[my-app]", { | ||
| colors: { | ||
| INFO: 'color: purple', | ||
| ERROR: 'color: darkred', | ||
| WARN: 'color: orange', | ||
| DEBUG: 'color: cyan', | ||
| TIMER: 'color: magenta' | ||
| } | ||
| colors: { | ||
| INFO: "color: purple", | ||
| ERROR: "color: darkred", | ||
| WARN: "color: orange", | ||
| DEBUG: "color: cyan", | ||
| TIMER: "color: magenta", | ||
| }, | ||
| }); | ||
@@ -110,9 +131,9 @@ | ||
| const logger = new Logger("[my-app]", { | ||
| colors: { | ||
| INFO: '\x1b[35m', // Purple | ||
| ERROR: '\x1b[31m', // Red | ||
| WARN: '\x1b[33m', // Yellow | ||
| DEBUG: '\x1b[36m', // Cyan | ||
| TIMER: '\x1b[35m' // Magenta | ||
| } | ||
| colors: { | ||
| INFO: "\x1b[35m", // Purple | ||
| ERROR: "\x1b[31m", // Red | ||
| WARN: "\x1b[33m", // Yellow | ||
| DEBUG: "\x1b[36m", // Cyan | ||
| TIMER: "\x1b[35m", // Magenta | ||
| }, | ||
| }); | ||
@@ -126,5 +147,5 @@ ``` | ||
| ```ts | ||
| const logger = new Logger("[my-app]", { | ||
| timestamp: true, | ||
| timestampFormat: 'full' | ||
| const logger = new Logger("[my-app]", { | ||
| timestamp: true, | ||
| timestampFormat: "full", | ||
| }); | ||
@@ -154,2 +175,17 @@ | ||
| // Output: [my-app] db-query: 123.45ms | ||
| ``` | ||
| ``` | ||
| ### With Conditional Debug Timers | ||
| ```ts | ||
| const logger = new Logger("[my-app]", { debug: true }); | ||
| // Only starts timer if debug is enabled | ||
| logger.debugTime("expensive-calculation"); | ||
| // Some expensive operation | ||
| const result = performExpensiveCalculation(); | ||
| // Only logs time if debug is enabled | ||
| logger.debugTimeEnd("expensive-calculation"); | ||
| ``` |
12996
14.57%123
14.95%184
25.17%