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

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

Closed
artiely opened this issue Jul 1, 2022 · 2 comments · Fixed by #1280
Closed
Labels
bug Something isn't working
Milestone

Comments

@artiely
Copy link

artiely commented Jul 1, 2022

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 lamweili added the bug Something isn't working label Jul 1, 2022
@lamweili lamweili added this to the 6.5.3 milestone Jul 1, 2022
@lamweili lamweili linked a pull request Jul 3, 2022 that will close this issue
@lamweili
Copy link
Contributor

lamweili commented Jul 3, 2022

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 fix: load CJS appenders for ESM packages #1280, rename log.js to log.cjs. That should work.

  2. Alternatively, you can also take a look at
    Custom appender support for ESM project #1122 (comment) by importing the appender yourself and then passing it to log4js.configure().

@lamweili
Copy link
Contributor

lamweili commented Jul 6, 2022

@artiely Did you managed to get this resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants