From 4ae9c995b25f44a118a55cc7455548dbb58f4181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Sun, 21 May 2023 03:56:30 +0200 Subject: [PATCH] set export last in the the files --- README.md | 4 +- examples/color-message.js | 4 +- examples/custom-levels-colors.js | 4 +- examples/format-dynamic-content.js | 4 +- lib/winston.js | 228 +++++++++---------- lib/winston/config/index.js | 15 +- lib/winston/container.js | 4 +- lib/winston/create-logger.js | 4 +- lib/winston/exception-stream.js | 5 +- lib/winston/profiler.js | 4 +- lib/winston/rejection-handler.js | 4 +- lib/winston/tail-file.js | 4 +- lib/winston/transports/console.js | 4 +- lib/winston/transports/file.js | 4 +- lib/winston/transports/http.js | 4 +- lib/winston/transports/stream.js | 4 +- test/helpers/mocks/legacy-mixed-transport.js | 6 +- test/helpers/mocks/legacy-transport.js | 4 +- 18 files changed, 162 insertions(+), 148 deletions(-) diff --git a/README.md b/README.md index d2df45aab..bbca57c1a 100644 --- a/README.md +++ b/README.md @@ -729,7 +729,7 @@ const util = require('util'); // Inherit from `winston-transport` so you can take advantage // of the base functionality and `.exceptions.handle()`. // -module.exports = class YourCustomTransport extends Transport { +class YourCustomTransport extends Transport { constructor(opts) { super(opts); // @@ -749,6 +749,8 @@ module.exports = class YourCustomTransport extends Transport { callback(); } }; + +module.exports = YourCustomTransport; ``` ## Common Transport options diff --git a/examples/color-message.js b/examples/color-message.js index e18b7c74f..3c1a997c6 100644 --- a/examples/color-message.js +++ b/examples/color-message.js @@ -2,7 +2,7 @@ const winston = require('../'); -const logger = module.exports = winston.createLogger({ +const logger = winston.createLogger({ transports: [new winston.transports.Console()], format: winston.format.combine( winston.format.colorize({ all: true }), @@ -11,3 +11,5 @@ const logger = module.exports = winston.createLogger({ }); logger.log('info', 'This is an information message.'); + +module.exports = logger; diff --git a/examples/custom-levels-colors.js b/examples/custom-levels-colors.js index 5731c606c..1b4137fb1 100644 --- a/examples/custom-levels-colors.js +++ b/examples/custom-levels-colors.js @@ -30,7 +30,7 @@ const config = { winston.addColors(config.colors); -const logger = module.exports = winston.createLogger({ +const logger = winston.createLogger({ levels: config.levels, format: winston.format.combine( winston.format.colorize(), @@ -43,3 +43,5 @@ const logger = module.exports = winston.createLogger({ }); logger.custom('hello') + +module.exports = logger; diff --git a/examples/format-dynamic-content.js b/examples/format-dynamic-content.js index 40c32fa4e..3ed9fdf70 100644 --- a/examples/format-dynamic-content.js +++ b/examples/format-dynamic-content.js @@ -2,7 +2,7 @@ const winston = require('../'); -const logger = module.exports = winston.createLogger({ +const logger = winston.createLogger({ transports: [new winston.transports.Console()], format: winston.format.combine( winston.format(function dynamicContent(info, opts) { @@ -14,3 +14,5 @@ const logger = module.exports = winston.createLogger({ }); logger.log('info', 'This is an information message.'); + +module.exports = logger; diff --git a/lib/winston.js b/lib/winston.js index 2357d99e4..80b464de3 100644 --- a/lib/winston.js +++ b/lib/winston.js @@ -9,69 +9,14 @@ const logform = require('logform'); const { warn } = require('./winston/common'); - -/** - * Expose version. Use `require` method for `webpack` support. - * @type {string} - */ -exports.version = require('../package.json').version; -/** - * Include transports defined by default by winston - * @type {Array} - */ -exports.transports = require('./winston/transports'); -/** - * Expose utility methods - * @type {Object} - */ -exports.config = require('./winston/config'); -/** - * Hoist format-related functionality from logform. - * @type {Object} - */ -exports.addColors = logform.levels; -/** - * Hoist format-related functionality from logform. - * @type {Object} - */ -exports.format = logform.format; -/** - * Expose core Logging-related prototypes. - * @type {function} - */ -exports.createLogger = require('./winston/create-logger'); -/** - * Expose core Logging-related prototypes. - * @type {Object} - */ -exports.ExceptionHandler = require('./winston/exception-handler'); -/** - * Expose core Logging-related prototypes. - * @type {Object} - */ -exports.RejectionHandler = require('./winston/rejection-handler'); -/** - * Expose core Logging-related prototypes. - * @type {Container} - */ -exports.Container = require('./winston/container'); -/** - * Expose core Logging-related prototypes. - * @type {Object} - */ -exports.Transport = require('winston-transport'); -/** - * We create and expose a default `Container` to `winston.loggers` so that the - * programmer may manage multiple `winston.Logger` instances without any - * additional overhead. - * @example - * // some-file1.js - * const logger = require('winston').loggers.get('something'); - * - * // some-file2.js - * const logger = require('winston').loggers.get('something'); - */ -exports.loggers = new exports.Container(); +const { version } = require('../package.json'); +const transports = require('./winston/transports'); +const config = require('./winston/config'); +const createLogger = require('./winston/create-logger'); +const ExceptionHandler = require('./winston/exception-handler'); +const RejectionHandler = require('./winston/rejection-handler'); +const Container = require('./winston/container'); +const Transport = require('winston-transport'); /** * We create and expose a 'defaultLogger' so that the programmer may do the @@ -81,10 +26,93 @@ exports.loggers = new exports.Container(); * winston.log('info', 'some message'); * winston.error('some error'); */ -const defaultLogger = exports.createLogger(); +const defaultLogger = createLogger(); + +// Expose all the required modules and variables +const winston = { + // Expose version. Use `require` method for `webpack` support. + version, + // Include transports defined by default by winston + transports, + // Expose utility methods + config, + // Hoist format-related functionality from logform. + addColors: logform.levels, + // Hoist format-related functionality from logform. + format: logform.format, + // Expose core Logging-related prototypes. + createLogger, + // Expose core Logging-related prototypes. + ExceptionHandler, + // Expose core Logging-related prototypes. + RejectionHandler, + // Expose core Logging-related prototypes. + Container, + // Expose core Logging-related prototypes. + Transport, + /** + * We create and expose a default `Container` to `winston.loggers` so that the + * programmer may manage multiple `winston.Logger` instances without any + * additional overhead. + * @example + * // some-file1.js + * const logger = require('winston').loggers.get('something'); + * + * // some-file2.js + * const logger = require('winston').loggers.get('something'); + */ + loggers: new Container(), + + + /** + * Define getter / setter for the default logger level which need to be exposed + * by winston. + * @type {string} + */ + get level() { + return defaultLogger.level; + }, + + set level(val) { + defaultLogger.level = val; + }, + + /** + * Define getter for `exceptions` which replaces `handleExceptions` and + * `unhandleExceptions`. + * @type {Object} + */ + get exceptions() { + return defaultLogger.exceptions; + }, + + /** + * Define getters / setters for appropriate properties of the default logger + * which need to be exposed by winston. + */ + get exitOnError() { + return defaultLogger.exitOnError; + }, + + set exitOnError(val) { + defaultLogger.exitOnError = val; + }, + + /** + * The default transports and exceptionHandlers for the default winston logger. + * @type {Object} + */ + get default() { + return { + exceptionHandlers: defaultLogger.exceptionHandlers, + rejectionHandlers: defaultLogger.rejectionHandlers, + transports: defaultLogger.transports + }; + } +}; // Pass through the target methods onto `winston. -Object.keys(exports.config.npm.levels) +Object.keys(config.npm.levels) .concat([ 'log', 'query', @@ -102,75 +130,23 @@ Object.keys(exports.config.npm.levels) 'child' ]) .forEach( - method => (exports[method] = (...args) => defaultLogger[method](...args)) + method => (winston[method] = (...args) => defaultLogger[method](...args)) ); -/** - * Define getter / setter for the default logger level which need to be exposed - * by winston. - * @type {string} - */ -Object.defineProperty(exports, 'level', { - get() { - return defaultLogger.level; - }, - set(val) { - defaultLogger.level = val; - } -}); - -/** - * Define getter for `exceptions` which replaces `handleExceptions` and - * `unhandleExceptions`. - * @type {Object} - */ -Object.defineProperty(exports, 'exceptions', { - get() { - return defaultLogger.exceptions; - } -}); - -/** - * Define getters / setters for appropriate properties of the default logger - * which need to be exposed by winston. - * @type {Logger} - */ -['exitOnError'].forEach(prop => { - Object.defineProperty(exports, prop, { - get() { - return defaultLogger[prop]; - }, - set(val) { - defaultLogger[prop] = val; - } - }); -}); - -/** - * The default transports and exceptionHandlers for the default winston logger. - * @type {Object} - */ -Object.defineProperty(exports, 'default', { - get() { - return { - exceptionHandlers: defaultLogger.exceptionHandlers, - rejectionHandlers: defaultLogger.rejectionHandlers, - transports: defaultLogger.transports - }; - } -}); // Have friendlier breakage notices for properties that were exposed by default // on winston < 3.0. -warn.deprecated(exports, 'setLevels'); -warn.forFunctions(exports, 'useFormat', ['cli']); -warn.forProperties(exports, 'useFormat', ['padLevels', 'stripColors']); -warn.forFunctions(exports, 'deprecated', [ +warn.deprecated(winston, 'setLevels'); +warn.forFunctions(winston, 'useFormat', ['cli']); +warn.forProperties(winston, 'useFormat', ['padLevels', 'stripColors']); +warn.forFunctions(winston, 'deprecated', [ 'addRewriter', 'addFilter', 'clone', 'extend' ]); -warn.forProperties(exports, 'deprecated', ['emitErrs', 'levelLength']); +warn.forProperties(winston, 'deprecated', ['emitErrs', 'levelLength']); // Throw a useful error when users attempt to run `new winston.Logger`. -warn.moved(exports, 'createLogger', 'Logger'); +warn.moved(winston, 'createLogger', 'Logger'); + +module.exports = winston; diff --git a/lib/winston/config/index.js b/lib/winston/config/index.js index 6eb79de1c..f200a09b4 100644 --- a/lib/winston/config/index.js +++ b/lib/winston/config/index.js @@ -14,22 +14,29 @@ const { configs } = require('triple-beam'); * Export config set for the CLI. * @type {Object} */ -exports.cli = logform.levels(configs.cli); +const cli = logform.levels(configs.cli); /** * Export config set for npm. * @type {Object} */ -exports.npm = logform.levels(configs.npm); +const npm = logform.levels(configs.npm); /** * Export config set for the syslog. * @type {Object} */ -exports.syslog = logform.levels(configs.syslog); +const syslog = logform.levels(configs.syslog); /** * Hoist addColors from logform where it was refactored into in winston@3. * @type {Object} */ -exports.addColors = logform.levels; +const addColors = logform.levels; + +module.exports = { + cli, + npm, + syslog, + addColors +}; diff --git a/lib/winston/container.js b/lib/winston/container.js index 57720306a..b2b72d335 100644 --- a/lib/winston/container.js +++ b/lib/winston/container.js @@ -13,7 +13,7 @@ const createLogger = require('./create-logger'); * Inversion of control container for winston logger instances. * @type {Container} */ -module.exports = class Container { +class Container { /** * Constructor function for the Container object responsible for managing a * set of `winston.Logger` instances based on string ids. @@ -116,3 +116,5 @@ module.exports = class Container { this.loggers.delete(id); } }; + +module.exports = Container; diff --git a/lib/winston/create-logger.js b/lib/winston/create-logger.js index e868aeaad..441bf0793 100644 --- a/lib/winston/create-logger.js +++ b/lib/winston/create-logger.js @@ -22,7 +22,7 @@ function isLevelEnabledFunctionName(level) { * @param {!Object} opts - Options for the created logger. * @returns {Logger} - A newly created logger instance. */ -module.exports = function (opts = {}) { +function logger(opts = {}) { // // Default levels: npm // @@ -102,3 +102,5 @@ module.exports = function (opts = {}) { return logger; }; + +module.exports = logger; diff --git a/lib/winston/exception-stream.js b/lib/winston/exception-stream.js index 477eba0a7..7c2fc184d 100644 --- a/lib/winston/exception-stream.js +++ b/lib/winston/exception-stream.js @@ -12,9 +12,8 @@ const { Writable } = require('readable-stream'); /** * TODO: add class description. * @type {ExceptionStream} - * @extends {Writable} */ -module.exports = class ExceptionStream extends Writable { +class ExceptionStream extends Writable { /** * Constructor function for the ExceptionStream responsible for wrapping a * TransportStream; only allowing writes of `info` objects with @@ -52,3 +51,5 @@ module.exports = class ExceptionStream extends Writable { return true; } }; + +module.exports = ExceptionStream; diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index edcc5a65a..bffa681e5 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -12,7 +12,7 @@ * @type {Profiler} * @private */ -module.exports = class Profiler { +class Profiler { /** * Constructor function for the Profiler instance used by * `Logger.prototype.startTimer`. When done is called the timer will finish @@ -49,3 +49,5 @@ module.exports = class Profiler { return this.logger.write(info); } }; + +module.exports = Profiler; diff --git a/lib/winston/rejection-handler.js b/lib/winston/rejection-handler.js index d287f5185..8b9d04cd3 100644 --- a/lib/winston/rejection-handler.js +++ b/lib/winston/rejection-handler.js @@ -18,7 +18,7 @@ const ExceptionStream = require('./exception-stream'); * Object for handling unhandledRejection events. * @type {RejectionHandler} */ -module.exports = class RejectionHandler { +class RejectionHandler { /** * TODO: add contructor description * @param {!Logger} logger - TODO: add param description @@ -249,3 +249,5 @@ module.exports = class RejectionHandler { }); } }; + +module.exports = RejectionHandler; diff --git a/lib/winston/tail-file.js b/lib/winston/tail-file.js index 86ea904cd..8db4ba54a 100644 --- a/lib/winston/tail-file.js +++ b/lib/winston/tail-file.js @@ -24,7 +24,7 @@ function noop() {} * `tail -f` a file. Options must include file. * @returns {mixed} - TODO: add return description. */ -module.exports = (options, iter) => { +const tailFile = (options, iter) => { const buffer = Buffer.alloc(64 * 1024); const decode = new StringDecoder('utf8'); const stream = new Stream(); @@ -122,3 +122,5 @@ module.exports = (options, iter) => { return stream.destroy; }; + +module.exports = tailFile; diff --git a/lib/winston/transports/console.js b/lib/winston/transports/console.js index b54f3da9c..2c26aac2b 100644 --- a/lib/winston/transports/console.js +++ b/lib/winston/transports/console.js @@ -17,7 +17,7 @@ const TransportStream = require('winston-transport'); * @type {Console} * @extends {TransportStream} */ -module.exports = class Console extends TransportStream { +class Console extends TransportStream { /** * Constructor function for the Console transport object responsible for * persisting log messages and metadata to a terminal or TTY. @@ -115,3 +115,5 @@ module.exports = class Console extends TransportStream { }, {}); } }; + +module.exports = Console; diff --git a/lib/winston/transports/file.js b/lib/winston/transports/file.js index b5801038b..c5a11027c 100644 --- a/lib/winston/transports/file.js +++ b/lib/winston/transports/file.js @@ -24,7 +24,7 @@ const tailFile = require('../tail-file'); * @type {File} * @extends {TransportStream} */ -module.exports = class File extends TransportStream { +class File extends TransportStream { /** * Constructor function for the File transport object responsible for * persisting log messages and metadata to one or more files. @@ -730,3 +730,5 @@ module.exports = class File extends TransportStream { /* eslint-enable no-sync */ } }; + +module.exports = File; diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index 0653d78bc..988aa5336 100644 --- a/lib/winston/transports/http.js +++ b/lib/winston/transports/http.js @@ -18,7 +18,7 @@ const jsonStringify = require('safe-stable-stringify'); * @type {Stream} * @extends {TransportStream} */ -module.exports = class Http extends TransportStream { +class Http extends TransportStream { /** * Constructor function for the Http transport object responsible for * persisting log messages and metadata to a terminal or TTY. @@ -256,3 +256,5 @@ module.exports = class Http extends TransportStream { req.end(Buffer.from(jsonStringify(options, this.options.replacer), 'utf8')); } }; + +module.exports = Http; diff --git a/lib/winston/transports/stream.js b/lib/winston/transports/stream.js index 8027f6402..0218c456f 100644 --- a/lib/winston/transports/stream.js +++ b/lib/winston/transports/stream.js @@ -17,7 +17,7 @@ const TransportStream = require('winston-transport'); * @type {Stream} * @extends {TransportStream} */ -module.exports = class Stream extends TransportStream { +class Stream extends TransportStream { /** * Constructor function for the Console transport object responsible for * persisting log messages and metadata to a terminal or TTY. @@ -61,3 +61,5 @@ module.exports = class Stream extends TransportStream { return; } }; + +module.exports = Stream; diff --git a/test/helpers/mocks/legacy-mixed-transport.js b/test/helpers/mocks/legacy-mixed-transport.js index 38530c785..fafb75029 100644 --- a/test/helpers/mocks/legacy-mixed-transport.js +++ b/test/helpers/mocks/legacy-mixed-transport.js @@ -12,14 +12,14 @@ const Transport = require('../../../').Transport; // and conforming to the old winston transport API, **BUT** INHERITS FROM // THE MODERN WINSTON TRANSPORT. // -module.exports = class LegacyMixed extends Transport { +class LegacyMixed extends Transport { constructor(options = {}) { super(options); // // Expose the name of this Transport on the prototype // - module.exports.prototype.name = 'legacy-mixed-test'; + this.name = 'legacy-mixed-test'; this.silent = options.silent; this.output = { error: [], write: [] }; @@ -50,3 +50,5 @@ module.exports = class LegacyMixed extends Transport { callback(null, true); } }; + +module.exports = LegacyMixed; diff --git a/test/helpers/mocks/legacy-transport.js b/test/helpers/mocks/legacy-transport.js index 16fff743c..c75a26619 100644 --- a/test/helpers/mocks/legacy-transport.js +++ b/test/helpers/mocks/legacy-transport.js @@ -11,7 +11,7 @@ const Transport = require('winston-compat').Transport; // for persisting log messages and metadata to a memory array of messages // and conforming to the old winston transport API. // -const Legacy = module.exports = function Legacy(options) { +const Legacy = function Legacy(options) { options = options || {}; Transport.call(this, options); @@ -53,3 +53,5 @@ Legacy.prototype.log = function (level, msg, meta, callback) { this.emit('logged'); callback(null, true); }; + +module.exports = Legacy;