日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 日志打印

發(fā)布時間:2025/1/21 python 94 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 日志打印 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

發(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!")

轉(zhuǎn)載于:https://www.cnblogs.com/soft115/archive/2011/08/10/2134081.html

總結(jié)

以上是生活随笔為你收集整理的python 日志打印的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。