Add --follow to journald ctr logging

Signed-off-by: Peter Hunt <pehunt@redhat.com>
This commit is contained in:
Peter Hunt
2019-05-22 11:46:26 -04:00
parent 51bdf29f04
commit 88429242dd
4 changed files with 30 additions and 7 deletions

@ -23,8 +23,6 @@ Note: If you are following a container which is removed `podman container rm`
or removed on exit `podman run --rm ...`, then there is a chance the the log or removed on exit `podman run --rm ...`, then there is a chance the the log
file will be removed before `podman logs` reads the final content. file will be removed before `podman logs` reads the final content.
Also note: **--follow** is not currently supported when the container's log driver is journald
**--latest, -l** **--latest, -l**
Instead of providing the container name or ID, use the last created container. If you use methods other than Podman Instead of providing the container name or ID, use the last created container. If you use methods other than Podman

@ -63,9 +63,6 @@ func (c *Container) ReadLog(options *LogOptions, logChannel chan *LogLine) error
// TODO Skip sending logs until journald logs can be read // TODO Skip sending logs until journald logs can be read
// TODO make this not a magic string // TODO make this not a magic string
if c.LogDriver() == JournaldLogging { if c.LogDriver() == JournaldLogging {
if options.Follow {
return errors.Errorf("The follow option with journald logging is not currently supported")
}
return c.readFromJournal(options, logChannel) return c.readFromJournal(options, logChannel)
} }
return c.readFromLogFile(options, logChannel) return c.readFromLogFile(options, logChannel)

@ -57,6 +57,20 @@ func (c *Container) readFromJournal(options *LogOptions, logChannel chan *LogLin
r.Rewind() r.Rewind()
} }
if options.Follow {
go func() {
follower := FollowBuffer{logChannel}
err := r.Follow(nil, follower)
if err != nil {
logrus.Debugf(err.Error())
}
r.Close()
options.WaitGroup.Done()
return
}()
return nil
}
go func() { go func() {
bytes := make([]byte, bufLen) bytes := make([]byte, bufLen)
// /me complains about no do-while in go // /me complains about no do-while in go
@ -84,8 +98,8 @@ func (c *Container) readFromJournal(options *LogOptions, logChannel chan *LogLin
func journalFormatter(entry *journal.JournalEntry) (string, error) { func journalFormatter(entry *journal.JournalEntry) (string, error) {
usec := entry.RealtimeTimestamp usec := entry.RealtimeTimestamp
timestamp := time.Unix(0, int64(usec)*int64(time.Microsecond)) tsString := time.Unix(0, int64(usec)*int64(time.Microsecond)).Format(logTimeFormat)
output := timestamp.Format(logTimeFormat) + " " output := fmt.Sprintf("%s ", tsString)
priority, ok := entry.Fields["PRIORITY"] priority, ok := entry.Fields["PRIORITY"]
if !ok { if !ok {
return "", errors.Errorf("no PRIORITY field present in journal entry") return "", errors.Errorf("no PRIORITY field present in journal entry")
@ -113,3 +127,17 @@ func journalFormatter(entry *journal.JournalEntry) (string, error) {
output += strings.TrimSpace(msg) output += strings.TrimSpace(msg)
return output, nil return output, nil
} }
type FollowBuffer struct {
logChannel chan *LogLine
}
func (f FollowBuffer) Write(p []byte) (int, error) {
bytestr := string(p)
logLine, err := newLogLine(bytestr)
if err != nil {
return -1, err
}
f.logChannel <- logLine
return len(p), nil
}

Binary file not shown.