Just a basic question, I want to create a standard log file API and want that API to be shared by all my other python files.
For eg. I have file1 which creates a file handle and hands it over to say log.py
Now say I have file2, and it needs to write into the same log file, how do I achieve this and what is the best way of doing it ?
This obviously will not work because I am passing a str object instead of log instance.
Any tips ?
For eg. I have file1 which creates a file handle and hands it over to say log.py
Now say I have file2, and it needs to write into the same log file, how do I achieve this and what is the best way of doing it ?
Code:
------- File1: -------- log_file = file('out.log',"w") log_obj = log.log(log_file) log_obj.write("Hello world from File 1") ---------- log.py ---------- class log: def __init__(self,log): self.log = log def write(self,message): self.log.write(message) ---------- File 2: ---------- from log import * log.write("message from file2")
Any tips ?
Comment