loglevelnext
Advanced tools
Comparing version 5.0.6 to 6.0.0
@@ -1,5 +0,91 @@ | ||
import { LogLevel, LogLevelOptions } from './LogLevel'; | ||
export * from './LogLevel'; | ||
export * from './MethodFactory'; | ||
export * from './PrefixFactory'; | ||
interface PrefixTemplateOptions { | ||
level: string; | ||
logger: LogLevel; | ||
} | ||
type PrefixTemplateFn = (options: PrefixTemplateOptions) => string; | ||
interface PrefixFactoryOptions { | ||
[key: string]: PrefixTemplateFn | string | undefined; | ||
level?: PrefixTemplateFn; | ||
name?: PrefixTemplateFn; | ||
template?: string; | ||
time?: PrefixTemplateFn; | ||
} | ||
declare class PrefixFactory extends MethodFactory { | ||
private options; | ||
constructor(logger?: LogLevel, options?: PrefixFactoryOptions); | ||
interpolate(level: string): string; | ||
make(methodName: string): (...args: any[]) => void; | ||
} | ||
type SomeLevel = number | string; | ||
interface LogLevelIndex { | ||
[key: string]: any; | ||
} | ||
interface LogLevelOptions { | ||
factory?: Factory; | ||
id?: string; | ||
level?: number | string; | ||
name?: string; | ||
prefix?: PrefixFactoryOptions; | ||
} | ||
declare class LogLevel<TLevels extends FactoryLevels = typeof defaultLevels> implements LogLevelIndex { | ||
[key: string]: any; | ||
name: string; | ||
type: string; | ||
private currentLevel; | ||
private methodFactory; | ||
private options; | ||
constructor(options: LogLevelOptions); | ||
get level(): TLevels[keyof TLevels]; | ||
get levels(): TLevels; | ||
get factory(): Factory<TLevels>; | ||
set factory(factory: Factory<TLevels>); | ||
set level(logLevel: SomeLevel); | ||
disable(): void; | ||
enable(): void; | ||
} | ||
interface BindTarget { | ||
[key: string]: Function; | ||
} | ||
type FactoryLevels = Record<Uppercase<string>, number> & { | ||
DEBUG: number; | ||
ERROR: number; | ||
INFO: number; | ||
SILENT: number; | ||
TRACE: number; | ||
WARN: number; | ||
}; | ||
declare const defaultLevels: FactoryLevels; | ||
declare const levels: unique symbol; | ||
declare const instance: unique symbol; | ||
interface Factory<TLevels extends FactoryLevels = typeof defaultLevels> { | ||
[key: string]: any; | ||
bindMethod: (obj: BindTarget, methodName: string) => any; | ||
distillLevel: (level: number | string) => any; | ||
[instance]: LogLevel | undefined; | ||
levelValid: (level: number) => boolean; | ||
[levels]: TLevels; | ||
levels: TLevels; | ||
logger: LogLevel; | ||
make: (methodName: string) => Function; | ||
methods?: string[]; | ||
replaceMethods: (logLevel: number | string) => void; | ||
} | ||
type MethodFactoryLevels = Lowercase<keyof FactoryLevels>; | ||
declare class MethodFactory<TLevels extends FactoryLevels = typeof defaultLevels> implements Factory<TLevels> { | ||
[instance]: LogLevel | undefined; | ||
[levels]: TLevels; | ||
constructor(logger?: LogLevel); | ||
get levels(): TLevels; | ||
get logger(): LogLevel; | ||
get methods(): string[]; | ||
set logger(logger: LogLevel); | ||
bindMethod(obj: BindTarget, methodName: string): any; | ||
distillLevel(level: number | string): number | null; | ||
levelValid(level: number): boolean; | ||
make(methodName: string): Function; | ||
replaceMethods(logLevel: number | string): void; | ||
} | ||
declare class DefaultLogger extends LogLevel { | ||
@@ -9,6 +95,7 @@ private cache; | ||
get factories(): any; | ||
get loggers(): Record<string, LogLevel>; | ||
create(opts: LogLevelOptions | string): LogLevel; | ||
get loggers(): Record<string, LogLevel<FactoryLevels>>; | ||
create(opts: LogLevelOptions | string): LogLevel<FactoryLevels>; | ||
} | ||
declare const _default: DefaultLogger; | ||
export default _default; | ||
export { Factory, FactoryLevels, LogLevel, LogLevelIndex, LogLevelOptions, MethodFactory, MethodFactoryLevels, PrefixFactory, PrefixFactoryOptions, PrefixTemplateFn, PrefixTemplateOptions, _default as default, defaultLevels }; |
"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); | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const LogLevel_1 = require("./LogLevel"); | ||
const MethodFactory_1 = require("./MethodFactory"); | ||
const PrefixFactory_1 = require("./PrefixFactory"); | ||
__exportStar(require("./LogLevel"), exports); | ||
__exportStar(require("./MethodFactory"), exports); | ||
__exportStar(require("./PrefixFactory"), exports); | ||
const factories = Symbol('log-factories'); | ||
class DefaultLogger extends LogLevel_1.LogLevel { | ||
constructor() { | ||
super({ name: 'default' }); | ||
this.cache = { default: this }; | ||
this[factories] = { MethodFactory: MethodFactory_1.MethodFactory, PrefixFactory: PrefixFactory_1.PrefixFactory }; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
LogLevel: () => LogLevel, | ||
MethodFactory: () => MethodFactory, | ||
PrefixFactory: () => PrefixFactory, | ||
default: () => src_default, | ||
defaultLevels: () => defaultLevels | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/MethodFactory.ts | ||
var defaultLevels = { | ||
TRACE: 0, | ||
DEBUG: 1, | ||
INFO: 2, | ||
WARN: 3, | ||
ERROR: 4, | ||
SILENT: 5 | ||
}; | ||
var levels = Symbol("log-levels"); | ||
var instance = Symbol("log-instance"); | ||
var noop = () => { | ||
}; | ||
var MethodFactory = class { | ||
constructor(logger) { | ||
this[instance] = logger; | ||
this[levels] = defaultLevels; | ||
} | ||
// @ts-ignore | ||
get levels() { | ||
return this[levels]; | ||
} | ||
get logger() { | ||
return this[instance]; | ||
} | ||
get methods() { | ||
return Object.keys(this.levels).map((key) => key.toLowerCase()).filter((key) => key !== "silent"); | ||
} | ||
set logger(logger) { | ||
this[instance] = logger; | ||
} | ||
// eslint-disable-next-line class-methods-use-this | ||
bindMethod(obj, methodName) { | ||
const method = obj[methodName]; | ||
if (typeof method.bind === "function") { | ||
return method.bind(obj); | ||
} | ||
get factories() { | ||
return this[factories]; | ||
try { | ||
return Function.prototype.bind.call(method, obj); | ||
} catch (e) { | ||
return function result() { | ||
return Function.prototype.apply.apply(method, [obj, arguments]); | ||
}; | ||
} | ||
get loggers() { | ||
return this.cache; | ||
} | ||
distillLevel(level) { | ||
let value; | ||
if (typeof level === "string") { | ||
const levels2 = this.levels; | ||
value = levels2[level.toUpperCase()]; | ||
} else { | ||
value = level; | ||
} | ||
create(opts) { | ||
let options; | ||
if (typeof opts === 'string') { | ||
options = { name: opts }; | ||
} | ||
else { | ||
options = Object.assign({}, opts); | ||
} | ||
if (!options.id) { | ||
options.id = options.name.toString(); | ||
} | ||
const { name, id } = options; | ||
const defaults = { level: this.level }; | ||
if (typeof name !== 'string' || !name || !name.length) { | ||
throw new TypeError('You must supply a name when creating a logger.'); | ||
} | ||
let logger = this.cache[id]; | ||
if (!logger) { | ||
logger = new LogLevel_1.LogLevel(Object.assign({}, defaults, options)); | ||
this.cache[id] = logger; | ||
} | ||
return logger; | ||
if (this.levelValid(value)) | ||
return value; | ||
return null; | ||
} | ||
levelValid(level) { | ||
const max = Math.max(...Object.values(this.levels)); | ||
if (typeof level === "number" && level >= 0 && level <= max) { | ||
return true; | ||
} | ||
} | ||
exports.default = new DefaultLogger(); | ||
//# sourceMappingURL=index.js.map | ||
return false; | ||
} | ||
/** | ||
* Build the best logging method possible for this env | ||
* Wherever possible we want to bind, not wrap, to preserve stack traces. | ||
* Since we're targeting modern browsers, there's no need to wait for the | ||
* console to become available. | ||
*/ | ||
// eslint-disable-next-line class-methods-use-this | ||
make(methodName) { | ||
const target = console; | ||
if (typeof target[methodName] !== "undefined") { | ||
return this.bindMethod(target, methodName); | ||
} else if (typeof console.log !== "undefined") { | ||
return this.bindMethod(target, "log"); | ||
} | ||
return noop; | ||
} | ||
replaceMethods(logLevel) { | ||
const level = this.distillLevel(logLevel); | ||
if (level === null) { | ||
throw new Error(`loglevelnext: replaceMethods() called with invalid level: ${logLevel}`); | ||
} | ||
if (!this.logger || this.logger.type !== "LogLevel") { | ||
throw new TypeError( | ||
"loglevelnext: Logger is undefined or invalid. Please specify a valid Logger instance." | ||
); | ||
} | ||
this.methods.forEach((methodName) => { | ||
const { [methodName.toUpperCase()]: methodLevel } = this.levels; | ||
this.logger[methodName] = methodLevel < level ? noop : this.make(methodName); | ||
}); | ||
this.logger.log = this.logger.debug; | ||
} | ||
}; | ||
instance, levels; | ||
// src/PrefixFactory.ts | ||
var defaults = { | ||
level: (opts) => `[${opts.level}]`, | ||
name: (opts) => opts.logger.name, | ||
template: "{{time}} {{level}} ", | ||
time: () => (/* @__PURE__ */ new Date()).toTimeString().split(" ")[0] | ||
}; | ||
var PrefixFactory = class extends MethodFactory { | ||
constructor(logger, options) { | ||
super(logger); | ||
this.options = Object.assign({}, defaults, options); | ||
} | ||
interpolate(level) { | ||
return this.options.template.replace(/{{([^{}]*)}}/g, (stache, prop) => { | ||
const fn = this.options[prop]; | ||
if (typeof fn === "function") { | ||
return fn({ level, logger: this.logger }); | ||
} | ||
return stache; | ||
}); | ||
} | ||
make(methodName) { | ||
const og = super.make(methodName); | ||
return (...args) => { | ||
const output = this.interpolate(methodName); | ||
const [first] = args; | ||
if (typeof first === "string") { | ||
args[0] = output + first; | ||
} else { | ||
args.unshift(output); | ||
} | ||
og(...args); | ||
}; | ||
} | ||
}; | ||
// src/LogLevel.ts | ||
var defaults2 = { | ||
factory: void 0, | ||
level: "warn", | ||
name: (+/* @__PURE__ */ new Date()).toString(), | ||
prefix: void 0 | ||
}; | ||
var LogLevel = class { | ||
constructor(options) { | ||
this.type = "LogLevel"; | ||
this.options = Object.assign({}, defaults2, options); | ||
this.methodFactory = options.factory; | ||
if (!this.methodFactory) { | ||
const factory = options.prefix ? new PrefixFactory(this, options.prefix) : new MethodFactory(this); | ||
this.methodFactory = factory; | ||
} | ||
if (!this.methodFactory.logger) { | ||
this.methodFactory.logger = this; | ||
} | ||
this.name = options.name || "<unknown>"; | ||
this.level = this.options.level ?? "trace"; | ||
} | ||
get level() { | ||
return this.currentLevel; | ||
} | ||
get levels() { | ||
return this.methodFactory.levels; | ||
} | ||
get factory() { | ||
return this.methodFactory; | ||
} | ||
set factory(factory) { | ||
factory.logger = this; | ||
this.methodFactory = factory; | ||
this.methodFactory.replaceMethods(this.level); | ||
} | ||
set level(logLevel) { | ||
const level = this.methodFactory.distillLevel(logLevel); | ||
if (level === false || level == null) { | ||
throw new RangeError(`loglevelnext: setLevel() called with invalid level: ${logLevel}`); | ||
} | ||
this.currentLevel = level; | ||
this.methodFactory.replaceMethods(level); | ||
const max = Math.max(...Object.values(this.levels)); | ||
if (typeof console === "undefined") { | ||
process.stdout.write("loglevelnext: console is undefined. The log will produce no output.\n"); | ||
} else if (level > max) { | ||
console.warn( | ||
`The log level has been set to a value greater than 'silent'. The log will produce no output.` | ||
); | ||
} | ||
} | ||
disable() { | ||
const levels2 = this.levels; | ||
if (levels2.SILENT) { | ||
this.level = levels2.SILENT; | ||
} else { | ||
console.warn( | ||
`loglevelnext: no 'silent' level defined. The log cannot be disabled. You may want to override the 'disable' method.` | ||
); | ||
} | ||
} | ||
enable() { | ||
const levels2 = this.levels; | ||
if (typeof levels2.TRACE !== "undefined") { | ||
this.level = levels2.TRACE; | ||
} else { | ||
console.warn( | ||
`loglevelnext: no 'trace' level defined. The log cannot be enabled. You may want to override the 'trace' method.` | ||
); | ||
} | ||
} | ||
}; | ||
// src/index.ts | ||
var factories = Symbol("log-factories"); | ||
var DefaultLogger = class extends LogLevel { | ||
constructor() { | ||
super({ name: "default" }); | ||
this.cache = { default: this }; | ||
this[factories] = { MethodFactory, PrefixFactory }; | ||
} | ||
get factories() { | ||
return this[factories]; | ||
} | ||
get loggers() { | ||
return this.cache; | ||
} | ||
create(opts) { | ||
let options; | ||
if (typeof opts === "string") { | ||
options = { name: opts }; | ||
} else { | ||
options = Object.assign({}, opts); | ||
} | ||
if (!options.id) { | ||
options.id = options.name.toString(); | ||
} | ||
const { name, id } = options; | ||
const defaults3 = { level: this.level }; | ||
if (typeof name !== "string" || !name || !name.length) { | ||
throw new TypeError("You must supply a name when creating a logger."); | ||
} | ||
let logger = this.cache[id]; | ||
if (!logger) { | ||
logger = new LogLevel(Object.assign({}, defaults3, options)); | ||
this.cache[id] = logger; | ||
} | ||
return logger; | ||
} | ||
}; | ||
var src_default = new DefaultLogger(); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
LogLevel, | ||
MethodFactory, | ||
PrefixFactory, | ||
defaultLevels | ||
}); |
{ | ||
"name": "loglevelnext", | ||
"version": "5.0.6", | ||
"version": "6.0.0", | ||
"description": "A modern logging library for Node.js and modern browsers that provides log level mapping to the console", | ||
@@ -9,21 +9,19 @@ "license": "MPL-2.0", | ||
"homepage": "https://github.com/shellscape/loglevelnext", | ||
"main": "dist/index.js", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
} | ||
}, | ||
"engines": { | ||
"node": ">= 12.22.1" | ||
"node": ">= 18" | ||
}, | ||
"scripts": { | ||
"build": "tsc --project tsconfig.json", | ||
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov", | ||
"commitlint": "commitlint", | ||
"commitmsg": "commitlint -e $GIT_PARAMS", | ||
"lint": "pnpm lint:docs && pnpm lint:json && pnpm lint:package && pnpm lint:js", | ||
"lint-staged": "lint-staged", | ||
"lint:docs": "prettier --write .github/**/*.md **/README.md", | ||
"lint:js": "eslint --fix --cache src test", | ||
"lint:json": "prettier --write tsconfig.*.json", | ||
"lint:package": "prettier --write **/package.json --plugin=prettier-plugin-package", | ||
"prepublishOnly": "pnpm build", | ||
"security": "pnpm audit --audit-level high", | ||
"test": "FORCE_COLOR=3 ava" | ||
}, | ||
"files": [ | ||
@@ -52,11 +50,11 @@ "dist", | ||
"devDependencies": { | ||
"@commitlint/cli": "12.1.4", | ||
"@commitlint/config-conventional": "12.1.4", | ||
"@types/node": "^15.12.2", | ||
"@commitlint/cli": "17.8.0", | ||
"@commitlint/config-conventional": "17.8.0", | ||
"@types/node": "^20.8.6", | ||
"@types/sinon": "^10.0.2", | ||
"ava": "^3.15.0", | ||
"ava": "^5.3.1", | ||
"chalk": "^4.0.0", | ||
"eslint-config-shellscape": "^4.0.1", | ||
"husky": "^4.3.8", | ||
"lint-staged": "11.0.0", | ||
"eslint-config-shellscape": "^6.0.1", | ||
"husky": "^8.0.3", | ||
"lint-staged": "15.0.1", | ||
"nyc": "^15.0.1", | ||
@@ -66,4 +64,6 @@ "pre-commit": "^1.2.2", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.3.2" | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.2.2" | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"ava": { | ||
@@ -95,3 +95,18 @@ "extensions": [ | ||
] | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format esm,cjs --dts", | ||
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov", | ||
"commitlint": "commitlint", | ||
"commitmsg": "commitlint -e $GIT_PARAMS", | ||
"lint": "pnpm lint:docs && pnpm lint:json && pnpm lint:package && pnpm lint:js", | ||
"lint-staged": "lint-staged", | ||
"lint:docs": "prettier --write .github/**/*.md **/README.md", | ||
"lint:js": "eslint --fix --cache src test", | ||
"lint:json": "prettier --write tsconfig.*.json", | ||
"lint:package": "prettier --write **/package.json --plugin=prettier-plugin-package", | ||
"prebuild": "tsc --project tsconfig.json", | ||
"security": "pnpm audit --audit-level high", | ||
"test": "FORCE_COLOR=3 ava" | ||
} | ||
} | ||
} |
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
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
54343
905
15
8
1