金慶的專欄
C++博客
::
首頁
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
423 隨筆 :: 0 文章 :: 454 評(píng)論 :: 0 Trackbacks
公告
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
(12)
給我留言
查看公開留言
查看私人留言
隨筆分類
(502)
1. C/C++(166)
(rss)
2. 網(wǎng)游開發(fā)(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)
相冊(cè)
公告照片
搜索
積分與排名
積分 - 656618
排名 - 25
最新評(píng)論
1.?re: boost::asio::spawn 將一統(tǒng)C++網(wǎng)絡(luò)庫
asio 成為C++首選網(wǎng)絡(luò)庫
--linda
2.?re: log4cxx中文輸出錯(cuò)誤補(bǔ)丁
評(píng)論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--金慶
3.?re: mingw編譯OrzNet
能發(fā)送一個(gè)mingw編譯好的OrzNet庫給我嗎? liuweiqcxy@163.com
謝謝!
--劉威
4.?re: log4cxx中文輸出錯(cuò)誤補(bǔ)丁
評(píng)論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--bigbad
5.?re: log4cxx中文輸出錯(cuò)誤補(bǔ)丁
評(píng)論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--bigbad
閱讀排行榜
1.?"multiple definition of" 錯(cuò)誤(11032)
2.?SVN中邪惡的replace(10955)
3.?VS2005編譯libevent(10413)
4.?混音算法的學(xué)習(xí)與研究(10203)
5.?C調(diào)用lua腳本的效率測(cè)試(9007)
評(píng)論排行榜
1.?VC6正在被拋棄(35)
2.?VS2005編譯libevent(21)
3.?"multiple definition of" 錯(cuò)誤(18)
4.?C++引用優(yōu)于指針(17)
5.?ACE與ASIO之間關(guān)于Socket編程的比較(16)
使用Python的logging.config
使用Python的logging.config
Python的logging模塊接口仿log4j,概念上一致,使用上相當(dāng)方便。
利用logging.config.fileConfig(),可以將日志的配置用文件來描述,簡(jiǎn)化了日志的初始化。
例程:
#
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()會(huì)因?yàn)閞ename()出錯(cuò)而中途退出,造成日志文件沒有打開,并且后繼的日志消息都因?yàn)槿罩疚募]有打開而失敗。可以自己在rename()處加上try,或者不用RotatingFileHandler。估計(jì)隔一段時(shí)間就fileConfig()一次也是可以恢復(fù)正常。
詳見:
Python logging RotatingFileHandler bug
該錯(cuò)誤僅當(dāng)日志文件滿時(shí)切換文件時(shí)才可能發(fā)生,當(dāng)文件被鎖定時(shí)才會(huì)出錯(cuò)。正常使用不會(huì)有問題,并且日志出錯(cuò)不會(huì)影響主程序的運(yùn)行。所以可以放心使用,想再可靠點(diǎn)就直接在源碼中加個(gè)try.
(轉(zhuǎn)載請(qǐng)注明來源于金慶的專欄)
posted on 2007-09-03 11:35
金慶
閱讀(6409)
評(píng)論(6)
編輯
收藏
引用
所屬分類:
6. Python
評(píng)論
#
請(qǐng)教一下:
2007-10-30 10:19
APACHE
現(xiàn)在我有個(gè)問題想問一下,我打開一個(gè)LOG文件并輸出,最后怎么關(guān)閉這個(gè)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)
然后運(yùn)行:
from logtest import CLogInfo,c1
log = CLogInfo("1.log")
log.output("aaa right")
這些都正確,但是
log.close()就提示錯(cuò)誤,有知道如何關(guān)閉log文件的么?
回復(fù)
更多評(píng)論
#
ok
2007-10-30 10:39
APACHE
I have find it:
self.logger.removeHandler(self.hdlr)
回復(fù)
更多評(píng)論
#
re: 使用Python的logging.config
2007-10-30 11:07
金慶
@APACHE
我試了一下是好用的,沒有錯(cuò)誤,除了logging,error未定義。
你最好給出錯(cuò)誤信息。
logging.shutdown()是關(guān)閉所有處理器。
用self.hdlr.close()也可以。
回復(fù)
更多評(píng)論
#
re: 使用Python的logging.config
2008-07-25 17:03
feinix
我有個(gè)問題想問一下,我想用log直接寫進(jìn)數(shù)據(jù)庫
看見網(wǎng)上的例子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()
不知道怎么錯(cuò)誤了……因?yàn)槲也恢溃琇og的信息什么的由哪里傳遞出來。
回復(fù)
更多評(píng)論
#
re: 使用Python的logging.config
2008-07-25 18:26
金慶
@feinix
錯(cuò)誤信息呢?還要給出原例子的地址,看看你改了什么?
回復(fù)
更多評(píng)論
#
re: 使用Python的logging.config
2008-07-26 16:22
feinix
@金慶
多謝,我已經(jīng)找到了,是那個(gè)record,它帶有相關(guān)的屬性值。
我想知道,這里邊的config和log程序的編寫,有什么關(guān)系么?可以用設(shè)置來代替編寫?而且你寫這個(gè)日志程序怎么使用?在每個(gè)需要的模塊中直接調(diào)用?
回復(fù)
更多評(píng)論
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
vs2017 linux 編譯輸出改成 vs 格式
xlsx批量轉(zhuǎn)為utf8的csv
如何運(yùn)行 rpcz python example
Windows上Python讀取stdin出錯(cuò)
建立Socket Policy服務(wù)器
python計(jì)算24點(diǎn)
(Python編程)Pickle對(duì)象
Boost.Python中文文檔下載
用Boost.Python構(gòu)建混合系統(tǒng)
Python封裝的性能研究
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © 金慶
久久91精品国产91久久小草
|
亚洲精品tv久久久久
|
99久久精品费精品国产一区二区
|
99国产精品久久久久久久成人热
|
国产成年无码久久久久毛片
|
国内精品久久久久久中文字幕
|
久久久久女人精品毛片
|
无码人妻久久一区二区三区免费
|
久久线看观看精品香蕉国产
|
人妻无码精品久久亚瑟影视
|
热久久这里只有精品
|
国产成人精品综合久久久久
|
99久久精品国产综合一区
|
日本人妻丰满熟妇久久久久久
|
精品久久人人妻人人做精品
|
亚洲AV日韩精品久久久久久
|
久久亚洲精品无码观看不卡
|
久久综合中文字幕
|
国产精品久久久久久影院
|
精品久久久久久国产
|
欧美久久久久久
|
久久亚洲国产成人影院网站
|
亚洲国产婷婷香蕉久久久久久
|
2021少妇久久久久久久久久
|
欧洲精品久久久av无码电影
|
无码任你躁久久久久久久
|
香港aa三级久久三级
|
国产成人精品久久一区二区三区
|
亚洲国产精品无码久久98
|
热久久最新网站获取
|
久久亚洲国产最新网站
|
亚洲国产成人久久一区久久
|
无码精品久久一区二区三区
|
久久久艹
|
四虎影视久久久免费
|
久久午夜综合久久
|
久久久久亚洲AV综合波多野结衣
|
亚洲AV伊人久久青青草原
|
无码8090精品久久一区
|
久久99九九国产免费看小说
|
狠狠色狠狠色综合久久
|