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

Export Logger class #2181

Merged
merged 8 commits into from
Jul 10, 2023
Merged
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
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);
});
});