@4lch4/logger
Advanced tools
Comparing version 1.7.4 to 1.8.0
@@ -1,12 +0,12 @@ | ||
import { ILogger, ILoggerOpts } from './interfaces'; | ||
export declare class Logger implements ILogger { | ||
import { ILoggerOpts } from './interfaces/index.js'; | ||
export declare class Logger { | ||
private formatter; | ||
constructor(loggerOpts?: ILoggerOpts); | ||
info(msg: string | Object): void; | ||
warn(msg: string | Object): void; | ||
debug(msg: string | Object): void; | ||
error(msg: string | Error | unknown, ...extra: any[]): void; | ||
success(msg: string): void; | ||
log(msg: string, level: any): void; | ||
info(msg: string | Object, ...optionalParams: any[]): void; | ||
warn(msg: string | Object, ...optionalParams: any[]): void; | ||
debug(msg: string | Object, ...optionalParams: any[]): void; | ||
error(msg: string | Error | unknown, ...optionalParams: any[]): void; | ||
success(msg: string, ...optionalParams: any[]): void; | ||
log(msg: string, level: any, ...optionalParams: any[]): void; | ||
} | ||
export declare const logger: Logger; |
@@ -1,48 +0,47 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.logger = exports.Logger = void 0; | ||
const interfaces_1 = require("./interfaces"); | ||
const lib_1 = require("./lib"); | ||
class Logger { | ||
import { Level } from './interfaces/index.js'; | ||
import { DefaultColors, DefaultLogFormat, Formatter } from './lib/index.js'; | ||
export class Logger { | ||
formatter; | ||
constructor(loggerOpts) { | ||
let colorOpts = lib_1.DefaultColors; | ||
let formatOpt = lib_1.DefaultLogFormat; | ||
if (loggerOpts === null || loggerOpts === void 0 ? void 0 : loggerOpts.format) | ||
let colorOpts = DefaultColors; | ||
let formatOpt = DefaultLogFormat; | ||
if (loggerOpts?.format) | ||
formatOpt = loggerOpts.format; | ||
if (loggerOpts === null || loggerOpts === void 0 ? void 0 : loggerOpts.colorOpts) | ||
if (loggerOpts?.colorOpts) | ||
colorOpts = loggerOpts.colorOpts; | ||
this.formatter = new lib_1.Formatter(formatOpt, colorOpts); | ||
this.formatter = new Formatter(formatOpt, colorOpts); | ||
} | ||
info(msg) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.info)); | ||
info(msg, ...optionalParams) { | ||
console.log(this.formatter.formatMsg(msg, Level.info), optionalParams); | ||
} | ||
warn(msg) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.warn)); | ||
warn(msg, ...optionalParams) { | ||
console.log(this.formatter.formatMsg(msg, Level.warn), optionalParams); | ||
} | ||
debug(msg) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.debug)); | ||
debug(msg, ...optionalParams) { | ||
if (process.env.DEBUG) { | ||
console.log(this.formatter.formatMsg(msg, Level.debug), optionalParams); | ||
} | ||
} | ||
error(msg, ...extra) { | ||
error(msg, ...optionalParams) { | ||
if (msg instanceof Error) { | ||
console.log(this.formatter.formatMsg(msg.message, interfaces_1.Level.error)); | ||
console.error(this.formatter.formatMsg(msg.message, Level.error)); | ||
} | ||
else if (msg instanceof String) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.error)); | ||
console.error(this.formatter.formatMsg(msg, Level.error)); | ||
} | ||
else { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.error)); | ||
console.error(this.formatter.formatMsg(msg, Level.error)); | ||
} | ||
if (extra.length > 0) { | ||
console.log(this.formatter.formatMsg(extra.join('\n'), interfaces_1.Level.error)); | ||
if (optionalParams.length > 0) { | ||
console.error(this.formatter.formatMsg(optionalParams.join('\n'), Level.error)); | ||
} | ||
} | ||
success(msg) { | ||
console.log(this.formatter.formatMsg(msg, interfaces_1.Level.success)); | ||
success(msg, ...optionalParams) { | ||
console.log(this.formatter.formatMsg(msg, Level.success), optionalParams); | ||
} | ||
log(msg, level) { | ||
console.log(this.formatter.formatMsg(msg, level)); | ||
log(msg, level, ...optionalParams) { | ||
console.log(this.formatter.formatMsg(msg, level), optionalParams); | ||
} | ||
} | ||
exports.Logger = Logger; | ||
exports.logger = new Logger(); | ||
export const logger = new Logger(); | ||
//# sourceMappingURL=index.js.map |
import { Color } from 'chalk'; | ||
import { Level } from '.'; | ||
import { Level } from './index.js'; | ||
export interface IColorOpts { | ||
[Level.debug]: typeof Color; | ||
[Level.error]: typeof Color; | ||
[Level.info]: typeof Color; | ||
[Level.success]: typeof Color; | ||
[Level.warn]: typeof Color; | ||
[Level.debug]: Color; | ||
[Level.error]: Color; | ||
[Level.info]: Color; | ||
[Level.success]: Color; | ||
[Level.warn]: Color; | ||
} | ||
export interface IColorOptsInput { | ||
[Level.debug]?: string | Color; | ||
[Level.error]?: string | Color; | ||
[Level.info]?: string | Color; | ||
[Level.success]?: string | Color; | ||
[Level.warn]?: string | Color; | ||
} |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _1 = require("."); | ||
import { Level } from './index.js'; | ||
//# sourceMappingURL=IColorOpts.js.map |
@@ -1,2 +0,2 @@ | ||
import { Level } from '.'; | ||
import { Level } from './index.js'; | ||
export interface ILogger { | ||
@@ -3,0 +3,0 @@ info: (msg: string) => void; |
@@ -1,3 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; | ||
//# sourceMappingURL=ILogger.js.map |
@@ -1,2 +0,2 @@ | ||
import { IColorOpts, LogFormat } from '.'; | ||
import { IColorOpts, LogFormat } from './index.js'; | ||
export interface ILoggerOpts { | ||
@@ -3,0 +3,0 @@ format?: LogFormat; |
@@ -1,3 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; | ||
//# sourceMappingURL=ILoggerOpts.js.map |
@@ -1,5 +0,5 @@ | ||
export * from './IColorOpts'; | ||
export * from './ILogger'; | ||
export * from './ILoggerOpts'; | ||
export * from './Level'; | ||
export * from './LogFormat'; | ||
export * from './IColorOpts.js'; | ||
export * from './ILogger.js'; | ||
export * from './ILoggerOpts.js'; | ||
export * from './Level.js'; | ||
export * from './LogFormat.js'; |
@@ -1,18 +0,6 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./IColorOpts"), exports); | ||
__exportStar(require("./ILogger"), exports); | ||
__exportStar(require("./ILoggerOpts"), exports); | ||
__exportStar(require("./Level"), exports); | ||
__exportStar(require("./LogFormat"), exports); | ||
export * from './IColorOpts.js'; | ||
export * from './ILogger.js'; | ||
export * from './ILoggerOpts.js'; | ||
export * from './Level.js'; | ||
export * from './LogFormat.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Level = void 0; | ||
var Level; | ||
export var Level; | ||
(function (Level) { | ||
@@ -11,3 +8,3 @@ Level["info"] = "info"; | ||
Level["success"] = "success"; | ||
})(Level = exports.Level || (exports.Level = {})); | ||
})(Level || (Level = {})); | ||
//# sourceMappingURL=Level.js.map |
@@ -1,3 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; | ||
//# sourceMappingURL=LogFormat.js.map |
@@ -1,3 +0,2 @@ | ||
import { IColorOpts } from '../interfaces/IColorOpts'; | ||
import { LogFormat } from '../interfaces/LogFormat'; | ||
import { IColorOpts, LogFormat } from '../interfaces/index.js'; | ||
export declare const DefaultColors: IColorOpts; | ||
@@ -4,0 +3,0 @@ export declare const DefaultLogFormat: LogFormat; |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DefaultDayJSFormats = exports.DefaultLogFormat = exports.DefaultColors = void 0; | ||
exports.DefaultColors = { | ||
export const DefaultColors = { | ||
debug: 'cyan', | ||
@@ -11,4 +8,4 @@ error: 'red', | ||
}; | ||
exports.DefaultLogFormat = 'plaintext'; | ||
exports.DefaultDayJSFormats = { | ||
export const DefaultLogFormat = 'plaintext'; | ||
export const DefaultDayJSFormats = { | ||
prettyFormat: 'YYYY.MM.DD-HH:mm:ss', | ||
@@ -15,0 +12,0 @@ utcMillisecondsFormat: 'x' |
@@ -1,3 +0,2 @@ | ||
import { IColorOpts, Level } from '../interfaces'; | ||
import { LogFormat } from '../interfaces/LogFormat'; | ||
import { IColorOpts, Level, LogFormat } from '../interfaces/index.js'; | ||
export declare class Formatter { | ||
@@ -8,2 +7,3 @@ format: LogFormat; | ||
colorMsg(msg: string, level: Level): string; | ||
private stringifyMsg; | ||
formatMsg(msg: string | Object, level: Level): string; | ||
@@ -10,0 +10,0 @@ getUTCTime(): string; |
@@ -1,55 +0,49 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Formatter = void 0; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const dayjs_1 = __importDefault(require("dayjs")); | ||
const advancedFormat_1 = __importDefault(require("dayjs/plugin/advancedFormat")); | ||
const os_1 = require("os"); | ||
const _1 = require("./"); | ||
class Formatter { | ||
import chalk from 'chalk'; | ||
import dayjs from 'dayjs'; | ||
import AdvancedFormats from 'dayjs/plugin/advancedFormat'; | ||
import os from 'os'; | ||
import { DefaultDayJSFormats } from './index.js'; | ||
export class Formatter { | ||
format; | ||
colors; | ||
constructor(format, colors) { | ||
dayjs_1.default.extend(advancedFormat_1.default); | ||
this.colors = colors; | ||
this.format = format; | ||
dayjs.extend(AdvancedFormats); | ||
} | ||
colorMsg(msg, level) { | ||
return chalk_1.default.keyword(this.colors[level])(msg); | ||
return chalk[this.colors[level]](msg); | ||
} | ||
stringifyMsg(msg) { | ||
if (typeof msg === 'string') | ||
return msg; | ||
return JSON.stringify(msg); | ||
} | ||
formatMsg(msg, level) { | ||
switch (typeof msg) { | ||
case 'string': { | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
time: this.getUTCTime(), | ||
host: (0, os_1.hostname)(), | ||
pid: process.pid, | ||
level, | ||
msg | ||
}; | ||
return this.colorMsg(JSON.stringify(logOutput), level); | ||
} | ||
else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
msg | ||
]; | ||
return this.colorMsg(output.join(' - '), level); | ||
} | ||
} | ||
case 'object': | ||
return this.colorMsg(JSON.stringify(msg), level); | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
time: this.getUTCTime(), | ||
host: os.hostname(), | ||
pid: process.pid, | ||
level, | ||
msg: this.stringifyMsg(msg) | ||
}; | ||
return this.colorMsg(JSON.stringify(logOutput), level); | ||
} | ||
else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
this.stringifyMsg(msg) | ||
]; | ||
return this.colorMsg(output.join(' - '), level); | ||
} | ||
} | ||
getUTCTime() { | ||
return (0, dayjs_1.default)().format(_1.DefaultDayJSFormats.utcMillisecondsFormat); | ||
return dayjs().format(DefaultDayJSFormats.utcMillisecondsFormat); | ||
} | ||
getPrettyTime() { | ||
return (0, dayjs_1.default)().format(_1.DefaultDayJSFormats.prettyFormat); | ||
return dayjs().format(DefaultDayJSFormats.prettyFormat); | ||
} | ||
} | ||
exports.Formatter = Formatter; | ||
//# sourceMappingURL=Formatter.js.map |
@@ -1,2 +0,2 @@ | ||
export * from './defaults'; | ||
export * from './Formatter'; | ||
export * from './defaults.js'; | ||
export * from './Formatter.js'; |
@@ -1,15 +0,3 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./defaults"), exports); | ||
__exportStar(require("./Formatter"), exports); | ||
export * from './defaults.js'; | ||
export * from './Formatter.js'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@4lch4/logger", | ||
"displayName": "Logger", | ||
"version": "1.7.4", | ||
"version": "1.8.0", | ||
"description": "A small utility for logging to console within NodeJS/TypeScript applications.", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"pretty": "prettier --write . && git add . && git commit -m 'ci(pretty): project prettied'", | ||
"test": "mocha --reporter spec --require ts-node/register tests/*.test.ts" | ||
"type": "module", | ||
"engines": { | ||
"node": ">=14.16" | ||
}, | ||
@@ -21,4 +20,4 @@ "keywords": [ | ||
"name": "4lch4", | ||
"url": "https://4lch4.dev", | ||
"email": "inbox@4lch4.dev" | ||
"url": "https://4lch4.com", | ||
"email": "hey@4lch4.email" | ||
}, | ||
@@ -40,18 +39,24 @@ "bugs": { | ||
"dependencies": { | ||
"chalk": "^4.1.2", | ||
"dayjs": "^1.10.7", | ||
"fs-extra": "^10.0.0" | ||
"chalk": "^5.0.1", | ||
"dayjs": "^1.11.3", | ||
"fs-extra": "^10.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.22", | ||
"@types/chai": "^4.3.1", | ||
"@types/fs-extra": "^9.0.13", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node": "^16.10.3", | ||
"chai": "^4.3.4", | ||
"mocha": "^9.1.2", | ||
"np": "^7.5.0", | ||
"prettier": "^2.4.1", | ||
"ts-node": "^10.3.0", | ||
"typescript": "^4.4.4" | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^16", | ||
"chai": "^4.3.6", | ||
"mocha": "^10.0.0", | ||
"np": "^7.6.1", | ||
"prettier": "^2.7.1", | ||
"ts-node": "^10.8.1", | ||
"typescript": "^4.7.3" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"pretty": "prettier --write .", | ||
"pretty:commit": "prettier --write . && git add . && git commit -m 'ci(pretty): project prettied'", | ||
"test": "mocha --reporter spec --require ts-node/register tests/*.test.ts" | ||
} | ||
} | ||
} |
103
src/index.ts
@@ -1,5 +0,16 @@ | ||
import { ILogger, ILoggerOpts, Level } from './interfaces' | ||
import { DefaultColors, DefaultLogFormat, Formatter } from './lib' | ||
import { ILoggerOpts, Level } from './interfaces/index.js' | ||
import { DefaultColors, DefaultLogFormat, Formatter } from './lib/index.js' | ||
export class Logger implements ILogger { | ||
/** | ||
* This class is the main/only class of the library. It contains the various | ||
* methods to log messages, and the options provided (if any) are passed to the | ||
* formatter class for modifying the output. | ||
* | ||
* Each method uses the `console` module behind the scenes. This means that you | ||
* can provide the ...optionalParams to all methods and they will be passed to | ||
* the console module. | ||
* | ||
* @class Logger | ||
*/ | ||
export class Logger { | ||
private formatter: Formatter | ||
@@ -17,37 +28,91 @@ | ||
info(msg: string | Object) { | ||
console.log(this.formatter.formatMsg(msg, Level.info)) | ||
/** | ||
* Writes the given message to `stdout` with a new line. | ||
* | ||
* @param msg The content to write to `stdout`. | ||
* @param optionalParams Any extra parameters to pass to the console module. | ||
*/ | ||
info(msg: string | Object, ...optionalParams: any[]): void { | ||
console.log(this.formatter.formatMsg(msg, Level.info), optionalParams) | ||
} | ||
warn(msg: string | Object) { | ||
console.log(this.formatter.formatMsg(msg, Level.warn)) | ||
/** | ||
* Writes the given message to `stdout` with a new line and colors it yellow | ||
* to indicate it is a warning. | ||
* | ||
* @param msg The content to write to `stdout`. | ||
* @param optionalParams Any extra parameters to pass to the console module. | ||
*/ | ||
warn(msg: string | Object, ...optionalParams: any[]): void { | ||
console.log(this.formatter.formatMsg(msg, Level.warn), optionalParams) | ||
} | ||
debug(msg: string | Object) { | ||
console.log(this.formatter.formatMsg(msg, Level.debug)) | ||
/** | ||
* Writes the given message to the console at the debug level as long as the | ||
* `DEBUG` environment variable is set to `true`. | ||
* | ||
* @param msg The string/object to write to the debug channel of the console | ||
* @param optionalParams Any extra parameters to pass to the console module. | ||
*/ | ||
debug(msg: string | Object, ...optionalParams: any[]): void { | ||
if (process.env.DEBUG) { | ||
console.log(this.formatter.formatMsg(msg, Level.debug), optionalParams) | ||
} | ||
} | ||
error(msg: string | Error | unknown, ...extra: any[]) { | ||
/** | ||
* Writes the given content to `stderr`. If the content is an object, it will | ||
* be stringified and written to `stderr`. If the content is a string, it will | ||
* be written to `stderr` as is. If the content is `unknown`, it will attempt | ||
* to be written to `stderr` as an Object. | ||
* | ||
* Lastly, if there are any optionalParams provided they are joined with a new | ||
* line and written to `stderr`. | ||
* | ||
* @param msg The content to write to `stderr`. | ||
* @param optionalParams Any extra parameters to pass to the console module. | ||
*/ | ||
error(msg: string | Error | unknown, ...optionalParams: any[]): void { | ||
if (msg instanceof Error) { | ||
console.log(this.formatter.formatMsg(msg.message, Level.error)) | ||
console.error(this.formatter.formatMsg(msg.message, Level.error)) | ||
} else if (msg instanceof String) { | ||
console.log(this.formatter.formatMsg(msg, Level.error)) | ||
console.error(this.formatter.formatMsg(msg, Level.error)) | ||
} else { | ||
console.log(this.formatter.formatMsg(msg as Object, Level.error)) | ||
console.error(this.formatter.formatMsg(msg as Object, Level.error)) | ||
} | ||
if (extra.length > 0) { | ||
console.log(this.formatter.formatMsg(extra.join('\n'), Level.error)) | ||
if (optionalParams.length > 0) { | ||
console.error( | ||
this.formatter.formatMsg(optionalParams.join('\n'), Level.error) | ||
) | ||
} | ||
} | ||
success(msg: string) { | ||
console.log(this.formatter.formatMsg(msg, Level.success)) | ||
/** | ||
* Writes the given message to `stdout` with a new line and colors it green | ||
* to indicate a successful operation. | ||
* | ||
* @param msg The content to write to `stdout`. | ||
* @param optionalParams Any extra parameters to pass to the console module. | ||
*/ | ||
success(msg: string, ...optionalParams: any[]): void { | ||
console.log(this.formatter.formatMsg(msg, Level.success), optionalParams) | ||
} | ||
log(msg: string, level: any) { | ||
console.log(this.formatter.formatMsg(msg, level)) | ||
/** | ||
* A convenience method for writing to any level of the console. | ||
* | ||
* @param msg The content to write. | ||
* @param level The level of the message, such as DEBUG, INFO, WARN, ERROR, or SUCCESS. | ||
* @param optionalParams Any extra parameters to pass to the console module. | ||
*/ | ||
log(msg: string, level: any, ...optionalParams: any[]): void { | ||
console.log(this.formatter.formatMsg(msg, level), optionalParams) | ||
} | ||
} | ||
/** | ||
* A basic instance of the {@link Logger} class using some sane defaults for the | ||
* config options. | ||
*/ | ||
export const logger = new Logger() |
import { Color } from 'chalk' | ||
import { Level } from '.' | ||
import { Level } from './index.js' | ||
/** The options for the color of each log level. */ | ||
export interface IColorOpts { | ||
[Level.debug]: typeof Color | ||
[Level.error]: typeof Color | ||
[Level.info]: typeof Color | ||
[Level.success]: typeof Color | ||
[Level.warn]: typeof Color | ||
[Level.debug]: Color | ||
[Level.error]: Color | ||
[Level.info]: Color | ||
[Level.success]: Color | ||
[Level.warn]: Color | ||
} | ||
/** | ||
* The options for the color of each log level. All parameters are optional and | ||
* any level that isn't provided will use the default color. For details on the | ||
* default colors, see the exported `DefaultColors` object. | ||
*/ | ||
export interface IColorOptsInput { | ||
[Level.debug]?: string | Color | ||
[Level.error]?: string | Color | ||
[Level.info]?: string | Color | ||
[Level.success]?: string | Color | ||
[Level.warn]?: string | Color | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Level } from '.' | ||
import { Level } from './index.js' | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { IColorOpts, LogFormat } from '.' | ||
import { IColorOpts, LogFormat } from './index.js' | ||
@@ -3,0 +3,0 @@ export interface ILoggerOpts { |
@@ -1,5 +0,5 @@ | ||
export * from './IColorOpts' | ||
export * from './ILogger' | ||
export * from './ILoggerOpts' | ||
export * from './Level' | ||
export * from './LogFormat' | ||
export * from './IColorOpts.js' | ||
export * from './ILogger.js' | ||
export * from './ILoggerOpts.js' | ||
export * from './Level.js' | ||
export * from './LogFormat.js' |
@@ -1,4 +0,4 @@ | ||
import { IColorOpts } from '../interfaces/IColorOpts' | ||
import { LogFormat } from '../interfaces/LogFormat' | ||
import { IColorOpts, LogFormat } from '../interfaces/index.js' | ||
/** An object containing the default colors to use for each logging level. */ | ||
export const DefaultColors: IColorOpts = { | ||
@@ -12,7 +12,12 @@ debug: 'cyan', | ||
/** The default format to use for logging. */ | ||
export const DefaultLogFormat: LogFormat = 'plaintext' | ||
/** The default formatting options to use w/ DayJS. */ | ||
export const DefaultDayJSFormats = { | ||
/** The standard pretty format: `YYYY.MM.DD-HH:mm:ss` */ | ||
prettyFormat: 'YYYY.MM.DD-HH:mm:ss', | ||
/** For formatting the time as UTC Milliseconds. */ | ||
utcMillisecondsFormat: 'x' | ||
} |
import chalk from 'chalk' | ||
import dayjs from 'dayjs' | ||
import AdvancedFormats from 'dayjs/plugin/advancedFormat' | ||
import { hostname } from 'os' | ||
import { IColorOpts, Level } from '../interfaces' | ||
import { LogFormat } from '../interfaces/LogFormat' | ||
import { DefaultDayJSFormats } from './' | ||
import os from 'os' | ||
import { IColorOpts, Level, LogFormat } from '../interfaces/index.js' | ||
import { DefaultDayJSFormats } from './index.js' | ||
/** | ||
* A utility class used for formatting messages. | ||
* | ||
* @class Formatter | ||
*/ | ||
export class Formatter { | ||
@@ -13,41 +17,66 @@ format: LogFormat | ||
/** | ||
* The constructor for the Formatter class. Both parameters are required for | ||
* the class to function. The `format` parameter is the format to use for | ||
* logging. The `colorOpts` parameter is an object containing the colors to | ||
* use for each level. | ||
* | ||
* @param format The format to use for logging messages. | ||
* @param colors An object that defines the colors to use for each level. | ||
*/ | ||
constructor(format: LogFormat, colors: IColorOpts) { | ||
dayjs.extend(AdvancedFormats) | ||
this.colors = colors | ||
this.format = format | ||
dayjs.extend(AdvancedFormats) | ||
} | ||
/** | ||
* Colors a message based on the level of the message. | ||
* | ||
* @param msg The message to color. | ||
* @param level The level of the message. | ||
* @returns The given message colored for the given level. | ||
*/ | ||
colorMsg(msg: string, level: Level) { | ||
return chalk.keyword(this.colors[level])(msg) | ||
return chalk[this.colors[level]](msg) | ||
} | ||
private stringifyMsg(msg: string | Object): string { | ||
if (typeof msg === 'string') return msg | ||
return JSON.stringify(msg) | ||
} | ||
/** | ||
* Formats the given message for output based on the `format` parameter that | ||
* was provided when instantiating the class and the `level` parameter. | ||
* | ||
* @param msg The message to format. | ||
* @param level The level of the message. | ||
* @returns A formatted message to be logged. | ||
*/ | ||
formatMsg(msg: string | Object, level: Level) { | ||
switch (typeof msg) { | ||
case 'string': { | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
/** The timestamp for when the message was sent. */ | ||
time: this.getUTCTime(), | ||
host: hostname(), | ||
pid: process.pid, | ||
level, | ||
msg | ||
} | ||
if (this.format === 'json') { | ||
const logOutput = { | ||
/** The timestamp for when the message was sent. */ | ||
time: this.getUTCTime(), | ||
host: os.hostname(), | ||
pid: process.pid, | ||
level, | ||
msg: this.stringifyMsg(msg) | ||
} | ||
return this.colorMsg(JSON.stringify(logOutput), level) | ||
} else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
msg | ||
] | ||
return this.colorMsg(output.join(' - '), level) | ||
} | ||
} | ||
case 'object': | ||
return this.colorMsg(JSON.stringify(msg), level) | ||
return this.colorMsg(JSON.stringify(logOutput), level) | ||
} else { | ||
const output = [ | ||
`[${this.getPrettyTime()}]`, | ||
`[${level.toUpperCase()}]`, | ||
this.stringifyMsg(msg) | ||
] | ||
return this.colorMsg(output.join(' - '), level) | ||
} | ||
} | ||
/** Returns the time as UTC Milliseconds. */ | ||
getUTCTime() { | ||
@@ -57,2 +86,3 @@ return dayjs().format(DefaultDayJSFormats.utcMillisecondsFormat) | ||
/** Returns the current time using a pretty format: `YYYY.MM.DD-HH:mm:ss` */ | ||
getPrettyTime() { | ||
@@ -59,0 +89,0 @@ return dayjs().format(DefaultDayJSFormats.prettyFormat) |
@@ -1,2 +0,2 @@ | ||
export * from './defaults' | ||
export * from './Formatter' | ||
export * from './defaults.js' | ||
export * from './Formatter.js' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
129848
52
623
0
Yes
1
+ Addedchalk@5.4.1(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedchalk@4.1.2(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedsupports-color@7.2.0(transitive)
Updatedchalk@^5.0.1
Updateddayjs@^1.11.3
Updatedfs-extra@^10.1.0