Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with structured logger #5723

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion auditbeat/module/audit/file/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type MetricSet struct {
config Config
reader EventProducer
scanner EventProducer
log *logp.Logger

// Runtime params that are initialized on Run().
bucket datastore.BoltBucket
Expand All @@ -74,6 +75,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
BaseMetricSet: base,
config: config,
reader: r,
log: logp.NewLogger(metricsetName),
}

if config.ScanAtStart {
Expand All @@ -83,7 +85,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
}
}

debugf("Initialized the audit file event reader. Running as euid=%v", os.Geteuid())
ms.log.Debug("Initialized the audit file event reader.", logp.Int("euid", os.Geteuid()))

return ms, nil
}
Expand Down Expand Up @@ -203,6 +205,11 @@ func (ms *MetricSet) hasFileChangedSinceLastEvent(event *Event) bool {
if changed && logp.IsDebug(metricsetName) {
debugf("file at %v has changed since last seen: old=%v, new=%v",
event.Path, lastEvent, event)
ms.log.Debug("File has changed since last seen",
logp.String("file.path", event.Path),
logp.Stringer("old_event", lastEvent),
logp.Stringer("new_event", event),
)
}
return changed
}
Expand Down
43 changes: 35 additions & 8 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type beatConfig struct {
// beat internal components configurations
HTTP *common.Config `config:"http"`
Path paths.Path `config:"path"`
Logging logp.Logging `config:"logging"`
Logging logp.Config `config:"logging"`

// output/publishing related configurations
Pipeline pipeline.Config `config:",inline"`
Expand All @@ -87,6 +87,10 @@ var (
printVersion bool
setup bool
startTime time.Time

verbose bool
toStderr bool
debugSelectorsStr string
)

var debugf = logp.MakeDebug("beat")
Expand All @@ -98,6 +102,9 @@ func init() {

flag.BoolVar(&printVersion, "version", false, "Print the version and exit")
flag.BoolVar(&setup, "setup", false, "Load the sample Kibana dashboards")
flag.BoolVar(&verbose, "v", false, "Log at INFO level")
flag.BoolVar(&toStderr, "e", false, "Log to stderr and disable syslog/file output")
flag.StringVar(&debugSelectorsStr, "d", "", "Enable certain debug selectors")
}

// initRand initializes the runtime random number generator seed using
Expand Down Expand Up @@ -156,7 +163,10 @@ func NewBeat(name, indexPrefix, v string) (*Beat, error) {
},
}

return &Beat{Beat: b}, nil
rtn := &Beat{Beat: b}
rtn.Config.Logging = logp.DefaultConfig
rtn.Config.Logging.Files.Name = name
return rtn, nil
}

// init does initialization of things common to all actions (read confs, flags)
Expand Down Expand Up @@ -232,6 +242,8 @@ func (b *Beat) createBeater(bt beat.Creator) (beat.Beater, error) {
}

func (b *Beat) launch(bt beat.Creator) error {
defer logp.Sync()

err := b.Init()
if err != nil {
return err
Expand Down Expand Up @@ -273,9 +285,15 @@ func (b *Beat) launch(bt beat.Creator) error {
}
}

// TODO: Use config.
metricsLogger, err := monitoring.StartNewPeriodLogger(30 * time.Second)
if err != nil {
return err
}
defer metricsLogger.Stop()

logp.Info("%s start running.", b.Info.Beat)
defer logp.Info("%s stopped.", b.Info.Beat)
defer logp.LogTotalExpvars(&b.Config.Logging)

if b.Config.HTTP.Enabled() {
api.Start(b.Config.HTTP, b.Info)
Expand Down Expand Up @@ -382,11 +400,21 @@ func (b *Beat) handleFlags() error {
return beat.GracefulExit
}

if err := logp.HandleFlags(b.Info.Beat); err != nil {
return err
return cfgfile.HandleFlags()
}

func (b *Beat) configureLogging() error {
if toStderr {
b.Config.Logging.ToStderr = true
}
if verbose && strings.ToLower(b.Config.Logging.Level) != "debug" {
b.Config.Logging.Level = "info"
}
if debugSelectorsStr != "" {
b.Config.Logging.Selectors = strings.Split(debugSelectorsStr, ",")
}

return cfgfile.HandleFlags()
return logp.CustomSetup(b.Config.Logging)
}

// config reads the configuration file from disk, parses the common options
Expand Down Expand Up @@ -427,8 +455,7 @@ func (b *Beat) configure() error {
return fmt.Errorf("error setting default paths: %v", err)
}

err = logp.Init(b.Info.Beat, startTime, &b.Config.Logging)
if err != nil {
if err = b.configureLogging(); err != nil {
return fmt.Errorf("error initializing logging: %v", err)
}

Expand Down
7 changes: 3 additions & 4 deletions libbeat/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"runtime"
"strings"

ucfg "github.com/elastic/go-ucfg"
"github.com/elastic/go-ucfg/yaml"

"github.com/elastic/beats/libbeat/common/file"
"github.com/elastic/beats/libbeat/logp"
ucfg "github.com/elastic/go-ucfg"
"github.com/elastic/go-ucfg/cfgutil"
"github.com/elastic/go-ucfg/yaml"
)

var flagStrictPerms = flag.Bool("strict.perms", true, "Strict permission checking on config files")
Expand Down Expand Up @@ -63,7 +62,7 @@ var debugBlacklist = MakeStringSet(
)

// make hasSelector and configDebugf available for unit testing
var hasSelector = logp.HasSelector
var hasSelector = func(string) bool { return false }
var configDebugf = logp.Debug

func NewConfig() *Config {
Expand Down
Loading