Skip to content

Commit

Permalink
Export Logger class (#2181)
Browse files Browse the repository at this point in the history
* export Logger class

* Add semicolon and a couple of tests

* Update index.d.ts

* Update README.md

* refactor: 🔥 dead code

* revert: not so dead code
  • Loading branch information
Faithfinder committed Jul 10, 2023
1 parent f7e7f2f commit eda40ef
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -153,11 +153,11 @@ declare namespace winston {
stream(options?: any): NodeJS.ReadableStream;

startTimer(): Profiler;
profile(id: string | number, meta?: Record<string, any>): Logger;
profile(id: string | number, meta?: Record<string, any>): this;

configure(options: LoggerOptions): void;

child(options: Object): Logger;
child(options: Object): this;

isLevelEnabled(level: string): boolean;
isErrorEnabled(): boolean;
Expand Down
8 changes: 6 additions & 2 deletions lib/winston.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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');

15 changes: 0 additions & 15 deletions lib/winston/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions test/integration/logger.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit eda40ef

Please sign in to comment.