Debian Codegraph @

logger/syslog_windows.go

raw
1 // Copyright 2012-2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package logger logs to the windows event log
15 package logger
16
17 import (
18 "fmt"
19 "os"
20 "strings"
21
22 "golang.org/x/sys/windows/svc/eventlog"
23 )
24
25 var natsEventSource = "NATS-Server"
26
27 // SetSyslogName sets the name to use for the system log event source
28 func SetSyslogName(name string) {
29 natsEventSource = name
30 }
31
32 // SysLogger logs to the windows event logger
33 type SysLogger struct {
34 writer *eventlog.Log
35 debug bool
36 trace bool
37 }
38
39 // NewSysLogger creates a log using the windows event logger
40 func NewSysLogger(debug, trace bool) *SysLogger {
41 if err := eventlog.InstallAsEventCreate(natsEventSource, eventlog.Info|eventlog.Error|eventlog.Warning); err != nil {
42 if !strings.Contains(err.Error(), "registry key already exists") {
43 panic(fmt.Sprintf("could not access event log: %v", err))
44 }
45 }
46
47 w, err := eventlog.Open(natsEventSource)
48 if err != nil {
49 panic(fmt.Sprintf("could not open event log: %v", err))
50 }
51
52 return &SysLogger{
53 writer: w,
54 debug: debug,
55 trace: trace,
56 }
57 }
58
59 // NewRemoteSysLogger creates a remote event logger
60 func NewRemoteSysLogger(fqn string, debug, trace bool) *SysLogger {
61 w, err := eventlog.OpenRemote(fqn, natsEventSource)
62 if err != nil {
63 panic(fmt.Sprintf("could not open event log: %v", err))
64 }
65
66 return &SysLogger{
67 writer: w,
68 debug: debug,
69 trace: trace,
70 }
71 }
72
73 func formatMsg(tag, format string, v ...any) string {
74 orig := fmt.Sprintf(format, v...)
75 return fmt.Sprintf("pid[%d][%s]: %s", os.Getpid(), tag, orig)
76 }
77
78 // Noticef logs a notice statement
79 func (l *SysLogger) Noticef(format string, v ...any) {
80 l.writer.Info(1, formatMsg("NOTICE", format, v...))
81 }
82
83 // Warnf logs a warning statement
84 func (l *SysLogger) Warnf(format string, v ...any) {
85 l.writer.Info(1, formatMsg("WARN", format, v...))
86 }
87
88 // Fatalf logs a fatal error
89 func (l *SysLogger) Fatalf(format string, v ...any) {
90 msg := formatMsg("FATAL", format, v...)
91 l.writer.Error(5, msg)
92 panic(msg)
93 }
94
95 // Errorf logs an error statement
96 func (l *SysLogger) Errorf(format string, v ...any) {
97 l.writer.Error(2, formatMsg("ERROR", format, v...))
98 }
99
100 // Debugf logs a debug statement
101 func (l *SysLogger) Debugf(format string, v ...any) {
102 if l.debug {
103 l.writer.Info(3, formatMsg("DEBUG", format, v...))
104 }
105 }
106
107 // Tracef logs a trace statement
108 func (l *SysLogger) Tracef(format string, v ...any) {
109 if l.trace {
110 l.writer.Info(4, formatMsg("TRACE", format, v...))
111 }
112 }