Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 09:35 AM
Kattoo Kattoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 Kattoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 35 sec
Reputation 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.

Reply With Quote
  #2  
Old November 15th, 2012, 10:53 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 15th, 2012, 11:53 AM
Kattoo Kattoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 Kattoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 35 sec
Reputation 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.

Reply With Quote
  #4  
Old November 15th, 2012, 12:46 PM
Kattoo Kattoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 Kattoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 35 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Logging class question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap