log4js-node / log4js-node

A port of log4js to node.js
https://log4js-node.github.io/log4js-node/
Other
5.8k stars 773 forks source link

When the type is module, there is an error when reading Appenders. How to deal with it #1277

Closed artiely closed 2 years ago

artiely commented 2 years ago

package.json

+"type":"module",

node v16.15.0 log4js ^6.4.5

import log4js from "log4js"
log4js.configure({
  appenders: {
    tiktok: {
      type: 'log'
    },
    everything: {
      type: 'file',
      filename: 'all-the-logs.log'
    },
    emergencies: { type: 'file', filename: 'errors.log' },
    justError: {
      type: 'logLevelFilter',
      appender: 'emergencies',
      level: 'error'
    }
  },
  categories: { default: { appenders: ['tiktok', 'everything', 'justError'], level: 'debug' } },
  pm2: true,
  pm2InstanceVar: 'NODE_APP_INSTANCE',
})

log.js

function stdoutAppender(layout, timezoneOffset) {
  const appender = (loggingEvent) => {
    if (loggingEvent.level.levelStr == "DEBUG") {
      process.stdout.write(`${layout(loggingEvent, timezoneOffset)}\n`);
    } else if (loggingEvent.level.levelStr == "INFO") {
     //
      process.stdout.write(`${layout(loggingEvent, timezoneOffset)}\n`);
    };
  }

  // add a shutdown function.
  appender.shutdown = (done) => {
    process.stdout.write('', done);
  };

  return appender;
}

// stdout configure doesn't need to use findAppender, or levels
function configure(config, layouts) {
  // the default layout for the appender
  let layout = layouts.colouredLayout;
  // check if there is another layout specified
  if (config.layout) {
    // load the layout
    layout = layouts.layout(config.layout.type, config.layout);
  }
  //create a new appender instance
  return stdoutAppender(layout, config.timezoneOffset);
}

//export the only function needed
exports.configure = configure
tanjie@tanjiedeMacBook-Pro back % node index.js
/Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/configuration.js:31
      throw new Error(`Problem with log4js configuration: (${util.inspect(config, { depth: 5 })})`
            ^

Error: Problem with log4js configuration: ({
  appenders: {
    tiktok: { type: 'log' },
    everything: { type: 'file', filename: 'all-the-logs.log' },
    emergencies: { type: 'file', filename: 'errors.log' },
    justError: { type: 'logLevelFilter', appender: 'emergencies', level: 'error' }
  },
  categories: {
    default: {
      appenders: [ 'tiktok', 'everything', 'justError' ],
      level: 'debug'
    }
  },
  pm2: true,
  pm2InstanceVar: 'NODE_APP_INSTANCE'
}) - appender "/Users/tanjie/Desktop/danmu-plus/back/log" could not be loaded (error was: Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/tanjie/Desktop/danmu-plus/back/log.js from /Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js not supported.
Instead change the require of log.js in /Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js to a dynamic import() which is available in all CommonJS modules.)
    at /Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/configuration.js:31:13
    at Array.forEach (<anonymous>)
    at Object.throwExceptionIf (/Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/configuration.js:29:9)
    at tryLoading (/Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js:31:19)
    at loadAppenderModule (/Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js:44:6)
    at createAppender (/Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js:65:29)
    at getAppender (/Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js:56:20)
    at /Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js:117:7
    at Array.forEach (<anonymous>)
    at setup (/Users/tanjie/Desktop/danmu-plus/back/node_modules/log4js/lib/appenders/index.js:112:33)
tanjie@tanjiedeMacBook-Pro back % 
lamweili commented 2 years ago

While your project is in ESM, log4js is in CJS. Thus, when log4js (CJS) attempts to require your custom appender log.js (ESM), it will not work.


There are two ways:

  1. Your log.js custom appender can be explicitly declared as a CJS. With PR #1280, rename log.js to log.cjs. That should work.

  2. Alternatively, you can also take a look at https://github.com/log4js-node/log4js-node/issues/1122#issuecomment-1155285459 by importing the appender yourself and then passing it to log4js.configure().

lamweili commented 2 years ago

@artiely Did you managed to get this resolved?