2

I have a c++ tool that outputs to STDOUT via printf i.e.

printf ("%s\n", logline);  

I then pipe that to rotatelogs i.e.

tool | rotatelogs /tmp/logs/log_%s 60  

Everything works great, but rotatelogs will rotate mid line so log file 1 has this json snippet:

{"tim  

...and log file 2 will have:

e":1386088072}  

Is there a way to encourage rotatelogs to rotate on line breaks?

1
  • k... I found the answer, but can't post for eight hours per server fault rules... Stay tuned. Dec 3, 2013 at 20:06

2 Answers 2

2

Simple answer: IO buffering. Turn it off by prefixing your command with:

stdbuf -i0 -o0 -e0  

for example:

stdbuf -i0 -o0 -e0  tool | rotatelogs /tmp/logs/log_%s 60
0

If the tool you have is your own or you have source of it, fix it first. I assumed the tool is a daemon since it has log to rotate. (means it has long term lifecycle.) to be a deamon, it must duplicate std* and close originals that related to tty. (it is somewhat off topic.)

Anyway, if you cannot touch the source, I recommend to use 'logrotate' instead of apache 'rotatelogs'. it is more general and well-made tool for generic purpose.

one of common problem of rotatelogs is, if process of rotatelogs is dead, all outputs are gone away. so try:

$ tool > logfile &

and create or edit logrotate.conf for logfile.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .