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

pattern for reloading log4js config when config file changes #978

Closed
GlenBikes opened this issue Dec 23, 2019 · 7 comments · Fixed by #1113
Closed

pattern for reloading log4js config when config file changes #978

GlenBikes opened this issue Dec 23, 2019 · 7 comments · Fixed by #1113

Comments

@GlenBikes
Copy link

I'm sure that a github issue is not the appropriate place for this, so I apologize, but I can't find any other place to ask the question.

I know that reloading on config file changes has been removed from log4js, but I am trying to do it myself and I am struggling to figure out how to do this without getting errors. Also, searching github repos for log4js and chokidar or watchr gives no results which makes me think this is not something many people are trying to do?

Is there a sample somewhere that could be added to the documentation to show how to do this? I am trying to use chokidar to do this but no matter what I try I end up with errors due to write operations occurring after the shutdown occurs.

@nomiddlename
Copy link
Collaborator

Could you post some sample code for what you're trying to do, please? That might help us work out a solution, but basically what you should do is call log4js.shutdown in your chokidar event handler, and then call log4js.configure in the callback from log4js.shutdown. I haven't used chokidar or watchr, it wasn't something we ever ended up needing in our applications (hence why the feature was removed), so I'm just guessing here.

@lamweili
Copy link
Contributor

lamweili commented Jan 12, 2020

Hi @nomiddlename ,

Can we call log4js.configure (without calling log4js.shutdown) to re-configure?

I tested and it works, but I'm not sure if there is any underlying performance impact or memory leak if I use it this way.

Use case: To re-configure logging levels on-the-fly

Or maybe, can the log4js.configure checks if it has been configured before? If it has, it runs log4js.shutdown implicitly before re-configuring. Perhaps consider this as a requested feature?

PS: I wanted to ask this on Slack, but it seems the invitation link is no longer valid.

@nomiddlename
Copy link
Collaborator

The only problem with calling configure without shutdown is that it may leave resources open (file handles, network connections, etc). If you're just logging to console or stdout, or your appender creates a new connection for every log message, then you should be ok to skip shutdown.

@nomiddlename
Copy link
Collaborator

I'm going to close this, because there's no issue to fix.

@OmgImAlexis
Copy link

@nomiddlename if we're reloading for example to change pattern is there any issue with just calling configure? Also what happens while the logger is shutdown, is there a queue or anything?

@lamweili
Copy link
Contributor

lamweili commented Jan 19, 2022

@OmgImAlexis From log4js@6.4.0 onwards, calling log4js.configure() properly closes all existing file handlers and re-initializes log4js using the latest provided configuration (past configurations are voided). Read more in #1105 and #1113.

Keep an updated copy of your configuration (which you will keep changing) to pass into the log4js.configure() calls.

@OmgImAlexis
Copy link

Thank you so so much @peteriman 🙏

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

Successfully merging a pull request may close this issue.

4 participants