金慶的專欄
C++博客
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
::
423 隨筆 :: 0 文章 :: 454 評論 :: 0 Trackbacks
公告
我的隨筆
我的評論
我參與的隨筆
留言簿
(12)
給我留言
查看公開留言
查看私人留言
隨筆分類
(502)
1. C/C++(166)
(rss)
2. 網游開發(108)
(rss)
3. Golang(20)
(rss)
4. Linux/Unix(30)
(rss)
5. 軟工與管理(44)
(rss)
6. Python(23)
(rss)
7. Erlang(18)
(rss)
8. Rust(16)
(rss)
9. 其它(77)
(rss)
隨筆檔案
(423)
2023年1月 (1)
2022年11月 (1)
2022年10月 (2)
2022年9月 (1)
2022年4月 (6)
2022年1月 (2)
2021年12月 (4)
2021年11月 (6)
2021年10月 (2)
2021年9月 (2)
2021年8月 (7)
2021年7月 (2)
2021年5月 (2)
2021年3月 (1)
2021年2月 (2)
2021年1月 (1)
2020年12月 (1)
2020年10月 (1)
2020年9月 (5)
2020年8月 (1)
2020年7月 (1)
2020年6月 (1)
2020年4月 (2)
2020年3月 (3)
2020年2月 (3)
2020年1月 (1)
2019年12月 (1)
2019年9月 (2)
2019年4月 (2)
2019年1月 (1)
2018年12月 (1)
2018年11月 (3)
2018年10月 (1)
2018年9月 (3)
2018年8月 (3)
2018年7月 (2)
2018年6月 (4)
2018年5月 (4)
2018年4月 (4)
2018年3月 (1)
2018年1月 (2)
2017年12月 (2)
2017年11月 (3)
2017年10月 (3)
2017年8月 (7)
2017年7月 (1)
2017年6月 (1)
2017年5月 (3)
2017年4月 (3)
2017年3月 (3)
2017年2月 (2)
2017年1月 (2)
2016年12月 (5)
2016年11月 (2)
2016年10月 (2)
2016年9月 (1)
2016年8月 (6)
2016年7月 (3)
2016年6月 (2)
2016年5月 (4)
2016年4月 (2)
2016年3月 (2)
2016年1月 (3)
2015年12月 (2)
2015年11月 (2)
2015年10月 (1)
2015年8月 (2)
2015年7月 (1)
2015年6月 (1)
2015年5月 (4)
2015年4月 (3)
2015年3月 (4)
2015年2月 (5)
2015年1月 (4)
2014年12月 (3)
2014年11月 (3)
2014年10月 (2)
2014年9月 (3)
2014年8月 (1)
2014年4月 (4)
2014年3月 (1)
2014年2月 (4)
2014年1月 (5)
2013年12月 (5)
2013年11月 (5)
2013年9月 (2)
2013年8月 (2)
2013年7月 (2)
2013年6月 (2)
2013年5月 (1)
2013年1月 (2)
2012年12月 (1)
2012年11月 (1)
2012年9月 (1)
2012年8月 (3)
2012年7月 (2)
2012年6月 (1)
2012年4月 (3)
2012年3月 (2)
2012年2月 (3)
2012年1月 (2)
2011年11月 (2)
2011年10月 (3)
2011年9月 (2)
2011年8月 (2)
2011年7月 (3)
2011年6月 (2)
2011年5月 (3)
2011年1月 (2)
2010年12月 (1)
2010年11月 (2)
2010年10月 (2)
2010年9月 (3)
2010年8月 (2)
2010年7月 (3)
2010年6月 (1)
2010年5月 (3)
2010年4月 (3)
2010年3月 (5)
2010年2月 (4)
2010年1月 (4)
2009年12月 (2)
2009年11月 (3)
2009年10月 (4)
2009年9月 (3)
2009年8月 (2)
2009年7月 (4)
2009年6月 (1)
2009年5月 (3)
2009年4月 (4)
2009年3月 (2)
2009年2月 (5)
2009年1月 (1)
2008年12月 (7)
2008年11月 (4)
2008年10月 (1)
2008年9月 (3)
2008年8月 (4)
2008年7月 (3)
2008年6月 (4)
2008年5月 (6)
2008年4月 (7)
2008年3月 (6)
2008年1月 (5)
2007年12月 (7)
2007年11月 (4)
2007年10月 (5)
2007年9月 (6)
2007年8月 (8)
2007年7月 (5)
相冊
公告照片
搜索
積分與排名
積分 - 659036
排名 - 25
最新評論
1.?re: boost::asio::spawn 將一統C++網絡庫
asio 成為C++首選網絡庫
--linda
2.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--金慶
3.?re: mingw編譯OrzNet
能發送一個mingw編譯好的OrzNet庫給我嗎? liuweiqcxy@163.com
謝謝!
--劉威
4.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--bigbad
5.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--bigbad
閱讀排行榜
1.?"multiple definition of" 錯誤(11054)
2.?SVN中邪惡的replace(10972)
3.?VS2005編譯libevent(10429)
4.?混音算法的學習與研究(10230)
5.?C調用lua腳本的效率測試(9019)
評論排行榜
1.?VC6正在被拋棄(35)
2.?VS2005編譯libevent(21)
3.?"multiple definition of" 錯誤(18)
4.?C++引用優于指針(17)
5.?ACE與ASIO之間關于Socket編程的比較(16)
使用Python的logging.config
使用Python的logging.config
Python的logging模塊接口仿log4j,概念上一致,使用上相當方便。
利用logging.config.fileConfig(),可以將日志的配置用文件來描述,簡化了日志的初始化。
例程:
#
test.py
import
logging
import
logging.config
logging.config.fileConfig(
"
logging.conf
"
)
#
create logger
logger
=
logging.getLogger(
"
example
"
)
#
"application" code
logger.debug(
"
debug message
"
)
logger.info(
"
info message
"
)
logger.warn(
"
warn message
"
)
logger.error(
"
error message
"
)
logger.critical(
"
critical message
"
)
logHello
=
logging.getLogger(
"
hello
"
)
logHello.info(
"
Hello world!
"
)
配置文件示例如下:
# logging.conf
[
loggers
]
keys
=
root
,
example
[
handlers
]
keys
=
consoleHandler
,
rotateFileHandler
[
formatters
]
keys
=
simpleFormatter
[
formatter_simpleFormatter
]
format
=
[
%(asctime)s
]
(%(levelname)s)%(name)s : %(message)s
[
logger_root
]
level
=
DEBUG
handlers
=
consoleHandler
,
rotateFileHandler
[
logger_example
]
level
=
DEBUG
handlers
=
consoleHandler
,
rotateFileHandler
qualname
=
example
propagate
=
0
[
handler_consoleHandler
]
class
=
StreamHandler
level
=
DEBUG
formatter
=
simpleFormatter
args
=
(sys.stdout
,
)
[
handler_rotateFileHandler
]
class
=
handlers.RotatingFileHandler
level
=
DEBUG
formatter
=
simpleFormatter
args
=
('test.log'
,
'a'
,
200000
,
9
)
注意,RotatingFileHandler中doRollover()會因為rename()出錯而中途退出,造成日志文件沒有打開,并且后繼的日志消息都因為日志文件沒有打開而失敗。可以自己在rename()處加上try,或者不用RotatingFileHandler。估計隔一段時間就fileConfig()一次也是可以恢復正常。
詳見:
Python logging RotatingFileHandler bug
該錯誤僅當日志文件滿時切換文件時才可能發生,當文件被鎖定時才會出錯。正常使用不會有問題,并且日志出錯不會影響主程序的運行。所以可以放心使用,想再可靠點就直接在源碼中加個try.
(轉載請注明來源于金慶的專欄)
posted on 2007-09-03 11:35
金慶
閱讀(6414)
評論(6)
編輯
收藏
引用
所屬分類:
6. Python
評論
#
請教一下:
2007-10-30 10:19
APACHE
現在我有個問題想問一下,我打開一個LOG文件并輸出,最后怎么關閉這個LOG文件呢?shutdown?
代碼如下:
class CLogInfo:
#output the information to log,it will be instanced in testcase modele.
def __init__(self,logfile="testcase.log"):
try:
import logging
self.logger = None
self.logger = logging.getLogger()
self.hdlr = logging.FileHandler(logfile)
formatter = logging.Formatter("[%(asctime)s]: %(message)s","%Y-%m-%d %H:%M:%S")
self.hdlr.setFormatter(formatter)
self.logger.addHandler(self.hdlr)
self.logger.setLevel(logging.DEBUG)
except:
print "log init error!"
#exit(1)
def output(self,logInfo,errFlag=0):
try:
if (errFlag):
self.logger.error("error:"+logInfo)
#exit(errFlag)
else:
self.logger.info(logInfo)
except:
print "log output error!"
#exit(1)
def close(self):
try:
logging.shutdown([self.hdlr])
except error:
print "log closed error!"
#exit(1)
然后運行:
from logtest import CLogInfo,c1
log = CLogInfo("1.log")
log.output("aaa right")
這些都正確,但是
log.close()就提示錯誤,有知道如何關閉log文件的么?
回復
更多評論
#
ok
2007-10-30 10:39
APACHE
I have find it:
self.logger.removeHandler(self.hdlr)
回復
更多評論
#
re: 使用Python的logging.config
2007-10-30 11:07
金慶
@APACHE
我試了一下是好用的,沒有錯誤,除了logging,error未定義。
你最好給出錯誤信息。
logging.shutdown()是關閉所有處理器。
用self.hdlr.close()也可以。
回復
更多評論
#
re: 使用Python的logging.config
2008-07-25 17:03
feinix
我有個問題想問一下,我想用log直接寫進數據庫
看見網上的例子14
改為
# vim: set fileencoding=<encoding name> :
#!/usr/bin/env python
#coding=utf-8
#import tableE
from sqlobject import *
#from sqlobject import SQLObject
import sys, string, time, logging
connection=connectionForURI('sqlite:/d|/fff.db')
sqlhub.processConnection = connection
class DBHandler(logging.Handler):
def __init__(self, dsn, uid='', pwd=''):
logging.Handler.__init__(self)
#import mx.ODBC.Windows
class execptionsf(SQLObject):
Created= DateTimeCol
Name= StringCol()
LogLevel= StringCol()
LevelText= StringCol()
Message= StringCol()
Filename= StringCol()
Pathname= StringCol()
Lineno= StringCol()
Milliseconds= StringCol()
Exception= StringCol()
Thread= StringCol()
execptionsf.createTable(ifNotExists=True)
#def _set_Execptions(self,Created,RelativeCreated,Name,LogLevel,LevelText,Message,Filename,Pathname,Lineno,Milliseconds,Exception,Thread):
#self.dsn = dsn
#self.uid = uid
#self.pwd = pwd
#self.conn = mx.ODBC.Windows.connect(self.dsn, self.uid, self.pwd)
#self.SQL =
"""INSERT INTO Events (
Created,
RelativeCreated,
Name,
LogLevel,
LevelText,
Message,
Filename,
Pathname,
Lineno,
Milliseconds,
Exception,
Thread
)
VALUES (
%(dbtime)s,
%(relativeCreated)d,
'%(name)s',
%(levelno)d,
'%(levelname)s',
'%(message)s',
'%(filename)s',
'%(pathname)s',
%(lineno)d,
%(msecs)d,
'%(exc_text)s',
'%(thread)s'
);
"""
#self.cursor = self.conn.cursor()
def formatDBTime(self, record):
record.dbtime = time.strftime("#%m/%d/%Y#", time.localtime(record.created))
def emit(self, record):
try:
#use default formatting
self.format(record)
#now set the database time up
self.formatDBTime(record)
if record.exc_info:
record.exc_text = logging._defaultFormatter.formatException(record.exc_info)
else:
record.exc_text = ""
execptionsf.Created= %(dbtime)s
execptionsf.RelativeCreated=Created
execptionsf.Name= '%(name)s'
execptionsf.LogLevel= '%(levelno)s'
execptionsff.LevelText= '%(levelname)s'
execptionsf.Message= '%(message)s'
execptionsf.Filename= '%(filename)s'
execptionsf.Pathname= '%(pathname)s'
execptionsf.Lineno= '%(lineno)s'
execptionsf.Milliseconds= '%(msecs)s'
execptionsf.Exception= '%(exc_text)s'
execptionsf.Thread= '%(thread)s'
#sql = self.SQL % record.__dict__
#self.cursor.execute(sql)
#self.conn.commit()
except:
import traceback
ei = sys.exc_info() #get the tracebak infomation
traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
del ei
def close(self):
#self.cursor.close()
#self.conn.close()
logging.Handler.close(self)
dh = DBHandler('Logging')
logger = logging.getLogger("")
# logger
# logger.setLevel(logging.DEBUG)
logger.addHandler(dh)
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
try:
import math
math.exp(1000)
except:
logger.exception("Problem with %s", "math.exp")
logging.shutdown()
不知道怎么錯誤了……因為我不知道,Log的信息什么的由哪里傳遞出來。
回復
更多評論
#
re: 使用Python的logging.config
2008-07-25 18:26
金慶
@feinix
錯誤信息呢?還要給出原例子的地址,看看你改了什么?
回復
更多評論
#
re: 使用Python的logging.config
2008-07-26 16:22
feinix
@金慶
多謝,我已經找到了,是那個record,它帶有相關的屬性值。
我想知道,這里邊的config和log程序的編寫,有什么關系么?可以用設置來代替編寫?而且你寫這個日志程序怎么使用?在每個需要的模塊中直接調用?
回復
更多評論
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
vs2017 linux 編譯輸出改成 vs 格式
xlsx批量轉為utf8的csv
如何運行 rpcz python example
Windows上Python讀取stdin出錯
建立Socket Policy服務器
python計算24點
(Python編程)Pickle對象
Boost.Python中文文檔下載
用Boost.Python構建混合系統
Python封裝的性能研究
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © 金慶
亚洲精品乱码久久久久久久久久久久
|
国产精品一久久香蕉国产线看观看
|
久久成人18免费网站
|
欧美日韩精品久久久久
|
久久久女人与动物群交毛片
|
区久久AAA片69亚洲
|
亚洲国产精品无码久久一线
|
精品综合久久久久久88小说
|
久久精品国产亚洲av麻豆色欲
|
久久福利片
|
亚洲人成精品久久久久
|
精品乱码久久久久久夜夜嗨
|
久久人人爽人人爽人人AV
|
久久精品国产精品亚洲人人
|
97久久精品无码一区二区天美
|
久久久久久久亚洲精品
|
久久99免费视频
|
久久九九精品99国产精品
|
亚洲国产成人精品91久久久
|
欧美久久天天综合香蕉伊
|
久久久久久国产精品免费无码
|
91精品国产综合久久精品
|
久久婷婷色香五月综合激情
|
久久久久国产视频电影
|
91久久成人免费
|
久久亚洲国产成人精品无码区
|
久久99精品综合国产首页
|
久久亚洲精品成人AV
|
伊人久久无码中文字幕
|
久久久久亚洲国产
|
精品一二三区久久aaa片
|
久久99热这里只有精品66
|
亚洲精品成人久久久
|
国产精品免费久久久久久久久
|
中文字幕日本人妻久久久免费
|
欧美激情精品久久久久久久九九九
|
国产99久久九九精品无码
|
久久精品国产欧美日韩
|
97精品伊人久久大香线蕉
|
中文字幕久久精品无码
|
97热久久免费频精品99
|