简体   繁体   English

记录 function 在 python 中返回 object 的确切时间

[英]Logging the exact time that a function returns an object in python

I have a function that creates and returns a dataframe in python:我有一个 function 在 python 中创建并返回一个 dataframe:

import logging
import pandas as pd
from pandas import util 

def some_function():
    
    logging.info('creating foobar')
    foobar = util.testing.makeDataFrame()
    
    return foobar

What is the best method for logging the precise time that foobar is returned?记录foobar返回的精确时间的最佳方法是什么?

The easiest way to accomplish what you're trying to do is to just configure the logging object, like so:完成您想要做的最简单的方法是配置日志记录 object,如下所示:

import logging

logging.basicConfig(
    format="[%(asctime)s.%(msecs)03d] %(levelname)s [%(thread)d] - %(message)s", 
    level=logging.DEBUG, 
    datefmt="%Y-%m-%d %H:%M:%S",
    stream=sys.stderr
)

logging.info('creating foobar')

That way your log will automatically log the timestamp for every single log you write, and you wont need to add that in on every single log you write up.这样,您的日志将自动记录您编写的每条日志的时间戳,并且您无需将其添加到您编写的每条日志中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM