import logging
logfile = 'e:\\a.txt'
# logging.basicConfig(filename=logfile,level=logging.INFO)
# logging.basicConfig(format='%(time.asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.basicConfig(level=logging.INFO,
#format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', #返回值:Thu, 26 May 2016 15:09:31 t11.py[line:92] INFO
format='%(asctime)s %(levelname)s %(message)s',
#datefmt='%a, %d %b %Y %H:%M:%S',
#datefmt='%Y/%m/%d %I:%M:%S %p', #返回2016/05/26 03:12:56 PM
datefmt='%Y-%m-%d %H:%M:%S', #返回2016/05/26 03:12:56 PM
filename=logfile#,
#filemode='a' #默認為a
)
logging.info('username valid passed.\r') #logging會自動在每行log后面添加"\000"換行,windows下未自動換行
#logging輸出結果:
#2016-05-26 15:22:29 INFO liu1 valid passed.
#2016-05-26 15:22:37 INFO liu1 valid passed.
參考:http://blog.chinaunix.net/uid-26000296-id-4372063.html
http://www.cnblogs.com/alex3714/articles/5161349.html
日志級別等級:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET
format參數中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 數字形式的日志級別
%(levelname)s 文本形式的日志級別
%(pathname)s 調用日志輸出函數的模塊的完整路徑名,可能沒有
%(filename)s 調用日志輸出函數的模塊的文件名
%(module)s 調用日志輸出函數的模塊名
%(funcName)s 調用日志輸出函數的函數名
%(lineno)d 調用日志輸出函數的語句所在的代碼行
%(created)f 當前時間,用UNIX標准的表示時間的浮 點數表示
%(relativeCreated)d 輸出日志信息時的,自Logger創建以 來的毫秒數
%(asctime)s 字符串形式的當前時間。默認格式是 “2003-07-08 16:49:45,896”。逗號后面的是毫秒
%(thread)d 線程ID。可能沒有
%(threadName)s 線程名。可能沒有
%(process)d 進程ID。可能沒有
%(message)s 用戶輸出的消息
參考鏈接:https://www.cnblogs.com/dreamer-fish/p/5460929.html

