diff --git a/src/lib/logger.js b/src/lib/logger.js index 9e1ab8906c..d334eef287 100644 --- a/src/lib/logger.js +++ b/src/lib/logger.js @@ -1,31 +1,61 @@ +const userLogger = { + error: undefined, + warn: undefined, + debug: undefined +} + const logger = { - error: (errorCode, ...text) => { - if (!console) { return } + error (code, ...text) { if (text && text.length <= 1) { text = text[0] || '' } - console.error( - `[next-auth][error][${errorCode.toLowerCase()}]`, - text, - `\nhttps://next-auth.js.org/errors#${errorCode.toLowerCase()}` - ) + code = code.toLowerCase() + const link = `https://next-auth.js.org/errors#${code}` + if (userLogger.error) { + userLogger.error({ code, text, link }) + } else { + console.error( + `[next-auth][error][${code}]`, + text, + `\n${link}` + ) + } }, - warn: (warnCode, ...text) => { - if (!console) { return } + warn (code, ...text) { if (text && text.length <= 1) { text = text[0] || '' } - console.warn( - `[next-auth][warn][${warnCode.toLowerCase()}]`, - text, - `\nhttps://next-auth.js.org/warnings#${warnCode.toLowerCase()}` - ) + code = code.toLowerCase() + const link = `https://next-auth.js.org/warnings#${code}` + if (userLogger.warn) { + userLogger.warn({ code, text, link }) + } else { + console.warn( + `[next-auth][warn][${code}]`, + text, + `\n${link}}` + ) + } }, - debug: (debugCode, ...text) => { - if (!console) { return } + debug (code, ...text) { if (text && text.length <= 1) { text = text[0] || '' } + code = code.toLowerCase() + if (userLogger.debug) { + userLogger.debug({ code, text }) + } if (process && process.env && process.env._NEXTAUTH_DEBUG) { console.log( - `[next-auth][debug][${debugCode.toLowerCase()}]`, + `[next-auth][debug][${code}]`, text ) } + }, + setLogger (logger = {}) { + if (typeof logger.error === 'function') { + userLogger.error = logger.error + } + if (typeof logger.warn === 'function') { + userLogger.warn = logger.warn + } + if (typeof logger.debug === 'function') { + userLogger.debug = logger.debug + } } } diff --git a/src/server/index.js b/src/server/index.js index 8c8cbb29b5..af1a9c5249 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -22,6 +22,9 @@ if (!process.env.NEXTAUTH_URL) { } export default async (req, res, userSuppliedOptions) => { + if (userSuppliedOptions.logger) { + logger.setLogger(userSuppliedOptions.logger) + } // To the best of my knowledge, we need to return a promise here // to avoid early termination of calls to the serverless function // (and then return that promise when we are done) - eslint