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

RSDK-9742: Add event logger support for windows. #4713

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ require (
golang.org/x/image v0.19.0
golang.org/x/mobile v0.0.0-20240112133503-c713f31d574b
golang.org/x/sync v0.8.0
golang.org/x/sys v0.27.0
golang.org/x/sys v0.29.0
golang.org/x/term v0.25.0
golang.org/x/text v0.19.0
golang.org/x/time v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1778,8 +1778,8 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
23 changes: 15 additions & 8 deletions logging/appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ func NewWriterAppender(writer io.Writer) ConsoleAppender {
return ConsoleAppender{writer}
}

func ZapcoreFieldsToJSON(fields []zapcore.Field) (string, error) {
// Use zap's json encoder which will encode our slice of fields in-order. As opposed to the
// random iteration order of a map. Call it with an empty Entry object such that only the fields
// become "map-ified".
jsonEncoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{SkipLineEnding: true})
buf, err := jsonEncoder.EncodeEntry(zapcore.Entry{}, fields)
if err != nil {
return "", err
}

return string(buf.Bytes()), nil
}

// Write outputs the log entry to the underlying stream.
func (appender ConsoleAppender) Write(entry zapcore.Entry, fields []zapcore.Field) error {
const maxLength = 10
Expand All @@ -54,17 +67,11 @@ func (appender ConsoleAppender) Write(entry zapcore.Entry, fields []zapcore.Fiel
return nil
}

// Use zap's json encoder which will encode our slice of fields in-order. As opposed to the
// random iteration order of a map. Call it with an empty Entry object such that only the fields
// become "map-ified".
jsonEncoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{SkipLineEnding: true})
buf, err := jsonEncoder.EncodeEntry(zapcore.Entry{}, fields)
fieldsJSON, err := ZapcoreFieldsToJSON(fields)
if err != nil {
// Log what we have and return the error.
fmt.Fprintln(appender.Writer, strings.Join(toPrint, "\t")) //nolint:errcheck
return err
}
toPrint = append(toPrint, string(buf.Bytes()))
toPrint = append(toPrint, fieldsJSON)

fmt.Fprintln(appender.Writer, strings.Join(toPrint, "\t")) //nolint:errcheck
return nil
Expand Down
5 changes: 5 additions & 0 deletions logging/noop_event_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !windows

package logging

func RegisterEventLogger(rootLogger Logger) {}
63 changes: 63 additions & 0 deletions logging/windows_event_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build windows

package logging

import (
"strings"

"go.uber.org/zap/zapcore"
"golang.org/x/sys/windows/svc/eventlog"
)

func RegisterEventLogger(rootLogger Logger) {
log, err := eventlog.Open("viam-server")
if err != nil {
rootLogger.Errorw("Unable to open windows event log", "err", err)
}
rootLogger.AddAppender(&eventLogger{log})
}

type eventLogger struct {
log *eventlog.Log
}

func getMessage(entry zapcore.Entry, fields []zapcore.Field) string {
const maxLength = 10
toPrint := make([]string, 0, maxLength)
// We use UTC such that logs from different `viam-server`s can have their logs compared without
// needing them to be configured in the same timezone.
toPrint = append(toPrint, entry.Time.UTC().Format(DefaultTimeFormatStr))
toPrint = append(toPrint, strings.ToUpper(entry.Level.String()))
toPrint = append(toPrint, entry.LoggerName)
if entry.Caller.Defined {
toPrint = append(toPrint, callerToString(&entry.Caller))
}
toPrint = append(toPrint, entry.Message)
if len(fields) == 0 {
return strings.Join(toPrint, "\t")
}

fieldsJSON, err := ZapcoreFieldsToJSON(fields)
if err != nil {
return strings.Join(toPrint, "\t")
}
toPrint = append(toPrint, fieldsJSON)

return strings.Join(toPrint, "\t")
}

func (el *eventLogger) Write(entry zapcore.Entry, fields []zapcore.Field) error {
switch entry.Level {
case zapcore.DebugLevel, zapcore.InfoLevel:
el.log.Info(0, getMessage(entry, fields))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no debug level on windows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in that API that was linked to me, no.

case zapcore.WarnLevel:
el.log.Warning(0, getMessage(entry, fields))
default: // includes zapcore.ErrorLevel and "more threatening" levels
el.log.Error(0, getMessage(entry, fields))
}
return nil
}

func (el *eventLogger) Sync() error {
return nil
}
1 change: 1 addition & 0 deletions web/server/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func RunServer(ctx context.Context, args []string, _ logging.Logger) (err error)
}

logger, registry := logging.NewLoggerWithRegistry("rdk")
logging.RegisterEventLogger(logger)
logging.ReplaceGlobal(logger)
config.InitLoggingSettings(logger, argsParsed.Debug)

Expand Down
Loading