| #-*- coding: GBK -*-  | 
| #LOG¶¨ÒåÄ£¿é  | 
| # author: Alee  | 
| # Date: 2011.9.1  | 
| # history:  2011.9.1     Created  | 
|   | 
| import logging  | 
| from time import localtime  | 
| import os  | 
|   | 
| def InitMyLog( LogName, addConsule = False, LEV = logging.DEBUG ):  | 
|     # set up logging to file - see previous section for more details  | 
|     now = localtime()  | 
|     filePath = r"D:\ServerLog\InsideRobot"  | 
|     fileName = filePath + r"\%s-%d-%d-%d.log" % ( LogName, now.tm_year, now.tm_mon, now.tm_mday )  | 
|     if not os.path.exists(filePath):  | 
|         os.makedirs(filePath)  | 
|     logging.basicConfig(level=LEV,  | 
|                         format='%(name)-12s %(asctime)s %(name)-12s %(levelname)-8s %(message)s',  | 
|                         datefmt='%Y-%m-%dT%H:%M:%S',  | 
|                         filename=fileName,  | 
|                         filemode='a+')  | 
|     if addConsule:  | 
|         # define a Handler which writes INFO messages or higher to the sys.stderr  | 
|         console = logging.StreamHandler()  | 
|         console.setLevel(logging.DEBUG)  | 
|         # set a format which is simpler for console use  | 
|         formatter = logging.Formatter('%(name)-12s %(asctime)s %(name)-12s %(levelname)-8s %(message)s')  | 
|         # tell the handler to use this format  | 
|         console.setFormatter(formatter)  | 
|         # add the handler to the root logger  | 
|         logging.getLogger('').addHandler(console)  | 
|   | 
|   | 
| def test():  | 
|     InitMyLog()  | 
|         # Now, we can log to the root logger, or any other logger. First the root...  | 
|     logging.info('Jackdaws love my big sphinx of quartz.')  | 
|   | 
|     # Now, define a couple of other loggers which might represent areas in your  | 
|     # application:  | 
|   | 
|     logger1 = logging.getLogger('myapp.area1')  | 
|     logger2 = logging.getLogger('myapp.area2')  | 
|   | 
|     logger1.debug('Quick zephyrs blow, vexing daft Jim.')  | 
|     logger1.info('How quickly daft jumping zebras vex.')  | 
|     logger2.warning('Jail zesty vixen who grabbed pay from quack.')  | 
|     logger2.error('The five boxing wizards jump quickly.')  | 
|   | 
| if __name__ == '__main__':  | 
|     test()  | 
|   |