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

How register work? #3088

Closed
vipcxj opened this issue May 10, 2024 · 3 comments
Closed

How register work? #3088

vipcxj opened this issue May 10, 2024 · 3 comments

Comments

@vipcxj
Copy link

vipcxj commented May 10, 2024

For example I want to create many many logger and bind each to a object instance.

class Object {
  std::shared_ptr<spdlog::logger> m_logger;
}

When the object is destroied, will the logger be unregistered?
What about all m_loggers of objects with same name?
What about all m_loggers of objects with different name?
Will spdlog::set_level (set_pattern, set_formatter, set_err_handler) effect m_loggers?

@tt4g
Copy link
Contributor

tt4g commented May 10, 2024

You need to register and unregister loggers to the spdlog registry with spdlog::register_logger() and spdlog::drop().
spdlog::* API affects all loggers registered in the registry.

spdlog::set_level() calls spdlog::logger::set_level() of the loggers registered in the registry.

spdlog identifies loggers registered in the registry by name (spdlog::logger::name()),
You can get it by passing the logger name to spdlog::get().

When the object is destroied, will the logger be unregistered?

No.

What about all m_loggers of objects with same name?
What about all m_loggers of objects with different name?

Loggers registered in the registry cannot use the same name.

Will spdlog::set_level (set_pattern, set_formatter, set_err_handler) effect m_loggers?

Yes.

@vipcxj
Copy link
Author

vipcxj commented May 10, 2024

@tt4g Can I use the logger without register it? Then when the object is destroyed, will m_logger be safely destroyed?

@tt4g
Copy link
Contributor

tt4g commented May 10, 2024

You can use the logger without registering it in the registry.
However, it is no longer under the control of spdlog, so it is not affected by the spdlog::* API.

m_logger will be safely deleted if the destructor is called by std::shared_ptr.

@gabime gabime closed this as completed May 15, 2024
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

No branches or pull requests

3 participants