diff --git a/README.md b/README.md index d2df45aab..a36b8a0cc 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,7 @@ const logger = winston.createLogger({ const childLogger = logger.child({ requestId: '451' }); ``` +> `.child` is likely to be bugged if you're also extending the `Logger` class, due to some implementation details that make `this` keyword to point to unexpected things. Use with caution. ### Streams, `objectMode`, and `info` objects diff --git a/index.d.ts b/index.d.ts index 39e577642..929ef4ae0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -121,10 +121,10 @@ declare namespace winston { defaultMeta?: any; log: LogMethod; - add(transport: Transport): Logger; - remove(transport: Transport): Logger; - clear(): Logger; - close(): Logger; + add(transport: Transport): this; + remove(transport: Transport): this; + clear(): this; + close(): this; // for cli and npm levels error: LeveledLogMethod; @@ -153,11 +153,11 @@ declare namespace winston { stream(options?: any): NodeJS.ReadableStream; startTimer(): Profiler; - profile(id: string | number, meta?: Record): Logger; + profile(id: string | number, meta?: Record): this; configure(options: LoggerOptions): void; - child(options: Object): Logger; + child(options: Object): this; isLevelEnabled(level: string): boolean; isErrorEnabled(): boolean; diff --git a/lib/winston.js b/lib/winston.js index 2357d99e4..e9c12bf1e 100644 --- a/lib/winston.js +++ b/lib/winston.js @@ -40,6 +40,11 @@ exports.format = logform.format; * @type {function} */ exports.createLogger = require('./winston/create-logger'); +/** + * Expose core Logging-related prototypes. + * @type {function} + */ +exports.Logger = require('./winston/logger'); /** * Expose core Logging-related prototypes. * @type {Object} @@ -172,5 +177,4 @@ warn.forFunctions(exports, 'deprecated', [ 'extend' ]); warn.forProperties(exports, 'deprecated', ['emitErrs', 'levelLength']); -// Throw a useful error when users attempt to run `new winston.Logger`. -warn.moved(exports, 'createLogger', 'Logger'); + diff --git a/lib/winston/common.js b/lib/winston/common.js index 1516510c3..ac8e99d49 100644 --- a/lib/winston/common.js +++ b/lib/winston/common.js @@ -34,21 +34,6 @@ exports.warn = { obj[prop] = exports.warn[type](prop); }); }, - moved(obj, movedTo, prop) { - function movedNotice() { - return () => { - throw new Error([ - format('winston.%s was moved in winston@3.0.0.', prop), - format('Use a winston.%s instead.', movedTo) - ].join('\n')); - }; - } - - Object.defineProperty(obj, prop, { - get: movedNotice, - set: movedNotice - }); - }, forProperties(obj, type, props) { props.forEach(prop => { const notice = exports.warn[type](prop); diff --git a/test/integration/logger.test.js b/test/integration/logger.test.js new file mode 100644 index 000000000..756ca6e7b --- /dev/null +++ b/test/integration/logger.test.js @@ -0,0 +1,17 @@ +const assume = require('assume'); +const winston = require('../../lib/winston'); + +const Logger = winston.Logger; + +describe('Logger class', () => { + it('that Logger class is exported', () => { + Logger === require('../../lib/winston/logger'); + }); + + it('can be inherited', () => { + class CustomLogger extends Logger {} + const instance = new CustomLogger(); + assume(instance).instanceOf(CustomLogger); + assume(instance).instanceOf(Logger); + }); +});