Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating a standard log file that is accessible by all other files

Newbie
 
Join Date: Feb 2007
Posts: 20
#1: Mar 28 '07
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 ?
Expand|Select|Wrap|Line Numbers
  1. -------
  2. File1:
  3. --------
  4.  
  5. log_file = file('out.log',"w")
  6. log_obj = log.log(log_file)
  7.  
  8. log_obj.write("Hello world from File 1") 
  9.  
  10. ----------
  11. log.py 
  12. ----------
  13. class log:
  14.       def __init__(self,log):
  15.          self.log = log 
  16.        def write(self,message):
  17.             self.log.write(message)
  18. ----------
  19. File 2: 
  20. ----------
  21.  
  22. from log import * 
  23. log.write("message from file2")
This obviously will not work because I am passing a str object instead of log instance.

Any tips ?

Newbie
 
Join Date: Feb 2007
Posts: 20
#2: Mar 28 '07

re: Creating a standard log file that is accessible by all other files


Tried implementing the following method and it seems to work. Not sure if it's a good way to do it.

<code>
Expand|Select|Wrap|Line Numbers
  1. file1.py 
  2. # Declare a global list which can be accessible by other files 
  3. log_list = []
  4.  
  5. def func1():
  6.    global log_list 
  7.    log_file = file('abc.out',"w")
  8.    log_list.append(log_file)
  9.  
  10. </code>
  11.  
  12. <code> 
  13. File2.py 
  14. import log 
  15. import file1 
  16.  
  17. file1.func1() 
  18. log.write("Hello World ")
  19. </code> 
  20.  
  21. <code> 
  22. log.py 
  23. from g import log_list 
  24.  
  25. def write(message):
  26.        log_list[0].write(message) 
  27. </code> 
  28.  
So basically what happens is that when you run file2, file1 creates a log file which can be shared by other files. This one worked because log_list is a list which is mutable and that is imported by log.py. I thought that even log file handlers where mutable, but that didn't work for some reason. And putting that log_file handle within a list seemed to solve the issue.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,564
#3: Mar 29 '07

re: Creating a standard log file that is accessible by all other files


Quote:

Originally Posted by indiarocks

Tried implementing the following method and it seems to work. Not sure if it's a good way to do it.

<code>

file1.py
# Declare a global list which can be accessible by other files
log_list = []

def func1():
global log_list
log_file = file('abc.out',"w")
log_list.append(log_file)

</code>

<code>
File2.py
import log
import file1

file1.func1()
log.write("Hello World ")
</code>

<code>
log.py
from g import log_list

def write(message):
log_list[0].write(message)
</code>

So basically what happens is that when you run file2, file1 creates a log file which can be shared by other files. This one worked because log_list is a list which is mutable and that is imported by log.py. I thought that even log file handlers where mutable, but that didn't work for some reason. And putting that log_file handle within a list seemed to solve the issue.

I see you are trying to use code tags. Good, except use square brackets '[ ]' instead of '< >'.

Why not:
Expand|Select|Wrap|Line Numbers
  1. # file1.py
  2.  
  3. log_file_name = r'H:\TEMP\log.txt'
  4.  
  5. ............................................................
  6.  
  7.  
  8. # log_file.py
  9.  
  10. import file1
  11.  
  12. # open log file
  13. f = open(file1.log_file_name, 'a')
  14. .............................................
Expert
 
Join Date: Apr 2006
Posts: 512
#4: Mar 29 '07

re: Creating a standard log file that is accessible by all other files


actually you don't to create your own. Python has a logger module. Unless of course, you have other requirements.
Newbie
 
Join Date: Feb 2007
Posts: 20
#5: Mar 30 '07

re: Creating a standard log file that is accessible by all other files


Quote:

Originally Posted by bvdet

I see you are trying to use code tags. Good, except use square brackets '[ ]' instead of '< >'.

Why not:

Expand|Select|Wrap|Line Numbers
  1. # file1.py
  2.  
  3. log_file_name = r'H:\TEMP\log.txt'
  4.  
  5. ............................................................
  6.  
  7.  
  8. # log_file.py
  9.  
  10. import file1
  11.  
  12. # open log file
  13. f = open(file1.log_file_name, 'a')
  14. .............................................

In this case if I have my program spread across 15 files, I need to explicitly open each of the files in these 15 files. To avoid that I created a general log function which can be imported by other files and they can directly use as log(msg)
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,564
#6: Mar 30 '07

re: Creating a standard log file that is accessible by all other files


Quote:

Originally Posted by indiarocks

In this case if I have my program spread across 15 files, I need to explicitly open each of the files in these 15 files. To avoid that I created a general log function which can be imported by other files and they can directly use as log(msg)

Gotcha. For some reason I thought you were just needing the file name. Is it working well?
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#7: Mar 30 '07

re: Creating a standard log file that is accessible by all other files


Quote:

Originally Posted by indiarocks

In this case if I have my program spread across 15 files, I need to explicitly open each of the files in these 15 files. To avoid that I created a general log function which can be imported by other files and they can directly use as log(msg)

They're called 'tags', so (if you know HTML) in makes sense to use
<code>
</code>
But on this site, we use [code] and the next one has the forward slash (but if I put it here it will disappear. You can use the # button at the top of the editor.
Newbie
 
Join Date: Feb 2007
Posts: 20
#8: Mar 30 '07

re: Creating a standard log file that is accessible by all other files


Quote:

Originally Posted by bvdet

Gotcha. For some reason I thought you were just needing the file name. Is it working well?

Yeah, seems to be working well.

Thanks
Reply