util: move util.log to eol

PR-URL: https://github.com/nodejs/node/pull/52744
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
marco-ippolito 2024-04-29 11:38:00 +02:00 committed by Node.js GitHub Bot
parent 1d817dcb52
commit 982f6ad516
5 changed files with 5 additions and 141 deletions

View File

@ -1376,6 +1376,9 @@ The `util.isUndefined()` API has been removed. Please use
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/52744
description: End-of-Life deprecation.
- version: v22.0.0
pr-url: https://github.com/nodejs/node/pull/50488
description: Runtime deprecation.
@ -1387,9 +1390,9 @@ changes:
description: Documentation-only deprecation.
-->
Type: Runtime
Type: End-of-Life
The [`util.log()`][] API has been deprecated because it's an unmaintained
The `util.log()` API has been removed because it's an unmaintained
legacy API that was exposed to user land by accident. Instead,
consider the following alternatives based on your specific needs:

View File

@ -2950,26 +2950,6 @@ util.isArray({});
// Returns: false
```
### `util.log(string)`
<!-- YAML
added: v0.3.0
deprecated: v6.0.0
-->
> Stability: 0 - Deprecated: Use a third party module instead.
* `string` {string}
The `util.log()` method prints the given `string` to `stdout` with an included
timestamp.
```js
const util = require('node:util');
util.log('Timestamped message.');
```
[Common System Errors]: errors.md#common-system-errors
[Custom inspection functions on objects]: #custom-inspection-functions-on-objects
[Custom promisified functions]: #custom-promisified-functions

View File

@ -23,14 +23,7 @@
const {
ArrayIsArray,
ArrayPrototypeJoin,
ArrayPrototypePop,
Date,
DatePrototypeGetDate,
DatePrototypeGetHours,
DatePrototypeGetMinutes,
DatePrototypeGetMonth,
DatePrototypeGetSeconds,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
@ -42,7 +35,6 @@ const {
ObjectSetPrototypeOf,
ObjectValues,
ReflectApply,
StringPrototypePadStart,
StringPrototypeToWellFormed,
} = primordials;
@ -89,14 +81,6 @@ function lazyAbortController() {
let internalDeepEqual;
/**
* @param {number} n
* @returns {string}
*/
function pad(n) {
return StringPrototypePadStart(n.toString(), 2, '0');
}
/**
* @param {string} code
* @returns {string}
@ -134,35 +118,6 @@ function styleText(format, text) {
return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`;
}
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
/**
* @returns {string} 26 Feb 16:19:34
*/
function timestamp() {
const d = new Date();
const t = ArrayPrototypeJoin([
pad(DatePrototypeGetHours(d)),
pad(DatePrototypeGetMinutes(d)),
pad(DatePrototypeGetSeconds(d)),
], ':');
return `${DatePrototypeGetDate(d)} ${months[DatePrototypeGetMonth(d)]} ${t}`;
}
let console;
/**
* Log is just a thin wrapper to console.log that prepends a timestamp
* @deprecated since v6.0.0
* @type {(...args: any[]) => void}
*/
function log(...args) {
if (!console) {
console = require('internal/console/global');
}
console.log('%s - %s', timestamp(), format(...args));
}
/**
* Inherit the prototype methods from one constructor into another.
*
@ -325,10 +280,6 @@ module.exports = {
}
return internalDeepEqual(a, b);
},
log: deprecate(log,
'The `util.log API is deprecated. ' +
'Please use console.log() with a custom formatter or a third-party logger instead.',
'DEP0059'),
promisify,
stripVTControlCharacters,
toUSVString(input) {

View File

@ -21,7 +21,6 @@
'use strict';
require('../common');
const util = require('util');
const fs = require('fs');
let data = [
@ -50,5 +49,3 @@ tmpdir.refresh();
const buf = Buffer.from(data, 'base64');
fs.writeFileSync(tmpdir.resolve('test.jpg'), buf);
util.log('Done!');

View File

@ -1,67 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const common = require('../common');
const {
hijackStdout,
hijackStderr,
restoreStdout,
restoreStderr,
} = require('../common/hijackstdio');
const assert = require('assert');
const util = require('util');
assert.ok(process.stdout.writable);
assert.ok(process.stderr.writable);
const strings = [];
hijackStdout(function(data) {
strings.push(data);
});
hijackStderr(common.mustNotCall('stderr.write must not be called'));
const tests = [
{ input: 'foo', output: 'foo' },
{ input: undefined, output: 'undefined' },
{ input: null, output: 'null' },
{ input: false, output: 'false' },
{ input: 42, output: '42' },
{ input: function() {}, output: '[Function: input]' },
{ input: parseInt('not a number', 10), output: 'NaN' },
{ input: { answer: 42 }, output: '{ answer: 42 }' },
{ input: [1, 2, 3], output: '[ 1, 2, 3 ]' },
];
// test util.log()
const re = /[0-9]{1,2} [A-Z][a-z]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} - (.+)$/;
for (const test of tests) {
util.log(test.input);
const result = strings.shift().trim();
const match = re.exec(result);
assert.ok(match);
assert.strictEqual(match[1], test.output);
}
assert.strictEqual(process.stdout.writeTimes, tests.length);
restoreStdout();
restoreStderr();