sainsburys-tech / next-logger

JSON logging patcher for Next.js
MIT License
144 stars 14 forks source link

nextLogger.trace is not modified #14

Closed devinrhode2 closed 2 years ago

devinrhode2 commented 2 years ago

Using next 12.0.8

next v10 was released around fall 2020, it seems that v1 of this package was released very end of December in 2020

In Next v10/v11 nextLogger does not have a trace method. However, in next v12, nextLogger now a trace method.

CleanShot 2022-04-05 at 11 28 22@2x

devinrhode2 commented 2 years ago

Here's my patch-package diff/patch for next-logger@3.0.0:

diff --git a/node_modules/next-logger/lib/patches/next.js b/node_modules/next-logger/lib/patches/next.js
index 2ea521e..992346b 100644
--- a/node_modules/next-logger/lib/patches/next.js
+++ b/node_modules/next-logger/lib/patches/next.js
@@ -2,20 +2,44 @@ const nextLogger = require('next/dist/build/output/log')

 const logger = require('../logger')

-const getPinoMethod = nextMethod => {
-  const childLogger = logger.child({ name: 'next.js', prefix: nextMethod })
+// Goal: cause type error when:
+//   1. There's an incorrect method `asdf`
+//   2. When there's a missing method (next added a new one)
+// Array approach doesn't quite work:
+//   Basic attempt: https://www.typescriptlang.org/play?ssl=13&ssc=1&pln=19&pc=2&filetype=js#code/MYewdgzgLgBADgJwKYDMCWAPJEYF4YwD0AVMTAAJQCecSMA3qJFAL4zGEwAU9AUAQHcAhmigAuGAHJhomAFpJAGn4wkCBCAQTJajQnlKVwhGG3GwB5QWRCAJlW037llWjAoQ2tx5cEkANyQwcSkAoNgFKxgoBCFgJG0YuLpIlQA5AFEAdQB9AFkMgBUACQB5ABEctNKcjIAlOtK6xNj4lxYASgBuXl6SYn4ySho6egBBdSEqAB5BghgAayQqEBRokdX4ZHQsCDmAPjYOXiZoGDAkDCgAGRAAczu1PKQoAAsQWxx8LhUAbWkRFAlKF1JpgQCTOCnFRwd4QOCwsFwUl4uChBBbChJABdXgdIA
+//   Advanced attempt: https://www.typescriptlang.org/play?ssl=14&ssc=11&pln=14&pc=19&filetype=js#code/MYewdgzgLgBADgJwKYDMCWAPJEYF4YwD0AVMTAAJQCecSMA3qJFAL4zGEwAU9AUAQHcAhmigAuGAHJhomAFpJAGn4wkCBCAQTJajQnlKVwhGG3GwB5QWRCAJlW037llWjAoQ2tx5cEkANyQwcSkAoNgFKxgoBCFgJG0YuLpIlQA5AFEAdQB9AFkMgBUACQB5ABEctNKcjIAlOtK6xNj4lxYASgBuXl6SYn4ySho6eidwABsqGABtQYIYAGskKhAUaJG1+GR0LAgrMgJl1fXqWi3EVExsA4XjrbOkC53r-fmjlYfN9cvdm-elp9Tt9tlc9rcPicNucfi9wQD7sCYaC-hB5gBdNgcXhMaAwMBIDBQAAyIAA5mS1HkkFAABYgWw4fD9CiPBi41jsThcFQzaQiKBKULqTRC-kmMVOKhi7wgMVhYJipLxSTo3gdIA
+// Object approach actually works:
+//   https://www.typescriptlang.org/play?filetype=js#code/MYewdgzgLgBADgJwKYDMCWAPJEYF4YDeAUDDAO4CGaUAXDAOSXUwC09ANCTEggiAnXo8+CVhy6UEYQZLBjOpZBQAmAT0FK18rmjAoQg3fu2kkANyRhaDc5dhsFMKAgrAkg566QmYAOQCiAOoA+gCy-gAqABIA8gAiwb4xwf4ASqkxqR4ubtoAvgDcRMUA9ABUZTAAAlCqcN4EtfUgKPDI6FgQeTBlJUSgkLBgSBhQADIgAOaTPKFIUAAWIMo4+OWVNXUNA9DdvTAAFMSkTNaMVPbiprz8gsL8PrIyFFI+muoM7z5GBgw-PrYrHcLFYfJ43NkvD4AiFwtF4olkmkMlkGODvGwiHkAJRAA

-  switch (nextMethod) {
+/** @type {typeof nextLogger.prefixes} */
+const nextLoggerMethodPrefixMap = {
+  wait: 'wait -',
+  error: 'error -',
+  warn: 'warn -',
+  ready: 'ready -',
+  info: 'info -',
+  event: 'event -',
+  trace: 'trace -',
+}
+
+// Inside next-logger: use ObjectTyped.keys instead (npm install object-typed)
+Object.keys(nextLoggerMethodPrefixMap).forEach((
+  /** @type {keyof nextLoggerMethodPrefixMap} */
+  nextLoggerMethod
+) => {
+  // Q: can we 'bind' correct `prefix:` onto each childLogger method?
+  // and thus create one childLogger instance?
+  const childLogger = logger.child({
+    name: 'next.js',
+    prefix: nextLoggerMethod
+  })
+
+  switch (nextLoggerMethod) {
     case 'error':
-      return childLogger.error.bind(childLogger)
+      nextLogger[nextLoggerMethod] = childLogger.error.bind(childLogger)
     case 'warn':
-      return childLogger.warn.bind(childLogger)
+      nextLogger[nextLoggerMethod] = childLogger.warn.bind(childLogger)
     default:
-      return childLogger.info.bind(childLogger)
+      nextLogger[nextLoggerMethod] = childLogger.info.bind(childLogger)
   }
-}
-
-const methods = ['wait', 'error', 'warn', 'ready', 'info', 'event']
-methods.forEach(method => {
-  nextLogger[method] = getPinoMethod(method)
 })
atkinchris commented 2 years ago

I've added this through iterating the Next.js prefixes, and handled trace separately (https://github.com/atkinchris/next-logger/commit/7a77a22b3510f0f620fe7e00b1acf73207275313).

I'll get it released shortly.

devinrhode2 commented 2 years ago

resolved in commit direct to main: https://github.com/atkinchris/next-logger/commit/7a77a22b3510f0f620fe7e00b1acf73207275313