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

TypeError: log4js.getLogger is not a function #1410

Open TheChilliPL opened 10 months ago

TheChilliPL commented 10 months ago

Trying to use anything in log4js causes a TypeError. IDE sees all the methods correctly, but TypeScript itself does not.

D:\Projects\eng_to_ipa>npm start -- trans

> eng-to-ipa@0.1.0 start
> npx tsx src/cli.ts trans

D:\Projects\eng_to_ipa\src\main.ts:3
export let defaultLogger = log4js.getLogger();
                                  ^

TypeError: log4js.getLogger is not a function
    at <anonymous> (D:\Projects\eng_to_ipa\src\main.ts:3:35)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:120:12)

Node.js v21.5.0

D:\Projects\eng_to_ipa>

Importing with import { getLogger } from "log4js" doesn't work either

lamweili commented 10 months ago

What's your import statement? Could you show what's in your main.ts?

Did you try this?

import * as log4js from "log4js";
log4js.configure({
  appenders: { cheese: { type: "file", filename: "cheese.log" } },
  categories: { default: { appenders: ["cheese"], level: "error" } },
});

const logger = log4js.getLogger();
logger.level = "debug";
logger.debug("Some debug messages");

(src: https://github.com/log4js-node/log4js-node?tab=readme-ov-file#typescript)

TheChilliPL commented 10 months ago

The whole main.ts is just

import * as log4js from "log4js";

export let defaultLogger = log4js.getLogger();

If I do configure first, it just does

TypeError: log4js.configure is not a function
relandboyle commented 4 months ago

@TheChilliPL @lamweili What was the resolution here? I'm running into the same issue, I think.

TheChilliPL commented 4 months ago

@relandboyle Never fixed it, just never started using this logger, because I don't have the time necessary to debug this issue. If you find a way to fix it, however, please do let know!

MizuYaYa commented 3 months ago
import log4js from "log4js";

const logger = log4js.getLogger();
logger.level = "debug";

Now I was able to do it. Hope this is useful.

cjsilva-umich commented 1 month ago
import log4js from "log4js";

const logger = log4js.getLogger();
logger.level = "debug";

Now I was able to do it. Hope this is useful.

This is the correct way to import. The documentation really needs to be updated. Just lost a lot of time trying to understand why I was getting the "log4js.[blank] is not a function" errors.