-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Adding log level methods when using Logger class #2351
base: master
Are you sure you want to change the base?
Adding log level methods when using Logger class #2351
Conversation
@danilobatistaqueiroz Do you think #2349 would also solve for adding the methods like you're trying to do here with your PR? Wondering if we can just merge that one or if we really need both of these |
@DABH , after compiling the typescript snippet example we have: "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var winston_1 = require("winston");
var logger = new winston_1.Logger({
level: 'info',
transports: [new winston_1.transports.Console()],
format: winston_1.format.combine(winston_1.format.splat(), winston_1.format.colorize({ all: true }), winston_1.format.simple())
});
logger.warn('Hello world');
logger.info('Hello world');
exports.default = logger; Running using my commits, it will produce:
Running using the commits in #2349 it will produce:
|
@DABH , But when defined in When using the js version: const mylogger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [new winston.transports.Console()],
}); The prototype function is defined for the object mylogger and it works fine. const mylogger = new Logger({
level: 'info',
transports: [new transports.Console()],
format: format.combine(format.splat(), format.colorize({ all: true }), format.simple())
}) It doesn't call Obs: the anonymous function in A very simple solution could be calling the anonymous from i.e: const createLogger = require('./create-logger');
...
class Logger ...
constructor(options) {
super({ objectMode: true });
this.configure(options);
--> createLogger(options); //calling createLogger here doesn't work
} |
Moved the code for creation log level methods from
create-logger.js
tologger.js
.Encapsulated it in a new method named
createLogLevelMethods
.I only changed the code for creation of log level methods in 3 places:
From
DerivedLogger.prototype[level]
toLogger.prototype[level]
Instead of calling
Object.keys(opts.levels)
, now it callsObject.keys(levels)
because I passed opts.levels as parameter named levels.Removed the function
isLevelEnabledFunctionName
, because it's used only in one place, I created a constant instead.Added test for log level methods using
new Logger()
Now inside
create-logger.js
it just calls:logger.createLogLevelMethods(logger, opts.levels);
Now it works when using the typescript version.
Related Issue:
Fixes: #2348
Suggestion:
Create a test suit using Typescript
Difference between the old code from the new one. (old on left, new on right) (old in create-logger.js, new in logger.js)