-
Notifications
You must be signed in to change notification settings - Fork 711
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 to get a isolated logger #487
Comments
Hi. You can probably use See more in depth explanations in the documentation: Creating independent loggers with separate set of handlers. |
thank you for your help, but deepcopy need to remove all at first, this will affect other place simply use "logger", i think maybe we can add a interface to obain a empty new logger instance ... |
This is something I could consider. Do you mind elaborating your use case please? What do you need that cannot be done with |
yeah, in my case, i start many task(over x0000+), each one with a taskid, i want record task log to separate log, such as task1.log / task2.log ... |
and use filter msg will log to global "logger" too, that is i do not want |
i use configed global "logger" for convenience, so do i want a free logger instance too :) |
Thanks. Your use case is definitely not compatible with the Some time ago I stated that Loguru wasn't meant to be used with multiple loggers (see #72 (comment)). However, I've changed my mind and will probably add a |
In the meantime you can probably add |
than i can reconfigure global logger again,is that right? |
Yeah, keep using logger_a = copy.deepcopy(base_logger)
logger_a.add("specific.log") |
ok,thank you very much🤣 |
is it possible to use thousands of logger instance that with independent file? |
maybe is better to merge log to one file and record with identity like what you show
|
I hadn't thought about it but I think you are right. It's better to limit the number of files open at the same time, because the OS can put a limit on it. That's indeed why I prefer to use a single logger, with a small number of immutable handlers. This way, logging remains simple and limits the risk of problems. The logs can then be post-processed if needed and each worker easily identified thanks to |
For anyone still stumbling on this issue. I have a complex situation where I need to define a different logger for both a child and parent module. from loguru._logger import Core, Logger
logger = Logger(
core=Core(),
exception=None,
depth=0,
record=False,
lazy=False,
colors=True,
raw=False,
capture=True,
patchers=[],
extra={},
) |
@kevinderuijter Note that you're relying on a private API whose stability is not guaranteed and which may break your application in future updates. Why not use the Anyway I plan to add a public |
@Delgan Thank you for the warning, I am aware of the implications. When I use deep copy, at least in my scenario, I get the following exception. |
@kevinderuijter Yes, you need to # Remove the default handler causing copy to fail.
logger.remove()
# Create an independent empty logger.
logger_template = copy.deepcopy(logger)
# Restore default handler.
logger.add(sys.stderr)
# Whenever you need a new independent logger, just copy the template.
new_logger = copy.deepcopy(logger_template) Not very convenient, I agree. |
@Delgan Somehow calling
I wouldn't mind giving more context or diving deeper but I think I should open another Issue in that case. |
@kevinderuijter Weird, that's not what I'm observing with the following code: from loguru import logger
import sys
import copy
logger.remove()
logger_template = copy.deepcopy(logger)
new_logger = copy.deepcopy(logger_template)
new_logger.configure(handlers=[{"sink": sys.stderr}])
logger.info("Nothing printed")
logger_template.info("Nothing printed")
new_logger.info("Something printed") Feel free to open a new issue if you want to discuss that. But I guess it's not that important since you've found another workaround. In any case, the future |
@Delgan any updates on |
@serozhenka Np ETA so far, sorry. |
@Delgan am I able to help anyhow? |
Thanks for the proposal, @serozhenka. There is a lot to be done: implementing the function, documenting it, testing it. This is no small task for an external contributor. You could give it a try, but I don't want you to invest too much time in this. In the end, it's not complicated for me to implement it, but I need to spend time on it. You could use the |
i mean to get a logger for specific.log with some special format, but when i call logger_a.remove(), it will destory logger_b and logger.
The text was updated successfully, but these errors were encountered: