Linux 如何将日期字符串添加到连续写入的日志文件的每一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6375654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:34:44  来源:igfitidea点击:

How to add date string to each line of a continuously written log file

linuxbashstringloggingcsh

提问by bmk

Having a long running program that continuously writes to a logfile - how is it possible, disregarding any buffering issues, to add a date string to each line written to that file using a linux script?

有一个持续写入日志文件的长时间运行的程序 - 不考虑任何缓冲问题,如何将日期字符串添加到使用 linux 脚本写入该文件的每一行?

I would imagine something like this:

我会想象这样的事情:

tail -f logfile | ADD_DATE_TO_EACH_LINE > logfile2

The input would be something like that:

输入将是这样的:

abc
def
ghi
jkl

The output should be similar to that:

输出应该类似于:

2011-06-16 18:30:59 abc
2011-06-16 18:31:00 def
2011-06-16 18:35:21 ghi
2011-06-16 18:40:15 jkl

采纳答案by Steve Prentice

With perl:

使用 perl:

command 2>&1 | perl -pe 'print scalar(localtime()), " ";'

With gawk:

与呆呆:

command 2>&1 | awk '{ print strftime(), 
tail -f logfile | while read line; do echo `date` "$line" ; done
; fflush() }'

Replace commandwith tail -f logfilefor your specific example. Or, perhaps you could just redirect the original program's stdout/stderr to the above pipe.

更换commandtail -f logfile你的具体的例子。或者,也许您可​​以将原始程序的 stdout/stderr 重定向到上述管道。

回答by BurninLeo

Can you configure the long running program to write it's output to the standard output and not to the logfile? In this case it would be easy to pipe the output to a script that first writes the current timestamp and then the entry.

您能否将长时间运行的程序配置为将其输出写入标准输出而不是日志文件?在这种情况下,很容易将输出通过管道传输到脚本,该脚本首先写入当前时间戳,然后写入条目。

If that is impossible, it may help to periodically (e.g. every second) read the logfile content, copy each line to another file (adding the current timestamp) and then deleting the logfile. This may, however, impose losing logfile entries that are written between reading and deleting the file :(

如果这是不可能的,定期(例如每秒)读取日志文件内容,将每一行复制到另一个文件(添加当前时间戳)然后删除日志文件可能会有所帮助。但是,这可能会导致在读取和删除文件之间写入的日志文件条目丢失:(

回答by aioobe

Try

尝试

tail -f logfile | sed -u 's/%/%%/g' | xargs -I {} date +"%Y-%m-%d %H:%M:%S {}"

回答by Andrew Clark

A little lengthy, but here is what I came up with:

有点长,但这是我想出的:

cat /etc/motd | xargs -d"\n" -I {} date +"%Y-%m-%d %H:%M:%S {}"

回答by Felipe

You can try this

你可以试试这个

2013-02-26 15:13:57 
2013-02-26 15:13:57 The programs included with the Debian GNU/Linux system are free software;
2013-02-26 15:13:57 the exact distribution terms for each program are described in the
2013-02-26 15:13:57 individual files in /usr/share/doc/*/copyright.
2013-02-26 15:13:57 
2013-02-26 15:13:57 Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
2013-02-26 15:13:57 permitted by applicable law.

Example output:

示例输出:

screen -a ls -lh -L -U -X command

回答by Jorge Niedbalski R.

Or you can use python ...

或者你可以使用 python ...

cat /dev/urandom | python -c "from __future__ import print_function; import sys; import datetime; map(lambda x: print(datetime.datetime.now(), x), [line for line in sys.stdin.readlines()])"

cat /dev/urandom | python -c "from __future__ import print_function; import sys; import datetime; map(lambda x: print(datetime.datetime.now(), x), [line for line in sys.stdin.readlines()])"

Or use gnu screen

或者使用 gnu 屏幕

logfile /tmp/screen-%S-%n.log
logtstamp on

First you need to enable logging and timestamp on your ~/.screenrc.

首先,您需要在 ~/.screenrc 上启用日志记录和时间戳。

tail -f logfile | ts '%Y-%m-%d %H:%M:%S'

回答by user13042276

A tool exists for that exact purpose, it's ts(see man ts)

一个工具就是为了这个目的而存在的,它是ts(参见man ts

For example, with your logfile:

例如,使用您的日志文件:

./blabla | ts '%Y-%m-%d %H:%M:%S'

Also works with any software writing on the standard output, of course:

当然也适用于在标准输出上编写的任何软件:

##代码##

If needed, you can add sub-second precision, with %.Sinstead of %S

如果需要,您可以添加亚秒级精度,%.S而不是%S