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 ? -
-------
-
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")
This obviously will not work because I am passing a str object instead of log instance.
Any tips ?
7 1548
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.
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: - # file1.py
-
-
log_file_name = r'H:\TEMP\log.txt'
-
-
............................................................
-
-
-
# log_file.py
-
-
import file1
-
-
# open log file
-
f = open(file1.log_file_name, 'a')
-
.............................................
actually you don't to create your own. Python has a logger module. Unless of course, you have other requirements.
I see you are trying to use code tags. Good, except use square brackets '[ ]' instead of '< >'.
Why not: - # file1.py
-
-
log_file_name = r'H:\TEMP\log.txt'
-
-
............................................................
-
-
-
# log_file.py
-
-
import file1
-
-
# open log file
-
f = open(file1.log_file_name, 'a')
-
.............................................
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 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?
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.
Gotcha. For some reason I thought you were just needing the file name. Is it working well?
Yeah, seems to be working well.
Thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
| |