發(fā)現(xiàn)python里面擴展了日志打印功能。感覺比java自帶的還好,和log4j很類似。
          下面總結(jié)下其用法。先說一種不用配置文件的:
          <log.py>

          ?1#?-*-?coding:?gb2312?-*-
          ?2import?logging
          ?3
          ?4logging.basicConfig(level=logging.INFO,
          ?5????????????????????format='%(asctime)s?%(name)-12s?%(levelname)-8s?%(message)s',
          ?6????????????????????datefmt='%m-%d?%H:%M',
          ?7????????????????????filename='./AutoUpdate.log',
          ?8????????????????????filemode='w')
          ?9
          10console?=?logging.StreamHandler()
          11console.setLevel(logging.INFO)
          12formatter?=?logging.Formatter('%(name)-12s:?%(levelname)-8s?%(message)s')
          13console.setFormatter(formatter)
          14logging.getLogger('').addHandler(console)
          15
          16
          17##?
          18#?console?=?logging.StreamHandler()
          19#?console?=?setLevel(logging.DEBUG)
          20#?formatter?=?logging.Formatter('%(name)-12s:?%(levelname)-8s?%(message)s')
          21#?console.setFormatter(formatter)
          22#?logging.getLogger('').addHandler(console)
          23
          24
          25def?getLogging(name):
          26????return?logging.getLogger(name)

          <test.py>
          import?log

          testlog?
          =?log.getLogging('test')
          testlog.error(?
          "~~~~~~~~~~~~~~test1.")
          testlog.info(?
          "~~~~~~~~~~~~~~test2."?)
          testlog.exception(?
          "~~~~~~~~~~~~~~~~~~~~~~~~~test3.")

          用過log4j的人不會陌生這種用法。如果你對log4j有疑惑,或者想了解請在本人blog內(nèi)查找關(guān)于log4j的文章。有詳細介紹。這里不再贅述。

          第二種使用配置文件的:
          #?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',?10000,?9)

          <test2.py>
          import?logging
          import?logging.config

          logging.config.fileConfig(
          "logger.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!")

          其中值得注意的是args的含義。
          posted on 2008-04-10 22:25 -274°C 閱讀(3332) 評論(0)  編輯  收藏 所屬分類: python

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 917238
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 互助| 翼城县| 盐亭县| 灵石县| 铅山县| 佳木斯市| 海伦市| 宿松县| 勐海县| 仁布县| 治县。| 隆尧县| 汉川市| 区。| 久治县| 华坪县| 广平县| 新沂市| 唐河县| 诸城市| 大田县| 常州市| 广宗县| 庆城县| 称多县| 蓝山县| 栾川县| 苏尼特左旗| 界首市| 特克斯县| 和硕县| 安西县| 田东县| 大同市| 禹城市| 旺苍县| 金川县| 西乌珠穆沁旗| 庆云县| 南充市| 镶黄旗|