Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set export last in the the files #2307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//
Expand All @@ -749,6 +749,8 @@ module.exports = class YourCustomTransport extends Transport {
callback();
}
};

module.exports = YourCustomTransport;
```

## Common Transport options
Expand Down
4 changes: 3 additions & 1 deletion examples/color-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand All @@ -11,3 +11,5 @@ const logger = module.exports = winston.createLogger({
});

logger.log('info', 'This is an information message.');

module.exports = logger;
4 changes: 3 additions & 1 deletion examples/custom-levels-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -43,3 +43,5 @@ const logger = module.exports = winston.createLogger({
});

logger.custom('hello')

module.exports = logger;
4 changes: 3 additions & 1 deletion examples/format-dynamic-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -14,3 +14,5 @@ const logger = module.exports = winston.createLogger({
});

logger.log('info', 'This is an information message.');

module.exports = logger;
228 changes: 102 additions & 126 deletions lib/winston.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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;
15 changes: 11 additions & 4 deletions lib/winston/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 3 additions & 1 deletion lib/winston/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -116,3 +116,5 @@ module.exports = class Container {
this.loggers.delete(id);
}
};

module.exports = Container;
4 changes: 3 additions & 1 deletion lib/winston/create-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -102,3 +102,5 @@ module.exports = function (opts = {}) {

return logger;
};

module.exports = logger;
Loading