0

I have a python logging object type for e.g: <logging.Logger object at 0x00000000028AA0F0> that is written in a text file and I am trying to convert that string into that same instance.

Note that this instance has already been created by the same program in the same computer. So how do you think I should turn that string into an object, so that I can reuse all its features.

Note: This is not a class object, it is a logging type object so I am guessing that makes a difference. I tried solving it by trying to convert it into a class object but I got an error that AttributeError: 'module' object has no attribute '<logging.Logger object at 0x00000000028AA0F0>.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • If I understand your question, you have the string `` and you want to convert the string to an actual logger instance. Is that correct? – Mike67 Oct 18 '20 at 22:55
  • @Mike67 Yes, that is correct!!! – Ridwan Chowdhury Oct 18 '20 at 22:59
  • 1
    Recovering the object is not possible. The string is just a representation of the object. The hex number is just a memory address where the object existed when you printed the string. When the process ended, the logger object was deleted. – Mike67 Oct 18 '20 at 23:17
  • @Mike67 Suppose I have the actual object. Is there a way to use that same object instance through different python modules by importing it without actually creating new instances? For e.g if I actually import the module where the instance was created originally, each time I import it a new instance gets created. But I want to use the same original instance, throughout all different modules, that was created the first time I ran that module. – Ridwan Chowdhury Oct 18 '20 at 23:22
  • yes - create a global variable that stores the logger object then use it throughout your script – Mike67 Oct 18 '20 at 23:24
  • @Mike67 But a global variable only exists in a module. So suppose I declare a global variable glob_var = logger i.e our object but each time I try to import that glob_var value in a different module the logger will trigger the statement which is initializing it which will create a new instance. That way I am not going to get the original object that was created the first time logger was initialized. How do I keep that record of the object such that it never changes no matter how many different modules import the global variable – Ridwan Chowdhury Oct 18 '20 at 23:32
  • You can create a singleton wrapper class that creates a single instance of the logger and always uses the same instance. – Mike67 Oct 18 '20 at 23:36
  • @Mike67 Ok I can extend this problem to convert my initialization of logger into a singleton class. Should I edit this question out or should I create a new question where I show my original initialization of logger? I do not know how to create singleton classes so are you willing to help? I would be very grateful if you can help me convert my initialization to a singleton class – Ridwan Chowdhury Oct 18 '20 at 23:44
  • Yes - please edit the question to show how you're using the logger code now and describe the goal. It should be simple enough to convert it to a singleton process. – Mike67 Oct 19 '20 at 00:22
  • @Mike67 I already made the question before you posted this comment. You can find it here https://stackoverflow.com/questions/64419489/converting-a-function-into-a-singleton-class-that-returns-one-instance. Please answer. Thank You – Ridwan Chowdhury Oct 19 '20 at 00:33

0 Answers0