Python3 使用标准库打印日志

本人刚学python,发现print() 函数只能打印内容,不能像java的System.out.println()那样打印(含时间、线程、类、行等细信息),故自己写了个print_info()的方法。大神绕道,不喜勿喷,谢谢!!!!



# -*- coding:utf-8 -*-
import time
import sys
import threading

def print_info(message):
    # dept = 1 表示找到该函数第一层调用者的位置信息
    # dept = 0 表示该函数定义的位置信息
    frame = sys._getframe(1)
    code = frame.f_code

    # print("func name = ", code.co_name)
    # print("func filename = ", code.co_filename)
    # print("func import lineno = ", code.co_firstlineno)  # 模块导入该函数的位置
    # print("func call lineno = ", frame.f_lineno)  # 模块调用该函数的位置
    # print("func locals = ", frame.f_locals)

    thread_name = threading.currentThread().getName()
    ident = threading.currentThread().ident
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), ">>> [", thread_name, "-", ident, "] (", code.co_filename, "@", frame.f_lineno, "line)", ":", message)

 

 

 

调用print_info()函数:

from com.diyun.python import print_info

import logging



if __name__ == "__main__":
    print_info("hello world")

 

 

打印效果如下:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值