Reading log files as they're updated in Go

前端 未结 5 1172
刺人心
刺人心 2020-12-14 14:29

I\'m trying to parse some log files as they\'re being written in Go but I\'m not sure how I would accomplish this without rereading the file again and again while checking f

5条回答
  •  余生分开走
    2020-12-14 15:05

    You have to either watch the file for changes (using an OS-specific subsystem to accomplish this) or poll it periodically to see whether its modification time (and size) changed. In either case, after reading another chunk of data you remember the file offset and restore it before reading another chunk after detecting the change.

    But note that this seems to be easy only on paper: in real life files might be truncated, replaced or renamed (because that's what tools like logrotate are supposed to do).

    See this question for more discussion of this problem.

提交回复
热议问题