Skip to content

Commit

Permalink
Take into account watched files that are truncated and filled with NU…
Browse files Browse the repository at this point in the history
…L bytes by the OS (#192)

* Update go-tail/follower to fix NUL byte problem, and add note to README

* copy fix
  • Loading branch information
snorecone authored Nov 17, 2016
1 parent b19a735 commit 5776d38
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 42 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,29 @@ than the current set of matches). This is not necessary for globs defined in
the config file.


### Log rotation
### Log rotation and the behavior of remote_syslog

External log rotation scripts often move or remove an existing log file
and replace it with a new one (at a new inode). The Linux standard script
[logrotate](http://iain.cx/src/logrotate/) supports a `copytruncate` config
option. With that option, `logrotate` will copy files, operate on the copies,
and truncate the original so that the inode remains the same.

This comes closest to ensuring that programs watching these files (including
`remote_syslog`) will not be affected by, or need to be notified of, the
rotation. The only tradeoff of `copytruncate` is slightly higher disk usage
during rotation, so we recommend this option whether or not you use
`remote_syslog`.
`remote_syslog` will handle both approaches seamlessly, so it should be no
concern as to which method is used. If a log file is moved or renamed,
and a new file is created (at a new inode), `remote_syslog` will follow that
new file at the new inode (assuming it has the same absolute path name). If
a file is copied then truncated, `remote_syslog` will seek to the beginning of
the truncated file and continue to read it.

#### Log rotation edge cases to be aware of

Some logging programs such as Java's gclog (`-XX:+PrintGC` or `-verbose:gc`)
do not log in append mode, so if another program such as `logrotate` (set to
`copytruncate`) truncates the file, on the next write of the Java logger, the
OS will fill the file with NUL bytes upto the current offset of the file descriptor.
More info on that [here](http://stackoverflow.com/questions/8353401/garbage-collector-log-loggc-file-rotation-with-logrotate-does-not-work-properl).
`remote_syslog` will detect those leading NUL bytes, discard them, and log the discard count.


### Excluding files from being sent
Expand Down
14 changes: 13 additions & 1 deletion remote_syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,24 @@ func (s *Server) tailOne(file, tag string, whence int) {

for {
select {
case line := <-t.Lines():
case line, ok := <-t.Lines():
if !ok {
if t.Err() != nil {
log.Errorf("%s", t.Err())
}

return
}

if s.closing() {
t.Close()
return
}

if d := line.Discarded(); d > 0 {
log.Infof("Discarded %d NULL bytes", d)
}

l := line.String()

if !matchExps(l, s.config.ExcludePatterns) {
Expand Down
6 changes: 5 additions & 1 deletion remote_syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,16 @@ func (s *testSyslogServer) serve() {
continue
}

fmt.Printf(line)
packet, err := syslog.Parse(strings.TrimRight(line, "\n"))
if err != nil {
panic(err)
}

s.packets <- packet
select {
case s.packets <- packet:
case <-time.After(time.Second):
}
}
}
}
Expand Down
116 changes: 85 additions & 31 deletions vendor/github.com/papertrail/go-tail/follower/follower.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@
"revisionTime": "2016-11-15T00:28:15Z"
},
{
"checksumSHA1": "bxKDUFzzl81g5apnjRKVr5e0Cw4=",
"checksumSHA1": "ZnoFXmQ+DSHU1OMWqPWOGYxlv7g=",
"path": "github.com/papertrail/go-tail/follower",
"revision": "c08f359f0f47b6a0a603b526a012fcc8924a65b0",
"revisionTime": "2016-11-15T00:28:15Z"
"revision": "39566fbcbaada1657f8c074dc55a1314e913fc8c",
"revisionTime": "2016-11-16T22:37:32Z"
},
{
"checksumSHA1": "8Y05Pz7onrQPcVWW6JStSsYRh6E=",
Expand Down

0 comments on commit 5776d38

Please sign in to comment.