Skip to content
This repository was archived by the owner on Apr 3, 2022. It is now read-only.

Logger fails due to unknown log_appenders module #11

Open
simonihmig opened this issue Sep 13, 2020 · 5 comments
Open

Logger fails due to unknown log_appenders module #11

simonihmig opened this issue Sep 13, 2020 · 5 comments

Comments

@simonihmig
Copy link
Contributor

No description provided.

@jpg0
Copy link
Owner

jpg0 commented Sep 14, 2020

Need to look into whether this actually works with the included GraalJS module loading.

@simonihmig
Copy link
Contributor Author

So, for you it's working as you seem to have that module in your lib folder: https://github.com/jpg0/oh-config/blob/master/automation/lib/javascript/personal/node_modules/log_appenders.js

I guess that's not installed by npm, and certainly the ohj npm module has no dependency on it, so for me (and any other doing a plain installation of ohj) this is missing, and thus failing. I think you cannot require it, unless you either ship it by yourself or depend on it. Or do I miss something?

@jpg0
Copy link
Owner

jpg0 commented Sep 15, 2020

Sure - I have this file because I want to explicitly define additional log appenders. What is supposed to happen is that if you do not want additional appenders and you won't have this module, then the log module will fail to load it and default to no additional appenders.

(If you have a better way of doing this then I'm happy to hear it!)

@simonihmig
Copy link
Contributor Author

Usually I would not create the "appender" (or whatever) by myself, instead pass it when creating the logger instance, like with a Dependency Injection system. You can do it with the appenderProvider argument, but I guess your problem was that you wanted it to be used not only for the logger instance you create by yourself, but also for all the other loggers that are created implicitly within ohj?

So first it seems the JS engine here is behaving differently than node, as your code would normally work I think (able to catch the thrown module not found exception)! 🤔

Two approaches come to my mind:

  • define a static method like Log.setAppender() that you can pass the appender, and this is used by default (even for implicitly created loggers)
  • use require.resolve() to check if the module is available, and only then require it. Assuming that GraalVM has this, and the thrown exception is catchable in this case

@jpg0
Copy link
Owner

jpg0 commented Sep 17, 2020

Correct, I really want it to be applied to all loggers, within ohj and in consuming code.

I have considered your first option, however in OH, each script is run in it's own instance of the runtime, meaning no shared state (unless explicitly passed via the host), so it would mean calling the static method at the top of every script.

I was not aware of require.resolve - I'll check it out!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants