Skip to content

Commit

Permalink
Revert for 2103 (#2104)
Browse files Browse the repository at this point in the history
* Revert "fix(metadata precedence) - fix metadata override issues (#1989)"

This reverts commit ba93eae.

* Update changelog

* Update version number
  • Loading branch information
wbt committed Apr 4, 2022
1 parent 68f595b commit 7937f33
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 708 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"extends": "@dabh/eslint-config-populist",
"rules": {
"one-var": ["error", { "var": "never", "let": "never", "const": "never" }],
"semi": "error",
"strict": 0
},
"parserOptions": {
Expand Down
2 changes: 1 addition & 1 deletion .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.08
functions: 73.21
statements: 70.54
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## v3.7.2 / 2022-04-04
This change reverts what should have been the feature-level update in 3.7.0 due to issue #2103 showing this to be breaking, unintentionally.

## v3.7.1 / 2022-04-04
This change includes some minor updates to package-lock.json resolving npm audit failures: one in [ansi-regex](https://github.com/advisories/GHSA-93q8-gq69-wqmw) and another in [minimist](https://github.com/advisories/GHSA-xvch-5gv4-984h).

Expand Down Expand Up @@ -644,4 +647,3 @@ function filter (level, msg, meta, inst) {
* Multi-line values for conditional returns are not allowed
* Added acceptance of `stringify` option
* Fixed padding for log levels

37 changes: 24 additions & 13 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

const { Stream, Transform } = require('readable-stream');
const asyncForEach = require('async/forEach');
const { LEVEL, SPLAT, MESSAGE } = require('triple-beam');
const { LEVEL, SPLAT } = require('triple-beam');
const isStream = require('is-stream');
const ExceptionHandler = require('./exception-handler');
const RejectionHandler = require('./rejection-handler');
const LegacyTransportStream = require('winston-transport/legacy');
const Profiler = require('./profiler');
const { warn } = require('./common');
const config = require('./config');
const jsonStringify = require('safe-stable-stringify');

/**
* Captures the number of format (i.e. %s strings) in a given string.
Expand All @@ -43,12 +42,30 @@ class Logger extends Transform {
this.configure(options);
}

child(childMetadata) {
child(defaultRequestMetadata) {
const logger = this;
const clonedParentMetadata = JSON.parse(jsonStringify(this.defaultMeta));
return Object.create(logger, {
defaultMeta: {
value: Object.assign({}, clonedParentMetadata, childMetadata)
write: {
value: function (info) {
const infoClone = Object.assign(
{},
defaultRequestMetadata,
info
);

// Object.assign doesn't copy inherited Error
// properties so we have to do that explicitly
//
// Remark (indexzero): we should remove this
// since the errors format will handle this case.
//
if (info instanceof Error) {
infoClone.stack = info.stack;
infoClone.message = info.message;
}

logger.write(infoClone);
}
}
});
}
Expand Down Expand Up @@ -271,10 +288,6 @@ class Logger extends Transform {
info[LEVEL] = info.level;
}

if (!info[MESSAGE]) {
info[MESSAGE] = info.message;
}

// Remark: really not sure what to do here, but this has been reported as
// very confusing by pre winston@2.0.0 users as quite confusing when using
// custom levels.
Expand Down Expand Up @@ -634,9 +647,7 @@ class Logger extends Transform {

_addDefaultMeta(msg) {
if (this.defaultMeta) {
// The msg must be cloned as it is being mutated, but any metadata provided with the msg takes precedence over default
const msgClone = JSON.parse(jsonStringify(msg));
Object.assign(msg, this.defaultMeta, msgClone);
Object.assign(msg, this.defaultMeta);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/winston/profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ module.exports = class Profiler {
* @private
*/
constructor(logger) {
// TODO there is no restriction on what the Profiler considers a Logger. As such there is no guarantees it adheres
// to the proper interface. This needs to hardened.
if (!logger) {
throw new Error('Logger is required for profiling.');
}
Expand All @@ -34,8 +32,7 @@ module.exports = class Profiler {
/**
* Ends the current timer (i.e. Profiler) instance and logs the `msg` along
* with the duration since creation.
* @returns {boolean} - `false` if the logger stream wishes for the calling code to wait for the 'drain' event to be
* emitted before continuing to write additional data; otherwise `true`
* @returns {mixed} - TODO: add return description.
* @private
*/
done(...args) {
Expand All @@ -48,7 +45,7 @@ module.exports = class Profiler {
const info = typeof args[args.length - 1] === 'object' ? args.pop() : {};
info.level = info.level || 'info';
info.durationMs = (Date.now()) - this.start;
if (this.logger._addDefaultMeta) this.logger._addDefaultMeta(info);

return this.logger.write(info);
}
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "winston",
"description": "A logger for just about everything.",
"version": "3.7.1",
"version": "3.7.2",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"maintainers": [
"David Hyde <dabh@alumni.stanford.edu>"
Expand Down Expand Up @@ -60,7 +60,6 @@
"types": "./index.d.ts",
"scripts": {
"lint": "eslint lib/*.js lib/winston/*.js lib/winston/**/*.js --resolve-plugins-relative-to ./node_modules/@dabh/eslint-config-populist",
"lint:fix": "npm run lint -- --fix",
"test": "mocha",
"test:coverage": "nyc npm run test:unit",
"test:unit": "mocha test/unit",
Expand Down
22 changes: 2 additions & 20 deletions test/helpers/mocks/mock-transport.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const stream = require('stream')
const winston = require('../../../lib/winston');
const {Writable} = require("stream");

/**
* Returns a new Winston transport instance which will invoke
* the `write` method on each call to `.log`
* the `write` method on each call to `.log`
*
* @param {function} write Write function for the specified stream
* @returns {StreamTransportInstance} A transport instance
Expand All @@ -18,23 +17,6 @@ function createMockTransport(write) {
return new winston.transports.Stream({ stream: writeable })
}

/**
* Returns a valid Winston transport that writes to the passed array object
* @param array Array to be used to store the "written" chunks
* @returns {winston.transports.Stream}
*/
function inMemory(array, options = {}) {
const memoryStream = new Writable({
objectMode: true,
write: (chunk, encoding, next) => {
array.push(chunk);
next()
}
});
return new winston.transports.Stream({stream: memoryStream, ...options})
}

module.exports = {
createMockTransport,
inMemory
createMockTransport
};
Loading

0 comments on commit 7937f33

Please sign in to comment.