diff --git a/.prettierrc b/.prettierrc index 02f8c691c61112702a64fc989b6bf0de77bcff2e..5af29c81ac9dbf597646150b50ba0f05aa933834 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,6 +1,8 @@ { - "tabWidth": 3, - "trailingComma": "es5", - "semi": false, - "singleQuote": true -} \ No newline at end of file + "tabWidth": 4, + "trailingComma": "es5", + "semi": true, + "singleQuote": true, + "bracketSpacing": true, + "printWidth": 100 +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..b6882992d2a5213199919a5ce89998df50344d3b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [v3.0.0] - 2025-08-16 + +- New major release. +- Removed TypeScript support. +- Updated dependencies. +- Update `Logger` class to use ES modules. +- Use `console` for logging. +- Update documentation. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..253bfbd517ac9050e8cd3bd0a5fd90424c6d921e --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +ISC License + +Copyright (c) 2025 Rajat Sharma + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md index 452b620a73d97635c78c55a0c698414353d72de6..4640a39c565cc48e12ad2ed24b7462c524f58628 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,89 @@ -# Simple NodeJs Logger +# Simple Node.js Logger -### Installation -```bash -$ npm install @rxpm/logger +A simple, lightweight logging library for Node.js with colorized console output and file logging support. + +## Features + +- 📝 Multiple log levels (info, error, warn, debug) +- 🎨 Colorized console output +- 💾 Optional file logging +- 📂 Automatic log file rotation +- 📦 Zero-dependencies (except for `chalk` for colors) + +## Installation + +```sh +npm install @rxpm/logger ``` -### Usage +## Usage + +### Basic Usage -Create new logger instance ```javascript -const myLogger = new Logger("MyLogger", { enable: true }); +import logger from '@rxpm/logger'; + +// Log messages with different levels +logger.info('app', 'Application started'); +logger.warn('auth', 'User session about to expire'); +logger.error('db', 'Connection failed', { error: err }); +logger.debug('cache', 'Cache miss', { key: 'user:123' }); ``` -Use different loglevel methods +### With File Logging + ```javascript -myLogger.info("App", "First info log"); +import { Logger } from '@rxpm/logger'; + +// Initialize with log file +const logger = new Logger('./logs/app.log'); + +logger.info('app', 'This will be logged to both console and file'); +``` + +### Log Format + +Logs are formatted as: + +``` +TIMESTAMP LEVEL NAMESPACE MESSAGE +``` + +Example: + +```log +2023-08-16T16:05:23.123Z INFO app Server started on port 3000 ``` + +## API + +`new Logger([filepath])` + +Creates a new Logger instance. + +- `filepath` (String): Optional path to the log file. If not provided, logs will only be output to the console. + +### Methods + +`logger.info(namespace, ...args)` + +Logs an info message. + +- `namespace` (String): Log category/namespace +- `...args`: Values to log + +`logger.error(namespace, ...args)` + +Logs an error message. + +`logger.warn(namespace, ...args)` + +Logs a warning message. + +`logger.debug(namespace, ...args)` + +Logs a debug message. + +## License + +ISC © [Rajat Sharma](https://gitlab.com/rajatxs) diff --git a/main.js b/main.js new file mode 100644 index 0000000000000000000000000000000000000000..a13f9a1c9df40668a46c07d1b59399b75c08a996 --- /dev/null +++ b/main.js @@ -0,0 +1,103 @@ +import { resolve, isAbsolute } from 'path'; +import { createWriteStream } from 'fs'; +import { format } from 'util'; +import chalk from 'chalk'; + +export class Logger { + /** Log file path */ + outfile = ''; + + /** Log file stream */ + wstream = null; + + /** + * Contructs new instance of `Logger` + * @param filepath - Log file path + */ + constructor(filepath = '') { + if (filepath.length > 0) { + this.outfile = isAbsolute(filepath) ? filepath : resolve(filepath); + this.wstream = createWriteStream(this.outfile, { + encoding: 'utf8', + flags: 'a', + }); + } + } + + /** + * Logs message with given `level` and `namespace` + * @param {'info' | 'error' | 'warn' | 'debug'} level - Log level + * @param {string} namespace - Log namespace + * @param {any[]} args - Values + */ + log(level, namespace, ...args) { + const timestamp = new Date().toISOString(); + const levelText = level.toUpperCase().padEnd(5); + const namespaceText = namespace || 'app'; + const argsText = format(...args); + let levelFormat = ''; + + switch (level) { + case 'info': + levelFormat = chalk.green(levelText); + break; + case 'error': + levelFormat = chalk.red(levelText); + break; + case 'warn': + levelFormat = chalk.yellow(levelText); + break; + case 'debug': + levelFormat = chalk.blue(levelText); + break; + default: + levelFormat = chalk.white(levelText); + } + + console.log(format(`${timestamp} ${levelFormat} ${namespaceText} ${argsText}`)); + + if (this.wstream) { + const log = format(`${timestamp} ${levelText} ${namespaceText} ${argsText}`); + this.wstream.write(log + '\n'); + } + } + + /** + * Logs error message with given `namespace` + * @param {string} namespace - Log namespace + * @param {any[]} args - Values + */ + error(namespace, ...args) { + this.log('error', namespace, ...args); + } + + /** + * Logs warn message with given `namespace` + * @param {string} namespace - Log namespace + * @param {any[]} args - Values + */ + warn(namespace, ...args) { + this.log('warn', namespace, ...args); + } + + /** + * Logs info message with given `namespace` + * @param {string} namespace - Log namespace + * @param {any[]} args - Values + */ + info(namespace, ...args) { + this.log('info', namespace, ...args); + } + + /** + * Logs debug message with given `namespace` + * @param {string} namespace - Log namespace + * @param {any[]} args - Values + */ + debug(namespace, ...args) { + this.log('debug', namespace, ...args); + } +} + +// Default logger instance +export default new Logger(); diff --git a/package-lock.json b/package-lock.json index 1ab5c6413cc62d16f0ad1135cc1dc611a048197d..85c688d95f6eb16a7ed40e152203a68fc20c54d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,160 +1,35 @@ { - "name": "@rxpm/logger", - "version": "2.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "@rxpm/logger", - "version": "2.0.0", - "license": "MIT", - "dependencies": { - "chalk": "^4.1.2" - }, - "devDependencies": { - "@types/node": "^18.11.9", - "typescript": "^4.8.4" - } - }, - "node_modules/@types/node": { - "version": "18.11.9", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", - "integrity": "sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==", - "dev": true - }, - "node_modules/ansi-styles": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", - "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dependencies": { - "color-convert": "^2.0.1" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/chalk/ansi-styles?sponsor=1" - } - }, - "node_modules/chalk": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", - "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "dependencies": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/chalk/chalk?sponsor=1" - } - }, - "node_modules/color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dependencies": { - "color-name": "~1.1.4" - }, - "engines": { - "node": ">=7.0.0" - } - }, - "node_modules/color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" - }, - "node_modules/has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "engines": { - "node": ">=8" - } - }, - "node_modules/supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dependencies": { - "has-flag": "^4.0.0" - }, - "engines": { - "node": ">=8" - } - }, - "node_modules/typescript": { - "version": "4.8.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.8.4.tgz", - "integrity": "sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==", - "dev": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=4.2.0" - } - } - }, - "dependencies": { - "@types/node": { - "version": "18.11.9", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", - "integrity": "sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==", - "dev": true - }, - "ansi-styles": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", - "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "requires": { - "color-convert": "^2.0.1" - } - }, - "chalk": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", - "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "requires": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - } - }, - "color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "requires": { - "color-name": "~1.1.4" - } - }, - "color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" - }, - "has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" - }, - "supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "requires": { - "has-flag": "^4.0.0" - } - }, - "typescript": { - "version": "4.8.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.8.4.tgz", - "integrity": "sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==", - "dev": true + "name": "@rxpm/logger", + "version": "3.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "@rxpm/logger", + "version": "3.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^5.5.0" + } + }, + "node_modules/chalk": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.5.0.tgz", + "integrity": "sha512-1tm8DTaJhPBG3bIkVeZt1iZM9GfSX2lzOeDVZH9R9ffRHpmHvxZ/QhgQH/aDTkswQVt+YHdXAdS/In/30OjCbg==", + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + } + }, + "dependencies": { + "chalk": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.5.0.tgz", + "integrity": "sha512-1tm8DTaJhPBG3bIkVeZt1iZM9GfSX2lzOeDVZH9R9ffRHpmHvxZ/QhgQH/aDTkswQVt+YHdXAdS/In/30OjCbg==" + } } - } } diff --git a/package.json b/package.json index 1d5830d9f4dbe4ae18d899859b465771892f54d0..fec6e585afda9f3d165fbdd47756644c7d80b06d 100644 --- a/package.json +++ b/package.json @@ -1,38 +1,26 @@ { - "name": "@rxpm/logger", - "description": "Simple logger library for NodeJs based applications", - "version": "2.0.2", - "main": "lib/index.js", - "type": "commonjs", - "scripts": { - "build": "tsc" - }, - "keywords": [ - "logger", - "loggerjs", - "node-logger" - ], - "files": ["lib/**/*"], - "author": { - "name": "Rajat Sharma", - "email": "rxx175@gmail.com", - "url": "https://github.com/rajatxs" - }, - "repository": { - "type": "git", - "url": "https://github.com/rajatxs/logger-js.git" - }, - "bugs": { - "email": "rxx175@gmail.com", - "url": "https://github.com/rajatxs/logger-js/issues" - }, - "homepage": "https://github.com/rajatxs/logger-js#readme", - "license": "ISC", - "devDependencies": { - "@types/node": "^18.11.9", - "typescript": "^4.8.4" - }, - "dependencies": { - "chalk": "^4.1.2" - } + "name": "@rxpm/logger", + "description": "Simple Node.js logging library", + "version": "3.0.0", + "main": "main.js", + "type": "module", + "preferGlobal": false, + "keywords": [ + "logger", + "loggerjs", + "node-logger" + ], + "author": { + "name": "Rajat Sharma", + "email": "rajatxt@proton.me", + "url": "https://gitlab.com/rajatxs" + }, + "repository": { + "type": "git", + "url": "https://gitlab.com/rajatxs/logger-js.git" + }, + "license": "ISC", + "dependencies": { + "chalk": "^5.5.0" + } } diff --git a/src/index.ts b/src/index.ts deleted file mode 100644 index b7df6f26be5b8fbb9fb58a836de517d32b342265..0000000000000000000000000000000000000000 --- a/src/index.ts +++ /dev/null @@ -1,179 +0,0 @@ -import path from 'path' -import util from 'util' -import { - useDefault, - getLevelContext, - getNamespaceContext, - getMessageContext, - getTimestampContext, -} from './utils' -import { createWriteStream, WriteStream } from 'fs' -import type { - LoggerInitOptions, - LoggerCreateOptions, - LogMetadata, -} from './interfaces' - -export class Logger { - protected wstream: WriteStream - protected logFile: string - - /** - * Contructs new instance of `Logger` - * @param name - Logger name - * @param options - Logger init options - */ - constructor(public name: string, public options: LoggerInitOptions = {}) { - useDefault(options, 'enable', true) - useDefault(options, 'timestamp', true) - - if (options.write && options.write.enable) { - this.logFile = path.resolve(options.write.dir, options.write.filename) - this.wstream = createWriteStream(this.logFile, { - encoding: 'utf8', - flags: 'a', - }) - } - } - - /** Closes opened log file stream */ - public off(): Promise { - return new Promise((resolve, reject) => { - if (this.wstream) { - this.wstream.close((err) => { - if (err) { - return reject(err) - } - resolve() - }) - } else { - resolve() - } - }) - } - - /** - * Creates new log based on given `options` - * @param options - Log create options - * @returns Log metadata - */ - public createLog(options: LoggerCreateOptions): LogMetadata { - const levelCtx = getLevelContext(options.level) - const nsCtx = getNamespaceContext( - options.namespace || this.options.namespace || 'app' - ) - const msgCtx = getMessageContext(...(options.args || [])) - const tsCtx = getTimestampContext() - - let fmtList = [] - let fmtLog, meta: LogMetadata - - fmtList.push(levelCtx.format) - fmtList.push(nsCtx.format) - - if (this.options.timestamp) { - fmtList.push(tsCtx.format) - } - - fmtList.push(msgCtx.format) - - // create formatted log string - fmtLog = util.format(...fmtList) - - meta = { - label: options.level.toUpperCase(), - namespace: nsCtx.text, - msg: msgCtx.text, - timestamp: tsCtx.text, - args: options.args || [], - fmt: fmtLog, - } - - return meta - } - - /** - * Writes log string to stdout - * @param meta - Log metadata - */ - protected writeLogToStdOutput(meta: LogMetadata) { - process.stdout.write(meta.fmt + '\n') - } - - /** - * Writes log to output file - * @param meta - Log metadata - */ - protected writeLogToFile(meta: LogMetadata) { - const logStr = util.format( - '%s %s %s %s\n', - meta.label, - meta.namespace, - meta.timestamp, - meta.msg - ) - - if (this.wstream) { - this.wstream.write(logStr) - } - } - - /** - * Writes log string to std output and file by given format - * @param meta - Log metadata - */ - protected writeLog(meta: LogMetadata) { - if (this.options.enable) { - this.writeLogToStdOutput(meta) - } - - if (this.options.write?.enable) { - this.writeLogToFile(meta) - } - } - - /** - * Writes debug log with given `namespace` - * @param namespace - Log namespace - * @param args - Values - */ - public debug(namespace: string, ...args: any[]) { - this.writeLog(this.createLog({ level: 'DEBUG', args, namespace })) - } - - /** - * Writes error log with given `namespace` - * @param namespace - Log namespace - * @param args - Values - */ - public error(namespace: string, ...args: any[]) { - this.writeLog(this.createLog({ level: 'ERROR', args, namespace })) - } - - /** - * Writes info log with given `namespace` - * @param namespace - Log namespace - * @param args - Values - */ - public info(namespace: string, ...args: any[]) { - this.writeLog(this.createLog({ level: 'INFO', args, namespace })) - } - - /** - * Writes fatal log with given `namespace` - * @param namespace - Log namespace - * @param args - Values - */ - public fatal(namespace: string, ...args: any[]) { - this.writeLog(this.createLog({ level: 'FATAL', args, namespace })) - } - - /** - * Writes warn log with given `namespace` - * @param namespace - Log namespace - * @param args - Values - */ - public warn(namespace: string, ...args: any[]) { - this.writeLog(this.createLog({ level: 'WARN', args, namespace })) - } -} diff --git a/src/interfaces.ts b/src/interfaces.ts deleted file mode 100644 index e72be8bbbe5f8f3e54909999f052de724892f91a..0000000000000000000000000000000000000000 --- a/src/interfaces.ts +++ /dev/null @@ -1,34 +0,0 @@ -export type LogLevel = 'DEBUG' | 'ERROR' | 'INFO' | 'WARN' | 'FATAL' - -export interface LogContext { - text: string - format: string -} - -export interface LogMetadata { - label: string - namespace: string - msg: string - timestamp: string - args: any[] - fmt: string -} - -export interface LoggerWriteOptions { - enable?: boolean - dir: string - filename: string -} - -export interface LoggerInitOptions { - namespace?: string - enable?: boolean - timestamp?: boolean - write?: LoggerWriteOptions -} - -export interface LoggerCreateOptions { - level: LogLevel - namespace?: string - args?: any[] -} diff --git a/src/utils.ts b/src/utils.ts deleted file mode 100644 index 02ecd00387ec24ac757b2ba7864518b2c5e2c239..0000000000000000000000000000000000000000 --- a/src/utils.ts +++ /dev/null @@ -1,71 +0,0 @@ -import * as util from 'util' -import chalk from 'chalk' -import { logLevelColors, logLevelColorDefault } from './values' -import type { LogLevel, LogContext } from './interfaces' - -/** - * Assign default value to object if given `prop` is missing - * @param obj - Object - * @param prop - Property - * @param val - Default value - */ -export function useDefault(obj: object, prop: string, val: T) { - if (!Reflect.has(obj, prop)) { - Reflect.set(obj, prop, val) - } -} - -/** - * Returns context obejct of log level - * @param level - Log level - */ -export function getLevelContext(level: LogLevel): LogContext { - const color = logLevelColors[level] || logLevelColorDefault - const text = level - let format: string - - if (level === 'FATAL') { - format = chalk.bgHex(color).whiteBright(text) - } else { - format = chalk.hex(color)(text) - } - - return { - text, - format, - } -} - -/** - * Returns context object of given log namespace - * @param namespace - Log namespace - */ -export function getNamespaceContext(namespace: string): LogContext { - const text = namespace - const format = chalk.whiteBright(namespace) - - return { - text, - format, - } -} - -/** - * Returns context object of log message by given arguments - * @param args - Log arguments - */ -export function getMessageContext(...args: any[]): LogContext { - const text = util.format(...args) - const format = chalk.white(text) - - return { text, format } -} - -/** Returns context object of log timestamp */ -export function getTimestampContext(): LogContext { - const time = new Date() - const text = time.toISOString() - const format = chalk.dim(time.toLocaleString()) - - return { text, format } -} diff --git a/src/values.ts b/src/values.ts deleted file mode 100644 index f6b153503d393df9b7c0402d14e7b9a9adb08573..0000000000000000000000000000000000000000 --- a/src/values.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { LogLevel } from './interfaces' - -export const logLevelColorDefault = "#FFFFFF" -export const logLevelColors: Record = { - DEBUG: "#FFB86C", - INFO: "#50FA7B", - WARN: "#F1FA8C", - ERROR: "#FF5555", - FATAL: "#DC0000", -} diff --git a/test.js b/test.js new file mode 100644 index 0000000000000000000000000000000000000000..5e3f7f1f8f31c1a3a4952f446a75d2c61cf7d664 --- /dev/null +++ b/test.js @@ -0,0 +1,6 @@ +import logger from './main.js'; + +logger.info('app', 'Application started'); +logger.error('app', 'Application started'); +logger.warn('app', 'Application started'); +logger.debug('app', 'Application started'); diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index def4816d9e317fd0b4e2c1b4ea8cc7dab1c66912..0000000000000000000000000000000000000000 --- a/tsconfig.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "compilerOptions": { - "allowJs": false, - "checkJs": false, - "outDir": "lib", - "target": "ES5", - "moduleResolution": "Node", - "module": "CommonJS", - "sourceMap": true, - "declaration": true, - "declarationMap": true, - "strict": true, - "useDefineForClassFields": false, - "strictPropertyInitialization": false, - "esModuleInterop": true - }, - "include": [ - "src" - ] -} \ No newline at end of file