473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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 ?
Mar 28 '07 #1
7 1548
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.
Mar 28 '07 #2
bvdet
2,851 Expert Mod 2GB
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. .............................................
Mar 28 '07 #3
ghostdog74
511 Expert 256MB
actually you don't to create your own. Python has a logger module. Unless of course, you have other requirements.
Mar 29 '07 #4
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)
Mar 29 '07 #5
bvdet
2,851 Expert Mod 2GB
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?
Mar 30 '07 #6
bartonc
6,596 Expert 4TB
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.
Mar 30 '07 #7
Gotcha. For some reason I thought you were just needing the file name. Is it working well?
Yeah, seems to be working well.

Thanks
Mar 30 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Edvard Majakari | last post by:
Greetings, fellow Pythonistas! I'm about to create three modules. As an avid TDD fan I'd like to create typical 'use-cases' for each of these modules. One of them is rather large, and I wondered...
1
by: santhosh_176 | last post by:
:I Created a Pocket PC application for iPAQ 5450. Every thing went fine even installer creation. I could run the setup and install it into the actual device and worked fine. The application enables...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
1
by: Matt Tapia | last post by:
I want to create several files that will be accessible through the asp.net interface. I can call a stored procedure to create the file on the SQL server, however the asp.net user does not have...
2
by: | last post by:
Hi all, I have vs.net 2003 locally installed on my XP PRO machine. I have a web server on the LAN which has a website configured at http://services. This is accessible via my web browser and...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
1
by: Dan | last post by:
I have an application that I want to use for copying files. My goal is to copy a files, if a file is in use or not accessible because of security reasons I want to make note of that file then...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
3
by: Ed | last post by:
Hi, dear all, Sometimes, when I debug my program, VS seems very smart to find the cpp file even if the file was not in the working directory. I am wondering how did VS try to find the cpp file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.