Comparing version 1.0.11 to 1.1.0-beta.0
@@ -1,88 +0,154 @@ | ||
export type BaseLevels = { | ||
error: 1, | ||
warn: 2, | ||
info: 3, | ||
log: 4, | ||
debug: 5, | ||
trace: 6, | ||
} | ||
export interface BaseLogger<L extends BaseLevels = BaseLevels> { | ||
/** | ||
* The name of this logger. | ||
*/ | ||
name: string | ||
(level: keyof L, message?: any, ...args: any[]): void | ||
(message?: any, ...args: any[]): void | ||
error(message?: any, ...args: any[]): void | ||
warn(message?: any, ...args: any[]): void | ||
info(message?: any, ...args: any[]): void | ||
log(message?: any, ...args: any[]): void | ||
debug(message?: any, ...args: any[]): void | ||
trace(message?: any, ...args: any[]): void | ||
enabledFor(level: keyof L): boolean | ||
} | ||
export type Logger<L extends BaseLevels = BaseLevels> = BaseLogger<L> & { | ||
[P in keyof Omit<L, keyof BaseLevels>]: (message?: any, ...args: any[]) => void | ||
} | ||
export interface AnyLogger<L extends BaseLevels, T extends BaseLogger<L> = Logger<L>> { | ||
/** | ||
* Returns an object containing all loggers created so far, keyed by name. | ||
*/ | ||
(): { [name: string]: T } | ||
/** | ||
* @param name The name of the new logger. | ||
* @param config An optional config object. | ||
* @returns A logger with the given `name` and `config`. | ||
*/ | ||
(name: string, config?: object | undefined): T | ||
/** | ||
* An object containing a mapping of level names to level values. | ||
*/ | ||
levels: L & { [name: string]: number } | ||
/** | ||
* Creates a new logger function that calls `anylogger.log` when invoked. | ||
* | ||
* @param name The name of the new logger. | ||
* @param config An optional config object. | ||
* @returns A new logger function with the given `name`. | ||
*/ | ||
new(name: string, config?: object | undefined): T | ||
/** | ||
* The log function used by `anylogger.new`. | ||
* | ||
* @param name The name of the logger to use | ||
* @param level The log level. | ||
* @param message The (formatted) message or any data to log. | ||
* @param [params] Additional log (formatting) arguments. | ||
*/ | ||
log(name: string, level: keyof L, message?: any, ...args: any[]): void | ||
/** | ||
* The log function used by `anylogger.new`. | ||
* | ||
* @param name The name of the logger to use | ||
* @param args The log arguments. | ||
* @param message The (formatted) message or any data to log. | ||
* @param [params] Additional log (formatting) arguments. | ||
*/ | ||
log(name: string, message?: any, ...args: any[]): void | ||
/** | ||
* @param logger The logger that should be (re-)extended. | ||
* @return The logger that was given, extended. | ||
*/ | ||
ext(logger: T): T | ||
} | ||
declare const anylogger: AnyLogger<BaseLevels> | ||
export default anylogger | ||
/** | ||
* A N Y L O G G E R | ||
* Get a logger. Any logger. | ||
* | ||
* © 2024 by Stijn de Witt, some rights reserved | ||
* Licensed under the MIT Open Source license | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
/** | ||
* Gets or creates a logger by name | ||
*/ | ||
export type AnyLogger = ((name: LoggerName) => Logger) & { | ||
/** | ||
* Stores all loggers created so far | ||
*/ | ||
all: AllLoggers; | ||
/** | ||
* An object containing a mapping of level names to level values. | ||
* | ||
* To be compliant with the anylogger API, loggers should support at least | ||
* the log methods corresponding to the default levels specified here, but | ||
* they may define additional levels and they may choose to use different | ||
* numeric values for all the levels. | ||
* | ||
* The guarantees the Anylogger API makes are: | ||
* - there is a log method for each level listed in anylogger.levels | ||
* - the levels error, warn, info, log, debug and trace are always there | ||
* - each level corresponds to a numeric value | ||
* | ||
* Note that the Anylogger API explicitly does not guarantee that all levels | ||
* have distinct values or that the numeric values will follow any pattern | ||
* or have any specific order. For this reason it is best to think of levels | ||
* as separate log channels, possibly going to different output locations, | ||
* instead of thinking of them as having a specific order. | ||
* | ||
* Adapters can modify this object to include levels corresponding with | ||
* those available in the logging library the adapter is for. Adapters will | ||
* ensure to always include the default levels and to have a log method for | ||
* each level, so all code can rely on that contract. | ||
*/ | ||
levels: { | ||
[level: string]: number; | ||
}; | ||
/** | ||
* Called when a new logger needs to be created. | ||
* | ||
* The default implementation creates a log function that | ||
* allows for an optional first argument, preceding the | ||
* message argument(s), to specify the level to call. | ||
* | ||
* | ||
* @param name The name for the new logger | ||
* @returns The newly created logger | ||
*/ | ||
new: (name: LoggerName) => LogFunction; | ||
/** | ||
* Called by the log function that the default implementation of | ||
* `anylogger.new` creates. | ||
* | ||
* This log function calls the right log method on the right logger, | ||
* based on the `name` of the logger and the arguments given in `args`. | ||
* | ||
* if there is more than one argument given and the first argument is a | ||
* string that corresponds to a log level, that level will be called. | ||
* Otherwise it defaults to calling the `log` method. | ||
* | ||
* E.g. | ||
* | ||
* ```ts | ||
* log('message') // calls log.log('message') | ||
* log('error') // calls log.log('error') | ||
* log('info', 'message') // calls log.info('message') | ||
* log('error', 'message') // calls log.error('message') | ||
* log('Hello', 'World!') // calls log.log('Hello', 'World!') | ||
* ``` | ||
* | ||
* Having this code in anylogger makes writing adapters easier, because | ||
* they only have to override `anylogger.ext` and add the logging methods | ||
* to the new logger. | ||
* | ||
* @param name The name of the logger | ||
* @param args The arguments for the logger | ||
*/ | ||
log: (name: LoggerName, ...args: any) => void; | ||
/** | ||
* Called when a log function needs to be extended, either because it was | ||
* newly created, or because it's configuration or settings changed. | ||
* | ||
* This function implements `enabledFor` and a log method for | ||
* each level in `anylogger.levels` on the given `logfn`. | ||
* | ||
* This function can safely be called multiple times on the same `logfn`. | ||
* | ||
* The default adapter provided here is essentially a no-op adapter. | ||
* Adapters for common logging frameworks such as the | ||
* [console](https://npmjs.com/package/anylogger-console), | ||
* [debug](https://npmjs.com/package/anylogger-debug), | ||
* [loglevel](https://npmjs.com/package/anylogger-loglevel), | ||
* [ulog](https://npmjs.com/package/ulog) and | ||
* [log4js](https://npmjs.com/package/log4js) override | ||
* this default adapter. | ||
* | ||
* @param logfn The log function to be extended | ||
* | ||
* @return The log function that was given, extended to a Logger | ||
*/ | ||
ext: Adapter; | ||
}; | ||
/** | ||
* A log function is a function that takes a variable amount of | ||
* arguments and returns void. | ||
*/ | ||
export type LogFunction = (...args: any) => void; | ||
/** | ||
* An adapter accepts a LogFunction and returns a Logger | ||
*/ | ||
export type Adapter = (logfn: LogFunction) => Logger; | ||
/** | ||
* A logger is a log function that has a `name` that corresponds to the logger | ||
* name, a method `enabledFor(level: LogLevel)` to check whether the logger is | ||
* enabled for a certain log level, and log methods for each of the log levels | ||
* supported by AnyLogger: `error`, `warn`, `info`, `log`, `debug` and `trace`. | ||
*/ | ||
export type Logger = LogFunction & { | ||
readonly name: LoggerName; | ||
enabledFor: (level?: LogLevel) => boolean | void; | ||
} & { | ||
[P in keyof LogLevels as `${P}`]: LogFunction; | ||
}; | ||
export type LogLevels = { | ||
error: 1; | ||
warn: 2; | ||
info: 3; | ||
log: 4; | ||
debug: 5; | ||
trace: 6; | ||
}; | ||
/** | ||
* A log level is a string that is a key of `LogLevels` | ||
*/ | ||
export type LogLevel = keyof LogLevels; | ||
/** | ||
* All loggers, keyed by name | ||
*/ | ||
export type AllLoggers = { | ||
[name: string]: Logger; | ||
}; | ||
/** | ||
* An alias for the much used concept of a LoggerName | ||
*/ | ||
export type LoggerName = string; | ||
declare const anylogger: AnyLogger; | ||
export default anylogger; | ||
//# sourceMappingURL=anylogger.d.ts.map |
var anylogger = (function () { | ||
/** | ||
* A N Y L O G G E R | ||
* Get a logger. Any logger. | ||
* | ||
* © 2020 by Stijn de Witt, some rights reserved | ||
* Licensed under the MIT Open Source license | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
/** | ||
* A N Y L O G G E R | ||
* Get a logger. Any logger. | ||
* | ||
* © 2024 by Stijn de Witt, some rights reserved | ||
* Licensed under the MIT Open Source license | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
// the main `anylogger` function | ||
const anylogger = (name) => ( | ||
// return the existing logger, or | ||
anylogger.all[name] || | ||
// create and store a new logger with that name | ||
(anylogger.all[name] = anylogger.ext(anylogger.new(name)))); | ||
// all loggers created so far | ||
anylogger.all = Object.create(null); | ||
// the supported levels | ||
anylogger.levels = { error: 1, warn: 2, info: 3, log: 4, debug: 5, trace: 6 }; | ||
// creates a new named log function | ||
anylogger.new = (name) => ({ | ||
// to assign the function `name`, set it to a named key in an object. | ||
// the default implementation calls `anylogger.log`, which should be a | ||
// good choice in many cases. | ||
[name]: (...args) => anylogger.log(name, ...args) | ||
}[name]); // return only the function, not the encapsulating object | ||
// logs with the logger with the given `name` | ||
anylogger.log = (name, ...args) => { | ||
// select the logger to use | ||
anylogger.all[name][ | ||
// select the level to use | ||
// if multiple args and first matches a level name | ||
(((args.length > 1) && anylogger.levels[args[0]]) | ||
? args.shift() // use the level from the args | ||
: 'log' // else use default level `'log'` | ||
)](...args); // call method matching level with remaining args | ||
}; | ||
// extends the given `logger` function | ||
// the implementation here only adds no-ops | ||
// adapters should change this behavior | ||
anylogger.ext = (logger) => { | ||
logger.enabledFor = () => { }; | ||
for (const method in anylogger.levels) { | ||
logger[method] = () => { }; | ||
} | ||
return logger; | ||
}; | ||
// stores loggers keyed by name | ||
var loggers = Object.create(null); | ||
return anylogger; | ||
/** | ||
* anylogger([name] [, options]) => function logger([level='log'] [, ...args]) | ||
* | ||
* The main `anylogger` function creates a new or returns an existing logger | ||
* with the given `name`. It maintains a registry of all created loggers, | ||
* which it returns when called without a name, or with an empty name. | ||
* | ||
* If anylogger needs to create a new logger, it invokes | ||
* [`anylogger.new`](#anyloggernew). | ||
* | ||
* @param name {String} The name of the logger to create | ||
* @param options {Object} An optional options object | ||
* | ||
* @returns A logger with the given `name` and `options`. | ||
*/ | ||
var anylogger = function(name, options){ | ||
// return the existing logger, or create a new one. if no name was given, return all loggers | ||
return name ? loggers[name] || (loggers[name] = anylogger.ext(anylogger.new(name, options))) : loggers | ||
}; | ||
/** | ||
* `anylogger.levels` | ||
* | ||
* An object containing a mapping of level names to level values. | ||
* | ||
* To be compliant with the anylogger API, loggers should support at least | ||
* the log methods corresponding to the default levels, but they may define | ||
* additional levels and they may choose to use different numeric values | ||
* for all the levels. | ||
* | ||
* The guarantees the Anylogger API makes are: | ||
* - there is a logging method corresponding to each level listed in anylogger.levels | ||
* - the levels error, warn, info, log, debug and trace are always there | ||
* - each level corresponds to a numeric value | ||
* | ||
* Note that the Anylogger API explicitly does not guarantee that all levels | ||
* have distinct values or that the numeric values will follow any pattern | ||
* or have any specific order. For this reason it is best to think of levels | ||
* as separate log channels, possibly going to different output locations. | ||
* | ||
* You can replace or change this object to include levels corresponding with | ||
* those available in the framework you are writing an adapter for. Please | ||
* make sure to always include the default levels as well so all code can | ||
* rely on the 6 console methods `error`, `warn`, `info`, `log`, `debug` and | ||
* `trace` to always be there. | ||
*/ | ||
anylogger.levels = { error: 1, warn: 2, info: 3, log: 4, debug: 5, trace: 6 }; | ||
/** | ||
* `anylogger.new(name, options)` | ||
* | ||
* Creates a new logger function that calls `anylogger.log` when invoked. | ||
* | ||
* @param name {String} The name of the logger to create | ||
* @param options {Object} An optional options object | ||
* | ||
* @returns A new logger function with the given `name`. | ||
*/ | ||
anylogger.new = function(name, options) { | ||
var result = {}; | ||
result[name] = function() {anylogger.log(name, [].slice.call(arguments));}; | ||
// some old browsers dont'create the function.name property. polyfill it for those | ||
try {Object.defineProperty(result[name], 'name', {get:function(){return name}});} catch(e) {} | ||
return result[name] | ||
}; | ||
/** | ||
* `anylogger.log(name, args)` | ||
* | ||
* The log function used by the logger created by `anylogger.new`. | ||
* | ||
* You can override this method to change invocation behavior. | ||
* | ||
* @param name {String} The name of the logger to use. Required. Not empty. | ||
* @param args {Array} The log arguments. Required. May be empty. | ||
* | ||
* If multiple arguments were given in `args` and the first argument is a | ||
* log level name from anylogger.levels, this method will remove that argument | ||
* and call the corresponding log method with the remaining arguments. | ||
* Otherwise it will call the `log` method with the arguments given. | ||
*/ | ||
anylogger.log = function(name, args) { | ||
var level = args.length > 1 && anylogger.levels[args[0]] ? args.shift() : 'log'; | ||
loggers[name][level].apply(loggers[name], args); | ||
}; | ||
/** | ||
* `anylogger.ext(logger) => logger` | ||
* | ||
* Called when a logger needs to be extended, either because it was newly | ||
* created, or because it's configuration or settings changed in some way. | ||
* | ||
* This method must ensure that a log method is available on the logger for | ||
* each level in `anylogger.levels`. | ||
* | ||
* When overriding `anylogger.ext`, please ensure the function can safely | ||
* be called multiple times on the same object | ||
* | ||
* @param logger Function The logger to be (re-)extended | ||
* | ||
* @return The logger that was given, extended | ||
*/ | ||
anylogger.ext = function(logger) { | ||
logger.enabledFor = function(){}; | ||
for (var method in anylogger.levels) {logger[method] = function(){};} | ||
return logger | ||
}; | ||
return anylogger; | ||
}()); | ||
})(); |
158
anylogger.js
@@ -5,119 +5,47 @@ /** | ||
* | ||
* © 2020 by Stijn de Witt, some rights reserved | ||
* © 2024 by Stijn de Witt, some rights reserved | ||
* Licensed under the MIT Open Source license | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
// stores loggers keyed by name | ||
var loggers = Object.create(null) | ||
/** | ||
* anylogger([name] [, options]) => function logger([level='log'] [, ...args]) | ||
* | ||
* The main `anylogger` function creates a new or returns an existing logger | ||
* with the given `name`. It maintains a registry of all created loggers, | ||
* which it returns when called without a name, or with an empty name. | ||
* | ||
* If anylogger needs to create a new logger, it invokes | ||
* [`anylogger.new`](#anyloggernew). | ||
* | ||
* @param name {String} The name of the logger to create | ||
* @param options {Object} An optional options object | ||
* | ||
* @returns A logger with the given `name` and `options`. | ||
*/ | ||
var anylogger = function(name, options){ | ||
// return the existing logger, or create a new one. if no name was given, return all loggers | ||
return name ? loggers[name] || (loggers[name] = anylogger.ext(anylogger.new(name, options))) : loggers | ||
} | ||
/** | ||
* `anylogger.levels` | ||
* | ||
* An object containing a mapping of level names to level values. | ||
* | ||
* To be compliant with the anylogger API, loggers should support at least | ||
* the log methods corresponding to the default levels, but they may define | ||
* additional levels and they may choose to use different numeric values | ||
* for all the levels. | ||
* | ||
* The guarantees the Anylogger API makes are: | ||
* - there is a logging method corresponding to each level listed in anylogger.levels | ||
* - the levels error, warn, info, log, debug and trace are always there | ||
* - each level corresponds to a numeric value | ||
* | ||
* Note that the Anylogger API explicitly does not guarantee that all levels | ||
* have distinct values or that the numeric values will follow any pattern | ||
* or have any specific order. For this reason it is best to think of levels | ||
* as separate log channels, possibly going to different output locations. | ||
* | ||
* You can replace or change this object to include levels corresponding with | ||
* those available in the framework you are writing an adapter for. Please | ||
* make sure to always include the default levels as well so all code can | ||
* rely on the 6 console methods `error`, `warn`, `info`, `log`, `debug` and | ||
* `trace` to always be there. | ||
*/ | ||
anylogger.levels = { error: 1, warn: 2, info: 3, log: 4, debug: 5, trace: 6 } | ||
/** | ||
* `anylogger.new(name, options)` | ||
* | ||
* Creates a new logger function that calls `anylogger.log` when invoked. | ||
* | ||
* @param name {String} The name of the logger to create | ||
* @param options {Object} An optional options object | ||
* | ||
* @returns A new logger function with the given `name`. | ||
*/ | ||
anylogger.new = function(name, options) { | ||
var result = {} | ||
result[name] = function() {anylogger.log(name, [].slice.call(arguments))} | ||
// some old browsers dont'create the function.name property. polyfill it for those | ||
try {Object.defineProperty(result[name], 'name', {get:function(){return name}})} catch(e) {} | ||
return result[name] | ||
} | ||
/** | ||
* `anylogger.log(name, args)` | ||
* | ||
* The log function used by the logger created by `anylogger.new`. | ||
* | ||
* You can override this method to change invocation behavior. | ||
* | ||
* @param name {String} The name of the logger to use. Required. Not empty. | ||
* @param args {Array} The log arguments. Required. May be empty. | ||
* | ||
* If multiple arguments were given in `args` and the first argument is a | ||
* log level name from anylogger.levels, this method will remove that argument | ||
* and call the corresponding log method with the remaining arguments. | ||
* Otherwise it will call the `log` method with the arguments given. | ||
*/ | ||
anylogger.log = function(name, args) { | ||
var level = args.length > 1 && anylogger.levels[args[0]] ? args.shift() : 'log' | ||
loggers[name][level].apply(loggers[name], args) | ||
} | ||
/** | ||
* `anylogger.ext(logger) => logger` | ||
* | ||
* Called when a logger needs to be extended, either because it was newly | ||
* created, or because it's configuration or settings changed in some way. | ||
* | ||
* This method must ensure that a log method is available on the logger for | ||
* each level in `anylogger.levels`. | ||
* | ||
* When overriding `anylogger.ext`, please ensure the function can safely | ||
* be called multiple times on the same object | ||
* | ||
* @param logger Function The logger to be (re-)extended | ||
* | ||
* @return The logger that was given, extended | ||
*/ | ||
anylogger.ext = function(logger) { | ||
logger.enabledFor = function(){} | ||
for (var method in anylogger.levels) {logger[method] = function(){}} | ||
return logger | ||
} | ||
export default anylogger | ||
// the main `anylogger` function | ||
const anylogger = (name) => ( | ||
// return the existing logger, or | ||
anylogger.all[name] || | ||
// create and store a new logger with that name | ||
(anylogger.all[name] = anylogger.ext(anylogger.new(name)))); | ||
// all loggers created so far | ||
anylogger.all = Object.create(null); | ||
// the supported levels | ||
anylogger.levels = { error: 1, warn: 2, info: 3, log: 4, debug: 5, trace: 6 }; | ||
// creates a new named log function | ||
anylogger.new = (name) => ({ | ||
// to assign the function `name`, set it to a named key in an object. | ||
// the default implementation calls `anylogger.log`, which should be a | ||
// good choice in many cases. | ||
[name]: (...args) => anylogger.log(name, ...args) | ||
}[name]); // return only the function, not the encapsulating object | ||
// logs with the logger with the given `name` | ||
anylogger.log = (name, ...args) => { | ||
// select the logger to use | ||
anylogger.all[name][ | ||
// select the level to use | ||
// if multiple args and first matches a level name | ||
(((args.length > 1) && anylogger.levels[args[0]]) | ||
? args.shift() // use the level from the args | ||
: 'log' // else use default level `'log'` | ||
)](...args); // call method matching level with remaining args | ||
}; | ||
// extends the given `logger` function | ||
// the implementation here only adds no-ops | ||
// adapters should change this behavior | ||
anylogger.ext = (logger) => { | ||
logger.enabledFor = () => { }; | ||
for (const method in anylogger.levels) { | ||
logger[method] = () => { }; | ||
} | ||
return logger; | ||
}; | ||
// this is a real ESM module | ||
// we transpile the compiled Javascript back to commonjs with rollup | ||
export default anylogger; | ||
//# sourceMappingURL=anylogger.js.map |
@@ -1,1 +0,1 @@ | ||
var anylogger=function(){var t=Object.create(null),l=function(n,e){return n?t[n]||(t[n]=l.ext(l.new(n,e))):t};return l.levels={error:1,warn:2,info:3,log:4,debug:5,trace:6},l.new=function(n,e){var r={};r[n]=function(){l.log(n,[].slice.call(arguments))};try{Object.defineProperty(r[n],"name",{get:function(){return n}})}catch(n){}return r[n]},l.log=function(n,e){var r=1<e.length&&l.levels[e[0]]?e.shift():"log";t[n][r].apply(t[n],e)},l.ext=function(n){for(var e in n.enabledFor=function(){},l.levels)n[e]=function(){};return n},l}(); | ||
var anylogger=function(){const n=l=>n.all[l]||(n.all[l]=n.ext(n.new(l)));return n.all=Object.create(null),n.levels={error:1,warn:2,info:3,log:4,debug:5,trace:6},n.new=e=>({[e]:(...l)=>n.log(e,...l)})[e],n.log=(l,...e)=>{n.all[l][1<e.length&&n.levels[e[0]]?e.shift():"log"](...e)},n.ext=l=>{l.enabledFor=()=>{};for(const e in n.levels)l[e]=()=>{};return l},n}(); |
@@ -1,7 +0,4 @@ | ||
var expect = require('chai').expect | ||
var sinon = require('sinon') | ||
var anylogger = require('./anylogger.cjs') | ||
import { expect } from 'chai' | ||
import anylogger from 'anylogger' | ||
var sandbox = sinon.createSandbox(); | ||
describe('anylogger([name, [options]]) => log', function() { | ||
@@ -13,4 +10,2 @@ afterEach(function(){ | ||
}) | ||
// restore sandbox methods | ||
sandbox.restore() | ||
}) | ||
@@ -22,12 +17,2 @@ | ||
it('returns an object mapping names to loggers when called without arguments', function(){ | ||
var result = anylogger() | ||
expect(result).to.be.an('object') | ||
expect(Object.keys(result)).to.deep.eq([]) | ||
anylogger('test') | ||
result = anylogger() | ||
expect(result).to.be.an('object') | ||
expect(Object.keys(result)).to.deep.eq(['test']) | ||
}) | ||
it('returns a named logger when called with a name', function(){ | ||
@@ -47,41 +32,2 @@ var name = 'test' | ||
it('calls anylogger.new when a new logger named "test" is created', function(){ | ||
sandbox.spy(anylogger, 'new') | ||
expect(anylogger.new.callCount).to.equal(0) | ||
anylogger('test') | ||
expect(anylogger.new.callCount).to.equal(1) | ||
}) | ||
it('Calls anylogger.ext when a new logger named "test" is created', function(){ | ||
sandbox.spy(anylogger, 'ext') | ||
expect(anylogger.ext.callCount).to.equal(0) | ||
anylogger('test') | ||
expect(anylogger.ext.callCount).to.equal(1) | ||
}) | ||
it('does not call anylogger.new on subsequent calls with the same name', function(){ | ||
sandbox.spy(anylogger, 'new') | ||
expect(anylogger.new.callCount).to.equal(0) | ||
anylogger('test') | ||
expect(anylogger.new.callCount).to.equal(1) | ||
anylogger('test') | ||
expect(anylogger.new.callCount).to.equal(1) | ||
}) | ||
it('calls anylogger.new when a new logger named "toString" is created', function(){ | ||
sandbox.spy(anylogger, 'new') | ||
expect(anylogger.new.callCount).to.equal(0) | ||
anylogger('toString') | ||
expect(anylogger.new.callCount).to.equal(1) | ||
}) | ||
it('does not call anylogger.new on subsequent calls with "toString" as argument', function(){ | ||
sandbox.spy(anylogger, 'new') | ||
expect(anylogger.new.callCount).to.equal(0) | ||
anylogger('toString') | ||
expect(anylogger.new.callCount).to.equal(1) | ||
anylogger('toString') | ||
expect(anylogger.new.callCount).to.equal(1) | ||
}) | ||
it('accepts an optional options argument', function(){ | ||
@@ -148,5 +94,3 @@ var name = 'test' | ||
var log = anylogger('test') | ||
sandbox.spy(log, 'enabledFor') | ||
log.enabledFor('info') | ||
expect(log.enabledFor.callCount).to.equal(1) | ||
expect(log.enabledFor('info')).to.equal(undefined) | ||
}) | ||
@@ -156,5 +100,3 @@ | ||
var log = anylogger('test') | ||
sandbox.spy(log, 'log') | ||
log('message') | ||
expect(log.log.callCount).to.equal(1) | ||
expect(() => log('message')).not.to.throw() | ||
}) | ||
@@ -164,9 +106,12 @@ | ||
var log = anylogger('test') | ||
sandbox.spy(log, 'log') | ||
sandbox.spy(log, 'info') | ||
const old = log.info | ||
let called = false | ||
log.info = (...args) => { | ||
called = true | ||
old(...args) | ||
} | ||
log('info', 'message') | ||
expect(log.log.callCount).to.equal(0) | ||
expect(log.info.callCount).to.equal(1) | ||
expect(called).to.equal(true) | ||
}) | ||
}) | ||
}) |
{ | ||
"name": "anylogger", | ||
"version": "1.0.11", | ||
"version": "1.1.0-beta.0", | ||
"type": "module", | ||
"description": "Get a logger. Any logger.", | ||
"src": "./anylogger.js", | ||
"main": "./anylogger.cjs.js", | ||
"main": "./anylogger.js", | ||
"cjs": "./anylogger.cjs", | ||
"iife": "./anylogger.iife.js", | ||
@@ -12,13 +14,21 @@ "min": "./anylogger.min.js", | ||
"unpkg": "./anylogger.min.js", | ||
"exports": { | ||
".": { | ||
"import": "./anylogger.js", | ||
"require": "./anylogger.cjs" | ||
} | ||
}, | ||
"files": [ | ||
"anylogger.cjs", | ||
"anylogger.d.ts", | ||
"anylogger.d.ts.map", | ||
"anylogger.iife.js", | ||
"anylogger.js", | ||
"anylogger.cjs.js", | ||
"anylogger.iife.js", | ||
"anylogger.min.js", | ||
"anylogger.d.ts", | ||
"anylogger.spec.js", | ||
"test.html" | ||
"anylogger.ts" | ||
], | ||
"scripts": { | ||
"build": "npm run test -s && npm run minify -s && npm run docs -s", | ||
"build": "tsc && npm run test -s && npm run minify -s && npm run docs -s", | ||
"clean": "rimraf anylogger.cjs anylogger.d.ts anylogger.d.ts.map anylogger.iife.js anylogger.js anylogger.js.map anylogger.min.js", | ||
"docs": "cross-env NODE_ENV=production node build.js docs", | ||
@@ -28,3 +38,3 @@ "minify": "cross-env NODE_ENV=production node build.js minify", | ||
"prepare": "npm run build", | ||
"test": "npm run package -s && tsc && node ./test.js && mocha anylogger.spec.js" | ||
"test": "npm run package -s && mocha anylogger.spec.js" | ||
}, | ||
@@ -37,12 +47,13 @@ "author": "Stijn de Witt", | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"chai": "^4.3.4", | ||
"@types/node": "^20.11.19", | ||
"chai": "^5.1.0", | ||
"cross-env": "^7.0.3", | ||
"gzip-size": "^6.0.0", | ||
"mocha": "^8.3.2", | ||
"rollup": "^2.44.0", | ||
"sinon": "^10.0.0", | ||
"typescript": "^4.2.3", | ||
"uglify-js": "^3.13.3" | ||
"gzip-size": "^7.0.0", | ||
"mocha": "^10.3.0", | ||
"rimraf": "^5.0.5", | ||
"rollup": "^4.12.0", | ||
"sinon": "^17.0.1", | ||
"typescript": "^5.3.3", | ||
"uglify-js": "^3.17.4" | ||
}, | ||
@@ -49,0 +60,0 @@ "keywords": [ |
@@ -6,3 +6,2 @@ # anylogger <sub><sup>1.0.11</sup></sub> | ||
[![license](https://img.shields.io/npm/l/anylogger.svg)](https://opensource.org/licenses/MIT) | ||
[![travis](https://img.shields.io/travis/Download/anylogger.svg)](https://travis-ci.org/github/Download/anylogger) | ||
![mind BLOWN](https://img.shields.io/badge/mind-BLOWN-ff69b4.svg) | ||
@@ -24,6 +23,10 @@ | ||
<th><h3>Library</h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/anylogger-debug"><tt>debug</tt></a></h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/anylogger-loglevel"><tt>loglevel</tt></a></h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/ulog"><tt>ulog</tt></a></h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/anylogger-log4js"><tt>log4js</tt></a></h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/anylogger-debug" | ||
><tt>debug</tt></a></h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/anylogger-loglevel" | ||
><tt>loglevel</tt></a></h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/ulog" | ||
><tt>ulog</tt></a></h3></th> | ||
<th><h3>App with <a href="https://npmjs.com/package/anylogger-log4js" | ||
><tt>log4js</tt></a></h3></th> | ||
</tr> | ||
@@ -109,3 +112,3 @@ <tr> | ||
A tiny ~[330](#gzip-size) bytes logging facade that you can include in your | ||
A tiny ~[264](#gzip-size) bytes logging facade that you can include in your | ||
library to support logging, while at the same time allowing application | ||
@@ -117,3 +120,3 @@ developers to plug in any logging framework they choose. | ||
or just abandoning logging altogether, choose `anylogger` and for just | ||
~[330](#gzip-size) bytes shared between all libraries doing this, we can | ||
~[264](#gzip-size) bytes shared between all libraries doing this, we can | ||
plug in any framework of our choice and all libraries will automatically | ||
@@ -125,6 +128,12 @@ start to use that framework. Wouldn't it be much better and easier? | ||
* [anylogger.js](https://unpkg.com/anylogger@1.0.11/anylogger.js) | ||
(fully commented source ~5kB) | ||
* [anylogger.min.js](https://unpkg.com/anylogger@1.0.11) | ||
(minified 532 bytes, gzipped ~[330](#gzip-size) bytes) | ||
* [anylogger.ts](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.ts) | ||
(fully commented source ~6kB) | ||
* [anylogger.d.ts](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.d.ts) | ||
(typescript type definitions ~5kB) | ||
* [anylogger.js](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.js) | ||
(transpiled es6 ecmascript module ~1kB) | ||
* [anylogger.cjs](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.cjs) | ||
(transpiled es6 commonjs module ~1kB) | ||
* [anylogger.min.js](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.min.js) | ||
(minified 361 bytes, gzipped ~[264](#gzip-size) bytes) | ||
@@ -136,3 +145,3 @@ | ||
```html | ||
<script src="https://unpkg.com/anylogger@1.0.11"></script> | ||
<script src="https://unpkg.com/anylogger@1.1.0-beta.0"></script> | ||
<script>(function(){ // IIFE | ||
@@ -585,3 +594,3 @@ var log = anylogger('index.html') | ||
Please have a look at the | ||
[source](https://unpkg.com/anylogger@1.0.11/anylogger.js) | ||
[source](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.js) | ||
it should make it more clear how to write an adapter. Also consider studying | ||
@@ -588,0 +597,0 @@ the [available adapters](https://www.npmjs.com/search?q=keywords:anylogger) |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
46242
11
578
634
Yes
10
2
1