-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
loggers can't be pickled #74705
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
Comments
Loggers could simply be pickled and unpickled by name, but pickle currently tries the hard way: >>> import pickle
>>> import logging
>>> log = logging.getLogger('foo')
>>> pickle.dumps(log)
Traceback (most recent call last):
File "<ipython-input-4-6ecead831873>", line 1, in <module>
pickle.dumps(log)
TypeError: can't pickle _thread.RLock objects |
I am not sure it is a good idea to support pickling of loggers, as they are singletons and generally aren't supposed to be instantiated other than by calling logging.getLogger(name). What's the use case for pickling them, giving that (as you say) just the name could be used? In general, it's not needed to have loggers as part of an object instance, so I don't quite see where the need to pickle comes from. One could implement __getstate__() to just return the name, but there is no corresponding obvious implementation of __setstate__() because loggers aren't meant to be instantiated via unpickling. |
Le 01/06/2017 à 09:14, Vinay Sajip a écrit :
Pickling them by name is precisely what I'm having in mind. Right now, In other words (untested, but you get the idea): class Logger:
def __reduce__(self):
return getLogger, (self.name,)
You usually don't pickle loggers directly. You pickle an object that
No need for __getstate__ or __setstate__, __reduce__ should work fine |
The idea LGTM. But we should first check that the logger is accessible by the name: getLogger(self.name) is self. The same is done when pickling classes, functions, etc. It shouldn't be a surprise on unpickler side. And maybe use not getLogger(), but different method which should fail if the logger doesn't exist rather than creating it. Otherwise this looks as pickling open files by name. |
Le 01/06/2017 à 11:28, Serhiy Storchaka a écrit :
Good idea.
I disagree. The fact that it creates a new logger if it doesn't exist is class MyObject:
def __init__(self):
self.logger = getLogger('myobject') then unpickling the first MyObject instance in a new process should also |
May be. I don't have a strong opinion. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: