I'm attempting to use winston for logging in my project. To create a new logger, you would typically do:
import winston from 'winston';
const logger = winston.createLogger({ ...opts });
However, my options will be largely the same across the project, save for a label
option, which will be the name of the module doing the logging. Rather than duplicate the createLogger
code across 100 files, I'd like to create a wrapper class that provides the most common options to createLogger
, and allows the user to provide one param for label
to the constructor. The instance of the class should be an instance of winston's Logger
class, which is the return value of createLogger
.
So to summarize, I'd like to be able to do this:
import OurLogger from './our-logger';
const fooModuleLogger = new OurLogger('foo'); // full winston Logger instance
My best shot right now at OurLogger.js
would look something like:
import winston from 'winston';
export default class {
constructor(label = 'defaultLabel') {
const defaultOpts = { ... }
// I know this part is wrong. But what's the right way?
this = winston.createLogger({
label,
...defaultOpts
});
}
}
You can't assign this
and without making wrapper methods for every bit of functionality a class doesn't feel like the right thing here.
Why not just export a function and use that?
export default function OurLogger(label){
const defaultOpts = { ... }
return winston.createLogger({
label,
...defaultOpts
});
}
And then just
import OurLogger from './our-logger';
const fooModuleLogger = OurLogger('foo'); // full winston Logger instance