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
Custom appender support for ESM project #1122
Comments
ESM should be able to load CommonJS but not vice versa as far as I recall. |
So the Probably a major change. |
@nicojs, is line 27 the correct line to change? log4js-node/lib/appenders/index.js Lines 24 to 32 in 2f29d0a
Would you be kind enough to do a PR? 😊 |
I tried to change the codes in - const tryLoading = (modulePath, config) => {
+ const tryLoading = async (modulePath, config) => {
= debug('Loading module from ', modulePath);
= try {
- return require(modulePath); // eslint-disable-line
+ return (await import(modulePath)).default;
= } catch (e) {
= // if the module was found, and we still got an error, then raise it
= configuration.throwExceptionIf(
= config,
- e.code !== 'MODULE_NOT_FOUND',
+ e.code !== 'ERR_MODULE_NOT_FOUND', - const createAppender = (name, config) => {
+ const createAppender = async (name, config) => {
= const appenderConfig = config.appenders[name];
= const appenderModule = appenderConfig.type.configure
- ? appenderConfig.type : loadAppenderModule(appenderConfig.type, config);
+ ? appenderConfig.type : await loadAppenderModule(appenderConfig.type, config); Would need any help anyone can render. |
Yes, this is what I meant by major change. Simply making some functions In the end, I think the |
You might want to go all-the-way and migrate this library itself to esm. |
Related to #1156. |
@jmeis, you can actually use it right now. The export interface CustomAppender {
type: string | AppenderModule;
[key: string]: any;
}
export interface AppenderModule {
configure: (config: Config, layouts: LayoutsParam) => AppenderFunction;
} So, if you import your custom Appender ahead of time, you can use it that way. import appender from './customAppender.js';
log4js.configure({
appenders: {
custom: { type: appender, customProperty1: false, customProperty2: (a,b)=>a+b }
},
categories: { default: { appenders: ['custom'], level: 'info' } }
}); I'd actually recommend that for the next major version, log4js adopts this as the main way to use an appender instead of the string |
Thanks, @ZachHaber! Alternatively, if your project is in ESM and your custom appender can be in CommonJS, rename your custom appender to have the |
Hi, I am migrating a project from CommonJS to pure ESM. Is it possible to load a custom appender at this time using pure ESM?
The text was updated successfully, but these errors were encountered: