Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用终端将时间戳输出到日志文件_Python_Bash_Terminal - Fatal编程技术网

Python 使用终端将时间戳输出到日志文件

Python 使用终端将时间戳输出到日志文件,python,bash,terminal,Python,Bash,Terminal,我想为每个输出行在log.txt文件上设置一个时间戳。它正在做我希望它在终端输出中做的事情。如何在log.txt文件中设置时间戳 命令: python ig.py |& tee -a log.txt | ts '[%Y-%m-%d %H:%M:%S]' [2017-04-19 08:38:48] DEBUG:__main__:Liking 1494171280764517709 [2017-04-19 08:38:49] DEBUG:__main__:Sleeping for 33.9

我想为每个输出行在
log.txt
文件上设置一个时间戳。它正在做我希望它在终端输出中做的事情。如何在
log.txt
文件中设置时间戳

命令:

python ig.py |& tee -a log.txt | ts '[%Y-%m-%d %H:%M:%S]'
[2017-04-19 08:38:48] DEBUG:__main__:Liking 1494171280764517709
[2017-04-19 08:38:49] DEBUG:__main__:Sleeping for 33.919725732684476
DEBUG:__main__:Liking 1494171280764517709
DEBUG:__main__:Sleeping for 33.919725732684476
终端输出:

python ig.py |& tee -a log.txt | ts '[%Y-%m-%d %H:%M:%S]'
[2017-04-19 08:38:48] DEBUG:__main__:Liking 1494171280764517709
[2017-04-19 08:38:49] DEBUG:__main__:Sleeping for 33.919725732684476
DEBUG:__main__:Liking 1494171280764517709
DEBUG:__main__:Sleeping for 33.919725732684476
log.txt输出:

python ig.py |& tee -a log.txt | ts '[%Y-%m-%d %H:%M:%S]'
[2017-04-19 08:38:48] DEBUG:__main__:Liking 1494171280764517709
[2017-04-19 08:38:49] DEBUG:__main__:Sleeping for 33.919725732684476
DEBUG:__main__:Liking 1494171280764517709
DEBUG:__main__:Sleeping for 33.919725732684476

ts
移动到
tee
之前,以便在将时间戳写入文件之前将其添加到输出中:

python ig.py |& ts '[%Y-%m-%d %H:%M:%S]' | tee -a log.txt