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

Remove deprecated API and options in v7 option. #1249

Merged
merged 7 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
os: [macOS-latest, windows-latest, ubuntu-latest]
node-version: [12, 14, 16, 17]
node-version: [14, 16, 17]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
21 changes: 0 additions & 21 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,27 +353,6 @@ logger.info(thing)
In this way, logged objects' properties don't conflict with pino's standard logging properties,
and searching for logged objects can start from a consistent path.

<a id=prettyPrint></a>
#### `prettyPrint` (Boolean | Object)

Default: `false`

__DEPRECATED: look at [pino-pretty documentation](https://github.com/pinojs/pino-pretty)
for alternatives__. Using a [`transport`](#transport) is also an option.__

Enables pretty printing log logs. This is intended for non-production
configurations. This may be set to a configuration object as outlined in the
[`pino-pretty` documentation](https://github.com/pinojs/pino-pretty).

The options object may additionally contain a `prettifier` property to define
which prettifier module to use. When not present, `prettifier` defaults to
`'pino-pretty'`. Regardless of the value, the specified prettifier module
must be installed as a separate dependency:

```sh
npm install pino-pretty
```

#### `browser` (Object)

Browser only, may have `asObject` and `write` keys. This option is separately
Expand Down
6 changes: 2 additions & 4 deletions lib/deprecations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const warning = require('fastify-warning')()
module.exports = warning

const warnName = 'PinoWarning'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this file required anywhere else now? if not, remove?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll definitely need it at some point in the future, better to keep.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me it's a loose end - maybe this whole concept should be a separate module or something

// const warnName = 'PinoWarning'

warning.create(warnName, 'PINODEP008', 'prettyPrint is deprecated, look at https://github.com/pinojs/pino-pretty for alternatives.')

warning.create(warnName, 'PINODEP009', 'The use of pino.final is discouraged in Node.js v14+ and not required. It will be removed in the next major version')
// warning.create(warnName, 'PINODEP010', 'A new deprecation')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on purpose to avoid deleting this file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the file needed? if not we should remove it - if we ever need it again it's in the commit history right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easier to keep it and update it as necessary.

2 changes: 0 additions & 2 deletions lib/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const mixinSym = Symbol('pino.mixin')

const lsCacheSym = Symbol('pino.lsCache')
const chindingsSym = Symbol('pino.chindings')
const parsedChindingsSym = Symbol('pino.parsedChindings')

const asJsonSym = Symbol('pino.asJson')
const writeSym = Symbol('pino.write')
Expand Down Expand Up @@ -44,7 +43,6 @@ module.exports = {
mixinSym,
lsCacheSym,
chindingsSym,
parsedChindingsSym,
asJsonSym,
writeSym,
serializersSym,
Expand Down
232 changes: 13 additions & 219 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
const format = require('quick-format-unescaped')
const { mapHttpRequest, mapHttpResponse } = require('pino-std-serializers')
const SonicBoom = require('sonic-boom')
const warning = require('./deprecations')
const onExit = require('on-exit-leak-free')
const {
lsCacheSym,
chindingsSym,
parsedChindingsSym,
writeSym,
serializersSym,
formatOptsSym,
Expand All @@ -18,9 +17,6 @@ const {
stringifySym,
stringifySafeSym,
wildcardFirstSym,
needsMetadataGsym,
redactFmtSym,
streamSym,
nestedKeySym,
formattersSym,
messageKeySym,
Expand Down Expand Up @@ -211,123 +207,6 @@ function asChindings (instance, bindings) {
return data
}

function getPrettyStream (opts, prettifier, dest, instance) {
if (prettifier && typeof prettifier === 'function') {
prettifier = prettifier.bind(instance)
return prettifierMetaWrapper(prettifier(opts), dest, opts)
}
try {
const prettyFactory = require('pino-pretty').prettyFactory
prettyFactory.asMetaWrapper = prettifierMetaWrapper
return prettifierMetaWrapper(prettyFactory(opts), dest, opts)
} catch (e) {
if (e.message.startsWith("Cannot find module 'pino-pretty'")) {
throw Error('Missing `pino-pretty` module: `pino-pretty` must be installed separately')
};
throw e
}
}

function prettifierMetaWrapper (pretty, dest, opts) {
opts = Object.assign({ suppressFlushSyncWarning: false }, opts)
let warned = false
return {
[needsMetadataGsym]: true,
lastLevel: 0,
lastMsg: null,
lastObj: null,
lastLogger: null,
flushSync () {
if (opts.suppressFlushSyncWarning || warned) {
return
}
warned = true
setMetadataProps(dest, this)
dest.write(pretty(Object.assign({
level: 40, // warn
msg: 'pino.final with prettyPrint does not support flushing',
time: Date.now()
}, this.chindings())))
},
chindings () {
const lastLogger = this.lastLogger
let chindings = null

// protection against flushSync being called before logging
// anything
if (!lastLogger) {
return null
}

if (lastLogger.hasOwnProperty(parsedChindingsSym)) {
chindings = lastLogger[parsedChindingsSym]
} else {
chindings = JSON.parse('{' + lastLogger[chindingsSym].substr(1) + '}')
lastLogger[parsedChindingsSym] = chindings
}

return chindings
},
write (chunk) {
const lastLogger = this.lastLogger
const chindings = this.chindings()

let time = this.lastTime

/* istanbul ignore next */
if (typeof time === 'number') {
// do nothing!
} else if (time.match(/^\d+/)) {
time = parseInt(time)
} else {
time = time.slice(1, -1)
}

const lastObj = this.lastObj
const lastMsg = this.lastMsg
const errorProps = null

const formatters = lastLogger[formattersSym]
const formattedObj = formatters.log ? formatters.log(lastObj) : lastObj

const messageKey = lastLogger[messageKeySym]
if (lastMsg && formattedObj && !formattedObj.hasOwnProperty(messageKey)) {
formattedObj[messageKey] = lastMsg
}

const obj = Object.assign({
level: this.lastLevel,
time
}, formattedObj, errorProps)

const serializers = lastLogger[serializersSym]
const keys = Object.keys(serializers)

for (var i = 0; i < keys.length; i++) {
const key = keys[i]
if (obj[key] !== undefined) {
obj[key] = serializers[key](obj[key])
}
}

for (const key in chindings) {
if (!obj.hasOwnProperty(key)) {
obj[key] = chindings[key]
}
}

const stringifiers = lastLogger[stringifiersSym]
const redact = stringifiers[redactFmtSym]

const formatted = pretty(typeof redact === 'function' ? redact(obj) : obj)
if (formatted === undefined) return

setMetadataProps(dest, this)
dest.write(formatted)
}
}
}

function hasBeenTampered (stream) {
return stream.write !== stream.constructor.prototype.write
}
Expand All @@ -337,12 +216,17 @@ function buildSafeSonicBoom (opts) {
stream.on('error', filterBrokenPipe)
// if we are sync: false, we must flush on exit
if (!opts.sync && isMainThread) {
setupOnExit(stream)
onExit.register(stream, autoEnd)

stream.on('close', function () {
onExit.unregister(stream)
})
}
return stream

function filterBrokenPipe (err) {
// TODO verify on Windows
// Impossible to replicate across all operating systems
/* istanbul ignore next */
if (err.code === 'EPIPE') {
// If we get EPIPE, we should stop logging here
// however we have no control to the consumer of
Expand All @@ -358,20 +242,6 @@ function buildSafeSonicBoom (opts) {
}
}

function setupOnExit (stream) {
/* istanbul ignore next */
if (global.WeakRef && global.WeakMap && global.FinalizationRegistry) {
// This is leak free, it does not leave event handlers
const onExit = require('on-exit-leak-free')

onExit.register(stream, autoEnd)

stream.on('close', function () {
onExit.unregister(stream)
})
}
}

function autoEnd (stream, eventName) {
// This check is needed only on some platforms
/* istanbul ignore next */
Expand All @@ -386,6 +256,8 @@ function autoEnd (stream, eventName) {
stream.end()
})
} else {
// For some reason istanbul is not detecting this, but it's there
/* istanbul ignore next */
mcollina marked this conversation as resolved.
Show resolved Hide resolved
// We do not have an event loop, so flush synchronously
stream.flushSync()
}
Expand All @@ -410,85 +282,19 @@ function createArgsNormalizer (defaultOptions) {
}
opts = Object.assign({}, defaultOptions, opts)
opts.serializers = Object.assign({}, defaultOptions.serializers, opts.serializers)
if ('onTerminated' in opts) {
throw Error('The onTerminated option has been removed, use pino.final instead')
}
if ('changeLevelName' in opts) {
process.emitWarning(
'The changeLevelName option is deprecated and will be removed in v7. Use levelKey instead.',
{ code: 'changeLevelName_deprecation' }
)
opts.levelKey = opts.changeLevelName
delete opts.changeLevelName
if (opts.prettyPrint) {
throw new Error('prettyPrint option is no longer supported, see the pino-pretty package.')
}
const { enabled, prettyPrint, prettifier, messageKey } = opts
const { enabled } = opts
if (enabled === false) opts.level = 'silent'
stream = stream || process.stdout
if (stream === process.stdout && stream.fd >= 0 && !hasBeenTampered(stream)) {
stream = buildSafeSonicBoom({ fd: stream.fd, sync: true })
}
if (prettyPrint) {
warning.emit('PINODEP008')
const prettyOpts = Object.assign({ messageKey }, prettyPrint)
stream = getPrettyStream(prettyOpts, prettifier, stream, instance)
}
return { opts, stream }
}
}

function final (logger, handler) {
const major = Number(process.versions.node.split('.')[0])
if (major >= 14) warning.emit('PINODEP009')

if (typeof logger === 'undefined' || typeof logger.child !== 'function') {
throw Error('expected a pino logger instance')
}
const hasHandler = (typeof handler !== 'undefined')
if (hasHandler && typeof handler !== 'function') {
throw Error('if supplied, the handler parameter should be a function')
}
const stream = logger[streamSym]
if (typeof stream.flushSync !== 'function') {
throw Error('final requires a stream that has a flushSync method, such as pino.destination')
}

const finalLogger = new Proxy(logger, {
get: (logger, key) => {
if (key in logger.levels.values) {
return (...args) => {
logger[key](...args)
stream.flushSync()
}
}
return logger[key]
}
})

if (!hasHandler) {
try {
stream.flushSync()
} catch {
// it's too late to wait for the stream to be ready
// because this is a final tick scenario.
// in practice there shouldn't be a situation where it isn't
// however, swallow the error just in case (and for easier testing)
}
return finalLogger
}

return (err = null, ...args) => {
try {
stream.flushSync()
} catch (e) {
// it's too late to wait for the stream to be ready
// because this is a final tick scenario.
// in practice there shouldn't be a situation where it isn't
// however, swallow the error just in case (and for easier testing)
}
return handler(err, finalLogger, ...args)
}
}

function stringify (obj, stringifySafeFn) {
try {
return JSON.stringify(obj)
Expand All @@ -510,16 +316,6 @@ function buildFormatters (level, bindings, log) {
}
}

function setMetadataProps (dest, that) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love to see this go

if (dest[needsMetadataGsym] === true) {
dest.lastLevel = that.lastLevel
dest.lastMsg = that.lastMsg
dest.lastObj = that.lastObj
dest.lastTime = that.lastTime
dest.lastLogger = that.lastLogger
}
}

/**
* Convert a string integer file descriptor to a proper native integer
* file descriptor.
Expand All @@ -539,12 +335,10 @@ function normalizeDestFileDescriptor (destination) {
module.exports = {
noop,
buildSafeSonicBoom,
getPrettyStream,
asChindings,
asJson,
genLog,
createArgsNormalizer,
final,
stringify,
buildFormatters,
normalizeDestFileDescriptor
Expand Down
2 changes: 2 additions & 0 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { join, isAbsolute } = require('path')

let onExit

/* istanbul ignore next */
if (global.WeakRef && global.WeakMap && global.FinalizationRegistry) {
// This require MUST be top level otherwise the transport would
// not work from within Jest as it hijacks require.
Expand Down Expand Up @@ -59,6 +60,7 @@ function buildStream (filename, workerData, workerOpts) {
}

function onExit () {
/* istanbul ignore next */
if (stream.closed) {
return
}
Expand Down