#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Logging class question


    The code below gives me the following output:

    2012-11-15 09:04:28,482_pid:6060_DEBUG_ ~ 1 Log
    2012-11-15 09:04:28,496_pid:6060_DEBUG_ ~ 2 Log
    2012-11-15 09:04:28,496_pid:6060_DEBUG_ ~ 2 Log
    2012-11-15 09:04:28,513_pid:6060_DEBUG_ ~ 3 Log
    2012-11-15 09:04:28,513_pid:6060_DEBUG_ ~ 3 Log
    2012-11-15 09:04:28,513_pid:6060_DEBUG_ ~ 3 Log

    but, I am wanting this:
    2012-11-15 09:04:28,482_pid:6060_DEBUG_ ~ 1 Log
    2012-11-15 09:04:28,496_pid:6060_DEBUG_ ~ 2 Log
    2012-11-15 09:04:28,513_pid:6060_DEBUG_ ~ 3 Log

    I'm nearly certain the failure stems from my poor understanding of how to utilize the class.

    -Python 3.3
    Code:
    import os
    import time
    import logging
    
    class BaseLog:
        def Log(self, sToLog):
            #formatter and logger
            formatter = logging.Formatter("%(asctime)s_pid:%(process)d_%(levelname)s_ ~ %(message)s")
            sLogName = "Random_File_Name"
            logger = logging.getLogger(sLogName)
            logger.setLevel(logging.DEBUG)
    
            #file handler
            FileHandler = logging.FileHandler("logs\\" + sLogName + "_" + time.strftime("%Y%m%d", time.localtime()) + ".log")
            FileHandler.setLevel(logging.DEBUG)
            FileHandler.setFormatter(formatter)
            logger.addHandler(FileHandler)
    
            #console handler
            ConsoleHandler = logging.StreamHandler()
            ConsoleHandler.setLevel(logging.DEBUG)
            ConsoleHandler.setFormatter(formatter)
            logger.addHandler(ConsoleHandler)
            logger.debug(sToLog)
        #end def
    #end class
    
    oLog = BaseLog()
    oLog.Log("1 Log")
    oLog.Log("2 Log")
    oLog.Log("3 Log")
    Any advice that gets me closer to resolution would be helpful.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    You added a logger. Then told the logging system to issue a message. You got one message.

    You added another logger, then told the logging system to issue a message. You got two messages, one from each logger.

    You added another logger, then told the logging system to issue a message. You got three messages, one from each logger.

    There's no good reason to tell you this puzzled me for a spell also.
    Code:
    '''
        $ python p.py
    
        logger(1) A Log
    
        logger(1) B Log
        logger(2) B Log
    
        logger(1) C Log
        logger(2) C Log
        logger(3) C Log
    '''
    
    import logging
    
    i = 0
    
    def Log(sToLog):
        global i
        print('')
        logger = logging.getLogger()
        ConsoleHandler = logging.StreamHandler()
        i += 1
        formatter = logging.Formatter('logger(%d) %%(message)s'%i)
        ConsoleHandler = logging.StreamHandler()
        ConsoleHandler.setFormatter(formatter)
        logger.addHandler(ConsoleHandler)
        logger.error(sToLog)
    
    Log('A Log')
    Log('B Log')
    Log('C Log')

    Comments on this post

    • Lux Perpetua agrees : I knew the answer before even reading the code, probably because I have exactly the same bug in my code right now (but due to a more subtle cause: a module being imported & execeuted twice under two different names).
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Thanks, I'll post progress on it later. I'm out of time for logging. I'm moving on to email MIME issues for the afternoon, and will get back to logging tonight.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by Kattoo
    Thanks, I'll post progress on it later. I'm out of time for logging. I'm moving on to email MIME issues for the afternoon, and will get back to logging tonight.
    I lied, I'm eating lunch at my desk... Food does wonders for the brain. I added the lines below to the end of the code and it is working now.

    logger.removeHandler(FileHandler)
    logger.removeHandler(ConsoleHandler)

    weeeeeeeeeeeeeeeee... now it is time for that pesky email MIME lib.

IMN logo majestic logo threadwatch logo seochat tools logo