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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indiarocks
    New Member
    • Feb 2007
    • 20

    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 ?
    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")
    This obviously will not work because I am passing a str object instead of log instance.

    Any tips ?
    Last edited by bartonc; Mar 28 '07, 06:24 PM. Reason: added [code][/code] tags
  • indiarocks
    New Member
    • Feb 2007
    • 20

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

    <code>
    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.
    Last edited by bartonc; Mar 30 '07, 11:18 AM. Reason: added [code][/code] tags

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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("Hell o 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:
      Code:
      # 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')
      .............................................

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        actually you don't to create your own. Python has a logger module. Unless of course, you have other requirements.

        Comment

        • indiarocks
          New Member
          • Feb 2007
          • 20

          #5
          Originally posted by bvdet
          I see you are trying to use code tags. Good, except use square brackets '[ ]' instead of '< >'.

          Why not:
          Code:
          # 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)

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            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?

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              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.

              Comment

              • indiarocks
                New Member
                • Feb 2007
                • 20

                #8
                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

                Comment

                Working...