summaryrefslogtreecommitdiff
path: root/libpod/events/logfile.go
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2019-03-27 13:50:54 -0500
committerbaude <bbaude@redhat.com>2019-04-24 16:00:04 -0500
commit7bf7c177ab3f67d5de1689842204c258fca083e4 (patch)
tree960fd1365913209fec61e3226b96a6f8bb1fd051 /libpod/events/logfile.go
parentd75543fcd2ce87a9b87b8883400f355979004e91 (diff)
downloadpodman-7bf7c177ab3f67d5de1689842204c258fca083e4.tar.gz
podman-7bf7c177ab3f67d5de1689842204c258fca083e4.tar.bz2
podman-7bf7c177ab3f67d5de1689842204c258fca083e4.zip
journald event logging
add the ability for podman to read and write events to journald instead of just a logfile. This can be controlled in libpod.conf with the `events_logger` attribute of `journald` or `file`. The default will be set to `journald`. Signed-off-by: baude <bbaude@redhat.com>
Diffstat (limited to 'libpod/events/logfile.go')
-rw-r--r--libpod/events/logfile.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/libpod/events/logfile.go b/libpod/events/logfile.go
new file mode 100644
index 000000000..3232b86d0
--- /dev/null
+++ b/libpod/events/logfile.go
@@ -0,0 +1,65 @@
+package events
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/pkg/errors"
+)
+
+// EventLogFile is the structure for event writing to a logfile. It contains the eventer
+// options and the event itself. Methods for reading and writing are also defined from it.
+type EventLogFile struct {
+ options EventerOptions
+}
+
+// Writes to the log file
+func (e EventLogFile) Write(ee Event) error {
+ f, err := os.OpenFile(e.options.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0700)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ eventJSONString, err := ee.ToJSONString()
+ if err != nil {
+ return err
+ }
+ if _, err := f.WriteString(fmt.Sprintf("%s\n", eventJSONString)); err != nil {
+ return err
+ }
+ return nil
+
+}
+
+// Reads from the log file
+func (e EventLogFile) Read(options ReadOptions) error {
+ eventOptions, err := generateEventOptions(options.Filters, options.Since, options.Until)
+ if err != nil {
+ return errors.Wrapf(err, "unable to generate event options")
+ }
+ t, err := e.getTail(options)
+ if err != nil {
+ return err
+ }
+ for line := range t.Lines {
+ event, err := newEventFromJSONString(line.Text)
+ if err != nil {
+ return err
+ }
+ switch event.Type {
+ case Image, Volume, Pod, Container:
+ // no-op
+ default:
+ return errors.Errorf("event type %s is not valid in %s", event.Type.String(), e.options.LogFilePath)
+ }
+ include := true
+ for _, filter := range eventOptions {
+ include = include && filter(event)
+ }
+ if include {
+ options.EventChannel <- event
+ }
+ }
+ close(options.EventChannel)
+ return nil
+}