Skip to content

Commit

Permalink
Merge branch 'winstonjs:master' into fix-winstonjs#2091
Browse files Browse the repository at this point in the history
  • Loading branch information
debadutta98 authored Jul 31, 2023
2 parents f60d4a5 + 23cb80c commit 7267edb
Show file tree
Hide file tree
Showing 16 changed files with 2,243 additions and 1,693 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
strategy:
matrix:
node:
- 12
- 14
- 16
- 18
- 20
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
4 changes: 2 additions & 2 deletions .nycrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ reporter:
check-coverage: true
branches: 61.51
lines: 70.85
functions: 73.21
statements: 70.54
functions: 70.00
statements: 70.54
5 changes: 3 additions & 2 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 Expand Up @@ -764,11 +765,11 @@ const logger = winston.createLogger({
level: 'error',
format: winston.format.json()
}),
new transports.Http({
new winston.transports.Http({
level: 'warn',
format: winston.format.json()
}),
new transports.Console({
new winston.transports.Console({
level: 'info',
format: winston.format.combine(
winston.format.colorize(),
Expand Down
1 change: 1 addition & 0 deletions docs/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ looking for daily log rotation see [DailyRotateFile](#dailyrotatefile-transport)
* __level:__ Level of messages that this transport should log (default: level set on parent logger).
* __silent:__ Boolean flag indicating whether to suppress output (default false).
* __eol:__ Line-ending character to use. (default: `os.EOL`).
* __lazy:__ If true, log files will be created on demand, not at the initialization time.
* __filename:__ The filename of the logfile to write output to.
* __maxsize:__ Max size in bytes of the logfile, if the size is exceeded then a new file is created, a counter will become a suffix of the log file.
* __maxFiles:__ Limit the number of files created when the size of the logfile is exceeded.
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?: LogEntry): 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
43 changes: 37 additions & 6 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = class File extends TransportStream {
this.maxFiles = options.maxFiles || null;
this.eol = (typeof options.eol === 'string') ? options.eol : os.EOL;
this.tailable = options.tailable || false;
this.lazy = options.lazy || false;

// Internal state variables representing the number of files this instance
// has created and the current size (in bytes) of the current logfile.
Expand All @@ -88,9 +89,10 @@ module.exports = class File extends TransportStream {
this._drain = false;
this._opening = false;
this._ending = false;
this._fileExist = false;

if (this.dirname) this._createLogDirIfNotExist(this.dirname);
this.open();
if (!this.lazy) this.open();
}

finishIfEnding() {
Expand All @@ -107,14 +109,13 @@ module.exports = class File extends TransportStream {
}
}


/**
* Core logging method exposed to Winston. Metadata is optional.
* @param {Object} info - TODO: add param description.
* @param {Function} callback - TODO: add param description.
* @returns {undefined}
*/
log(info, callback = () => {}) {
log(info, callback = () => { }) {
// Remark: (jcrugzz) What is necessary about this callback(null, true) now
// when thinking about 3.x? Should silent be handled in the base
// TransportStream _write method?
Expand All @@ -123,6 +124,7 @@ module.exports = class File extends TransportStream {
return true;
}


// Output stream buffer is full and has asked us to wait for the drain event
if (this._drain) {
this._stream.once('drain', () => {
Expand All @@ -138,6 +140,32 @@ module.exports = class File extends TransportStream {
});
return;
}
if (this.lazy) {
if (!this._fileExist) {
if (!this._opening) {
this.open();
}
this.once('open', () => {
this._fileExist = true;
this.log(info, callback);
return;
});
return;
}
if (this._needsNewFile(this._pendingSize)) {
this._dest.once('close', () => {
if (!this._opening) {
this.open();
}
this.once('open', () => {
this.log(info, callback);
return;
});
return;
});
return;
}
}

// Grab the raw string and append the expected EOL.
const output = `${info[MESSAGE]}${this.eol}`;
Expand Down Expand Up @@ -169,6 +197,10 @@ module.exports = class File extends TransportStream {
if (!this._needsNewFile()) {
return;
}
if (this.lazy) {
this._endStream(() => {this.emit('fileclosed')});
return;
}

// End the current stream, ensure it flushes and create a new one.
// This could potentially be optimized to not run a stat call but its
Expand Down Expand Up @@ -508,7 +540,6 @@ module.exports = class File extends TransportStream {
_cleanupStream(stream) {
stream.removeListener('error', this._onError);
stream.destroy();

return stream;
}

Expand All @@ -526,7 +557,7 @@ module.exports = class File extends TransportStream {
* @param {function} callback - Callback for when the current file has closed.
* @private
*/
_endStream(callback = () => {}) {
_endStream(callback = () => { }) {
if (this._dest) {
this._stream.unpipe(this._dest);
this._dest.end(() => {
Expand All @@ -542,7 +573,7 @@ module.exports = class File extends TransportStream {
* Returns the WritableStream for the active file on this instance. If we
* should gzip the file then a zlib stream is returned.
*
* @param {ReadableStream} source – PassThrough to pipe to the file when open.
* @param {ReadableStream} source –PassThrough to pipe to the file when open.
* @returns {WritableStream} Stream that writes to disk for the active file.
*/
_createStream(source) {
Expand Down
2 changes: 2 additions & 0 deletions lib/winston/transports/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare namespace winston {
maxFiles?: number;
eol?: string;
tailable?: boolean;
lazy?: boolean;
}

interface FileTransportInstance extends Transport {
Expand All @@ -47,6 +48,7 @@ declare namespace winston {
maxFiles: number | null;
eol: string;
tailable: boolean;
lazy: boolean;

new(options?: FileTransportOptions): FileTransportInstance;
}
Expand Down
Loading

0 comments on commit 7267edb

Please sign in to comment.