Files
nomad/helper/winsvc/event.go
Chris Roberts 61c36bdef7 [winsvc] Add support for Windows Eventlog (#26441)
Defines a `winsvc.Event` type which can be sent using the `winsvc.SendEvent`
function. If nomad is running on Windows and can send to the Windows
Eventlog the event will be sent. Initial event types are defined for
starting, ready, stopped, and log message.

The `winsvc.EventLogger` provides an `io.WriteCloser` that can be included
in the logger's writers collection. It will extract the log level from
log lines and write them appropriately to the eventlog. The eventlog
only supports error, warning, and info levels so messages with other
levels will be ignored.

A new configuration block is included for enabling logging to the
eventlog. Logging must be enabled with the `log_level` option and
the `eventlog.level` value can then be of the same or higher severity.
2025-09-02 16:40:31 -07:00

74 lines
1.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package winsvc
type WindowsEventId uint32
//go:generate stringer -trimprefix=Event -output strings_eventid.go -linecomment -type=WindowsEventId
const (
EventUnknown WindowsEventId = iota // unknown event
EventServiceStarting // service starting
EventServiceReady // service ready
EventServiceStopped // service stopped
EventLogMessage // log message
)
// NewEvent creates a new Event for the Windows Eventlog
func NewEvent(kind WindowsEventId, opts ...EventOption) Event {
evt := &event{
kind: kind,
level: EVENTLOG_LEVEL_INFO,
}
for _, fn := range opts {
fn(evt)
}
return evt
}
type Event interface {
Kind() WindowsEventId
Message() string
Level() EventlogLevel
}
type EventOption func(*event)
// WithEventMessage sets a custom message for the event
func WithEventMessage(msg string) EventOption {
return func(e *event) {
e.message = msg
}
}
// WithEventLevel specifies the level used for the event
func WithEventLevel(level EventlogLevel) EventOption {
return func(e *event) {
e.level = level
}
}
type event struct {
kind WindowsEventId
message string
level EventlogLevel
}
func (e *event) Kind() WindowsEventId {
return e.kind
}
func (e *event) Message() string {
if e.message != "" {
return e.message
}
return e.kind.String()
}
func (e *event) Level() EventlogLevel {
return e.level
}