python写日志

Posted 咻_python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python写日志相关的知识,希望对你有一定的参考价值。

写日志的办法多种多样,我这个是我喜欢的办法,可以做个参考

没啥说的,直接上代码

import time

def write_log(value):
  now_time = time.time() #获取当前日期和时间
  time_format = %Y-%m-%d %H:%M:%S #指定日期和时间格式
  time_put = time.strftime(time_format,now_time) #格式化时间,时间变成YYYY-MM-DD HH:MI:SS
  file_name = /log/log.log
  log_file = open(file_name,a) #这里用追加模式,如果文件不存在的话会自动创建
  write_value = %s %s %(now_time, value)
  log_file.write(write_value)
  log_file.close()

if __name__ == __main__:
  value = insert something into log_file
  write_log(value)

 

以上是关于python写日志的主要内容,如果未能解决你的问题,请参考以下文章

Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段

argparse 代码片段只打印部分日志

python logging 重复写日志问题

python分析apache和nginx日志文件输出访客ip列表的代码

python logging 重复写日志问题

一日一技:更优雅地在 Python 中写日志