当前位置:首页 » 编程语言 » logpython

logpython

发布时间: 2022-02-10 03:24:17

python 将运行结果保存到log文件中遇到的问题

这个简单啊。通过管道输出到另一个程序里去,另外一个程序同时打印并输出到out.log

这是我常用的办法。

还有一个笨办法,用自己的myprint代替所有的print语句。这样你可以同时输出到屏幕与文件了。

第三个办法是通过logging。这个模块支持多个listener,可以同时输出到屏幕与文件以及网络。

Ⅱ python下比较好使的log模块有哪些

Python只宜用其读取和写入EXCEL数据,并不宜用它去控制Excel Excel本身功能强大,也用不着用Python去帮忙 只有数据量特别大小,用python才有优势

Ⅲ python的log可以像log4j一样发邮件吗

可以,需要自己写发邮件的代码,用smtplib就行。

Ⅳ 有几千个TXT格式的log文件,怎么用Python批量提取每个log文件固定位置的具体数值,输出到一个TXT文件

1.将这些TXT文件先合并
2.找到这些具体数值的特征
3.用正则表达式过滤提取
这阵子正学习python,不介意可以发给我,练着玩

Ⅳ 如何用python提取log文件中的特定字符串和数字

一般用正则表达式提取

Ⅵ python里怎模对复数取对数log,对矩阵实现log以3为底,怎么做

#coding=utf-8
importmath
importnumpyasnp

#复数
aComplex=4.23+8.5j
#对复数的实数部分取对数
math.log(aComplex.real)
#对复数的虚数部分取对数
math.log(aComplex.imag)

#矩阵
aArray=np.array([1.0,2.0,3.0])
#对矩阵求log以3为底
foriinrange(len(aArray)):
aArray[i]=math.log(aArray[i],3)

Ⅶ 怎么把python运行结果保存到log

通过管道输出到另一个程序里去,另外一个程序同时打印并输出到out.log。

用自己的myprint代替所有的print语句。这样你可以同时输出到屏幕与文件了。

通过logging。这个模块支持多个listener,可以同时输出到屏幕与文件以及网络。

Ⅷ 怎么把python运行结果保存到log

python test.py >1.log
将输出结果记录到1.log(覆盖写入)
python test.py >>1.log
将输出结果追加到1.log(每次追加)

Ⅸ python中log info 是什么文件

a. 利用sys.stdout将print行导向到你定义的日志文件中,例如:

import sys# make a of original stdout routestdout_backup = sys.stdout# define the log file that receives your log infolog_file = open("message.log", "w")# redirect print output to log filesys.stdout = log_fileprint "Now all print info will be written to message.log"# any command line that you will execute...

log_file.close()# restore the output to initial patternsys.stdout = stdout_backupprint "Now this will be presented on screen"

b. 利用logging模块(规范化日志输出,推荐!!)
由于logging模块的功能比较多,下面就放一些文档里介绍的简单的例子,更详细具体的用法请戳这里

需求

最好的实现方式

故障调查或者状态监测 logging.info()或logging.debug()(后者常用于针对性检测诊断的目的)

特定运行事件发出警告 logging.warning()

报告错误抑制不出发异常(常见于长时间运行的服务器进程的错误处理程序) logging.error(), logging.exception()或者logging.critical()

而以下是根据事件的严重性程度而应采取的logging函数的建议:

程度

使用场景

DEBUG 获得诊断问题是具体的信息

INFO 确认程序是否按正常工作

WARNING 在程序还正常运行时获取发生的意外的信息,这可能会在之后引发异常(例如磁盘空间不足)

ERROR 获取程序某些功能无法正常调用这类严重异常的信息

CRITICAL 获取程序无法继续运行的这类最严重异常信息

默认的等级是WARNING,也就是说logging函数在没有特别配置的前提下只追踪比WARNING程度更严重的异常。

下面就用一些例子来具体说明如何用logging函数记录日志信息:

# this is a simple exampleimport logging# define the log file, file mode and logging levellogging.basicConfig(filename='example.log', filemode="w", level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

查看example.log文件可以看到以下信息:

DEBUG:root:This message should go to the log fileINFO:root:So should thisWARNING:root:And this, too

从多个文件记录日志

# myapp.pyimport loggingimport mylibdef main():
logging.basicConfig(filename='myapp.log', level=logging.INFO)
logging.info('Started')
mylib.do_something()
logging.info('Finished')if __name__ == '__main__':
main()
# mylib.pyimport loggingdef do_something():
logging.info('Doing something')

输出的信息为

INFO:root:StartedINFO:root:Doing somethingINFO:root:Finished

改变默认输出信息的格式

import logging# output format: output time - logging level - log messageslogging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
logging.warning('This message will appear in python console.')

在python console中直接打印以下输出:

2016-8-2 2:59:11, 510 - WARNING - This message will appear in python console

logging高级用法
可以通过构建logger或者读取logging config文件对logging函数进行任意配置。

  • 构建logger法

  • import logging# create loggerlogger = logging.getLogger('simple_example')

  • logger.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler()

  • ch.setLevel(logging.DEBUG)# create formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)# 'application' codelogger.debug('debug message')

  • logger.info('info message')

  • logger.warn('warn message')

  • logger.error('error message')

  • logger.critical('critical message')

  • 以上程序结果会输出到python console中:

  • $ python simple_logging_mole.py2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message2005-03-19 15:10:26,620 - simple_example - INFO - info message2005-03-19 15:10:26,695 - simple_example - WARNING - warn message2005-03-19 15:10:26,697 - simple_example - ERROR - error message2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message

  • 读取配置文件法

  • import loggingimport logging.config


  • logging.config.fileConfig('logging.conf')# create loggerlogger = logging.getLogger('simpleExample')# 'application' codelogger.debug('debug message')

  • logger.info('info message')

  • logger.warn('warn message')

  • logger.error('error message')

  • logger.critical('critical message')

  • 其中logging.conf文件格式为:(其实就是将前一种方法的各项配置参数写到logging.conf文件中)

  • [loggers]

  • keys=root,simpleExample


  • [handlers]

  • keys=consoleHandler


  • [formatters]

  • keys=simpleFormatter


  • [logger_root]

  • level=DEBUG

  • handlers=consoleHandler


  • [logger_simpleExample]

  • level=DEBUG

  • handlers=consoleHandler

  • qualname=simpleExample

  • propagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt= '%m/%d/%Y %I:%M:%S %p'

  • 与前面一样,上述文件输出结果为:

  • $ python simple_logging_config.py2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message2005-03-19 15:38:55,979 - simpleExample - INFO - info message2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message2005-03-19 15:38:56,055 - simpleExample - ERROR - error message2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message

Ⅹ python log中文乱码

Python log出现中文乱码的解决方法:修改“handle”的“encode”参数为“utf-8”,即在源码中修改“encoding='utf-8'”。

python log写入中文乱码,直接修改handle的encode参数为utf-8
即在源码中修改encoding='utf-8',因为 logging.basicConfig() 配置时实际上是用到了4大组件,只不过给了默认值,在loging.FileHandler()方法中默认是这样的。

只需在源码中修改。
推荐课程:零基础入门学习Python(小甲鱼)

热点内容
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:333
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:374
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:609
java用什么软件写 发布:2025-05-18 03:56:19 浏览:30
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:105
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:940
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:736
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:800
网卡访问 发布:2025-05-18 03:35:04 浏览:507
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:369