@ecopages/logger
Advanced tools
| /** | ||
| * Represents a logger that can be used to log messages with different log levels. | ||
| */ | ||
| export declare class Logger { | ||
| private readonly prefix; | ||
| /** | ||
| * Creates a new instance of the Logger class. | ||
| * @param prefix The prefix to be added to each log message. | ||
| */ | ||
| constructor(prefix: string); | ||
| /** | ||
| * Logs an informational message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| info(...args: any[]): void; | ||
| /** | ||
| * Logs a warning message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| warn(...args: any[]): void; | ||
| /** | ||
| * Logs an error message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| error(...args: any[]): void; | ||
| /** | ||
| * Logs a debug message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| debug(...args: any[]): void; | ||
| private logInternal; | ||
| } |
| var k;(function(q){q["INFO"]="INFO";q["ERROR"]="ERROR";q["WARN"]="WARN";q["DEBUG"]="DEBUG"})(k||(k={}));var K={level:k.INFO},M={level:k.ERROR},P={level:k.WARN},Q={level:k.DEBUG};class V{prefix;constructor(j){this.prefix=j}info(...j){this.logInternal(K,...j)}warn(...j){this.logInternal(P,...j)}error(...j){this.logInternal(M,...j)}debug(...j){if(process.env.ECO_PAGES_DEBUG==="true")this.logInternal(Q,...j)}logInternal(j,...H){const z=j?{[k.INFO]:"\x1B[32m",[k.ERROR]:"\x1B[31m",[k.WARN]:"\x1B[33m",[k.DEBUG]:"\x1B[36m"}[j.level]:"",J=`${z?z:""}${this.prefix}`;console.log(J,...H,"\x1B[0m")}}export{V as Logger}; |
+2
-2
| { | ||
| "name": "@ecopages/logger", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "license": "MIT", | ||
@@ -8,3 +8,3 @@ "main": "./dist/logger.js", | ||
| "type": "module", | ||
| "files": ["src"], | ||
| "files": ["dist"], | ||
| "repository": { | ||
@@ -11,0 +11,0 @@ "type": "git", |
| import { beforeEach, describe, expect, it, spyOn } from 'bun:test'; | ||
| import { Logger } from './logger'; | ||
| describe('Logger', () => { | ||
| let logger: Logger; | ||
| beforeEach(() => { | ||
| logger = new Logger('Test'); | ||
| }); | ||
| it('should log info message', () => { | ||
| const consoleSpy = spyOn(console, 'log'); | ||
| logger.info('Info message'); | ||
| expect(consoleSpy).toHaveBeenCalledWith('\x1b[32mTest', 'Info message', '\x1b[0m'); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
| it('should log warning message', () => { | ||
| const consoleSpy = spyOn(console, 'log'); | ||
| logger.warn('Warning message'); | ||
| expect(consoleSpy).toHaveBeenCalledWith('\x1b[33mTest', 'Warning message', '\x1b[0m'); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
| it('should log error message', () => { | ||
| const consoleSpy = spyOn(console, 'log'); | ||
| logger.error('Error message'); | ||
| expect(consoleSpy).toHaveBeenCalledWith('\x1b[31mTest', 'Error message', '\x1b[0m'); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
| it('should log debug message when debugActive is true', () => { | ||
| const consoleSpy = spyOn(console, 'log'); | ||
| import.meta.env.ECO_PAGES_DEBUG = 'true'; | ||
| logger.debug('Debug message'); | ||
| expect(consoleSpy).toHaveBeenCalledWith('\x1b[36mTest', 'Debug message', '\x1b[0m'); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
| it('should not log debug message when debugActive is false', () => { | ||
| const consoleSpy = spyOn(console, 'log'); | ||
| import.meta.env.ECO_PAGES_DEBUG = 'false'; | ||
| logger.debug('Debug message'); | ||
| expect(consoleSpy).not.toHaveBeenCalled(); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
| }); |
| enum Level { | ||
| INFO = 'INFO', | ||
| ERROR = 'ERROR', | ||
| WARN = 'WARN', | ||
| DEBUG = 'DEBUG', | ||
| } | ||
| interface LogLevel { | ||
| level: Level; | ||
| } | ||
| const INFO: LogLevel = { level: Level.INFO }; | ||
| const ERROR: LogLevel = { level: Level.ERROR }; | ||
| const WARN: LogLevel = { level: Level.WARN }; | ||
| const DEBUG: LogLevel = { level: Level.DEBUG }; | ||
| /** | ||
| * Represents a logger that can be used to log messages with different log levels. | ||
| */ | ||
| export class Logger { | ||
| private readonly prefix: string; | ||
| /** | ||
| * Creates a new instance of the Logger class. | ||
| * @param prefix The prefix to be added to each log message. | ||
| */ | ||
| constructor(prefix: string) { | ||
| this.prefix = prefix; | ||
| } | ||
| /** | ||
| * Logs an informational message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| info(...args: any[]) { | ||
| this.logInternal(INFO, ...args); | ||
| } | ||
| /** | ||
| * Logs a warning message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| warn(...args: any[]) { | ||
| this.logInternal(WARN, ...args); | ||
| } | ||
| /** | ||
| * Logs an error message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| error(...args: any[]) { | ||
| this.logInternal(ERROR, ...args); | ||
| } | ||
| /** | ||
| * Logs a debug message. | ||
| * @param args The arguments to be logged. | ||
| */ | ||
| debug(...args: any[]) { | ||
| if (process.env.ECO_PAGES_DEBUG === 'true') { | ||
| this.logInternal(DEBUG, ...args); | ||
| } | ||
| } | ||
| private logInternal(level?: LogLevel, ...args: any[]) { | ||
| const colorCode = level | ||
| ? { | ||
| [Level.INFO]: '\x1b[32m', // Green | ||
| [Level.ERROR]: '\x1b[31m', // Red | ||
| [Level.WARN]: '\x1b[33m', // Yellow | ||
| [Level.DEBUG]: '\x1b[36m', // Cyan | ||
| }[level.level] | ||
| : ''; | ||
| const logStart = `${colorCode ? colorCode : ''}${this.prefix}`; | ||
| const logEnd = '\x1b[0m'; | ||
| console.log(logStart, ...args, logEnd); | ||
| } | ||
| } |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
5276
-26.36%35
-67.89%3
50%