| 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 server |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"io" |
| 19 |
"os" |
| 20 |
"sync/atomic" |
| 21 |
"time" |
| 22 |
|
| 23 |
srvlog "github.com/nats-io/nats-server/v2/logger" |
| 24 |
) |
| 25 |
|
| 26 |
// Logger interface of the NATS Server |
| 27 |
type Logger interface { |
| 28 |
|
| 29 |
// Log a notice statement |
| 30 |
Noticef(format string, v ...any) |
| 31 |
|
| 32 |
// Log a warning statement |
| 33 |
Warnf(format string, v ...any) |
| 34 |
|
| 35 |
// Log a fatal error |
| 36 |
Fatalf(format string, v ...any) |
| 37 |
|
| 38 |
// Log an error |
| 39 |
Errorf(format string, v ...any) |
| 40 |
|
| 41 |
// Log a debug statement |
| 42 |
Debugf(format string, v ...any) |
| 43 |
|
| 44 |
// Log a trace statement |
| 45 |
Tracef(format string, v ...any) |
| 46 |
} |
| 47 |
|
| 48 |
// ConfigureLogger configures and sets the logger for the server. |
| 49 |
func (s *Server) ConfigureLogger() { |
| 50 |
var ( |
| 51 |
log Logger |
| 52 |
|
| 53 |
// Snapshot server options. |
| 54 |
opts = s.getOpts() |
| 55 |
) |
| 56 |
|
| 57 |
if opts.NoLog { |
| 58 |
return |
| 59 |
} |
| 60 |
|
| 61 |
syslog := opts.Syslog |
| 62 |
if isWindowsService() && opts.LogFile == "" { |
| 63 |
// Enable syslog if no log file is specified and we're running as a |
| 64 |
// Windows service so that logs are written to the Windows event log. |
| 65 |
syslog = true |
| 66 |
} |
| 67 |
|
| 68 |
if opts.LogFile != "" { |
| 69 |
log = srvlog.NewFileLogger(opts.LogFile, opts.Logtime, opts.Debug, opts.Trace, true, srvlog.LogUTC(opts.LogtimeUTC)) |
| 70 |
if opts.LogSizeLimit > 0 { |
| 71 |
if l, ok := log.(*srvlog.Logger); ok { |
| 72 |
l.SetSizeLimit(opts.LogSizeLimit) |
| 73 |
} |
| 74 |
} |
| 75 |
if opts.LogMaxFiles > 0 { |
| 76 |
if l, ok := log.(*srvlog.Logger); ok { |
| 77 |
al := int(opts.LogMaxFiles) |
| 78 |
if int64(al) != opts.LogMaxFiles { |
| 79 |
// set to default (no max) on overflow |
| 80 |
al = 0 |
| 81 |
} |
| 82 |
l.SetMaxNumFiles(al) |
| 83 |
} |
| 84 |
} |
| 85 |
} else if opts.RemoteSyslog != "" { |
| 86 |
log = srvlog.NewRemoteSysLogger(opts.RemoteSyslog, opts.Debug, opts.Trace) |
| 87 |
} else if syslog { |
| 88 |
log = srvlog.NewSysLogger(opts.Debug, opts.Trace) |
| 89 |
} else { |
| 90 |
colors := true |
| 91 |
// Check to see if stderr is being redirected and if so turn off color |
| 92 |
// Also turn off colors if we're running on Windows where os.Stderr.Stat() returns an invalid handle-error |
| 93 |
stat, err := os.Stderr.Stat() |
| 94 |
if err != nil || (stat.Mode()&os.ModeCharDevice) == 0 { |
| 95 |
colors = false |
| 96 |
} |
| 97 |
log = srvlog.NewStdLogger(opts.Logtime, opts.Debug, opts.Trace, colors, true, srvlog.LogUTC(opts.LogtimeUTC)) |
| 98 |
} |
| 99 |
|
| 100 |
s.SetLoggerV2(log, opts.Debug, opts.Trace, opts.TraceVerbose) |
| 101 |
} |
| 102 |
|
| 103 |
// Returns our current logger. |
| 104 |
func (s *Server) Logger() Logger { |
| 105 |
s.logging.Lock() |
| 106 |
defer s.logging.Unlock() |
| 107 |
return s.logging.logger |
| 108 |
} |
| 109 |
|
| 110 |
// SetLogger sets the logger of the server |
| 111 |
func (s *Server) SetLogger(logger Logger, debugFlag, traceFlag bool) { |
| 112 |
s.SetLoggerV2(logger, debugFlag, traceFlag, false) |
| 113 |
} |
| 114 |
|
| 115 |
// SetLogger sets the logger of the server |
| 116 |
func (s *Server) SetLoggerV2(logger Logger, debugFlag, traceFlag, sysTrace bool) { |
| 117 |
if debugFlag { |
| 118 |
atomic.StoreInt32(&s.logging.debug, 1) |
| 119 |
} else { |
| 120 |
atomic.StoreInt32(&s.logging.debug, 0) |
| 121 |
} |
| 122 |
if traceFlag { |
| 123 |
atomic.StoreInt32(&s.logging.trace, 1) |
| 124 |
} else { |
| 125 |
atomic.StoreInt32(&s.logging.trace, 0) |
| 126 |
} |
| 127 |
if sysTrace { |
| 128 |
atomic.StoreInt32(&s.logging.traceSysAcc, 1) |
| 129 |
} else { |
| 130 |
atomic.StoreInt32(&s.logging.traceSysAcc, 0) |
| 131 |
} |
| 132 |
var prevLoggerErr error |
| 133 |
s.logging.Lock() |
| 134 |
if s.logging.logger != nil { |
| 135 |
// Check to see if the logger implements io.Closer. This could be a |
| 136 |
// logger from another process embedding the NATS server or a dummy |
| 137 |
// test logger that may not implement that interface. |
| 138 |
if l, ok := s.logging.logger.(io.Closer); ok { |
| 139 |
prevLoggerErr = l.Close() |
| 140 |
} |
| 141 |
} |
| 142 |
s.logging.logger = logger |
| 143 |
s.logging.Unlock() |
| 144 |
|
| 145 |
if prevLoggerErr != nil { |
| 146 |
s.Errorf("Error closing logger: %v", prevLoggerErr) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// ReOpenLogFile if the logger is a file based logger, close and re-open the file. |
| 151 |
// This allows for file rotation by 'mv'ing the file then signaling |
| 152 |
// the process to trigger this function. |
| 153 |
func (s *Server) ReOpenLogFile() { |
| 154 |
// Check to make sure this is a file logger. |
| 155 |
s.logging.RLock() |
| 156 |
ll := s.logging.logger |
| 157 |
s.logging.RUnlock() |
| 158 |
|
| 159 |
if ll == nil { |
| 160 |
s.Noticef("File log re-open ignored, no logger") |
| 161 |
return |
| 162 |
} |
| 163 |
|
| 164 |
// Snapshot server options. |
| 165 |
opts := s.getOpts() |
| 166 |
|
| 167 |
if opts.LogFile == "" { |
| 168 |
s.Noticef("File log re-open ignored, not a file logger") |
| 169 |
} else { |
| 170 |
fileLog := srvlog.NewFileLogger( |
| 171 |
opts.LogFile, opts.Logtime, |
| 172 |
opts.Debug, opts.Trace, true, |
| 173 |
srvlog.LogUTC(opts.LogtimeUTC), |
| 174 |
) |
| 175 |
s.SetLogger(fileLog, opts.Debug, opts.Trace) |
| 176 |
if opts.LogSizeLimit > 0 { |
| 177 |
fileLog.SetSizeLimit(opts.LogSizeLimit) |
| 178 |
} |
| 179 |
s.Noticef("File log re-opened") |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// Noticef logs a notice statement |
| 184 |
func (s *Server) Noticef(format string, v ...any) { |
| 185 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 186 |
logger.Noticef(format, v...) |
| 187 |
}, format, v...) |
| 188 |
} |
| 189 |
|
| 190 |
// Errorf logs an error |
| 191 |
func (s *Server) Errorf(format string, v ...any) { |
| 192 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 193 |
logger.Errorf(format, v...) |
| 194 |
}, format, v...) |
| 195 |
} |
| 196 |
|
| 197 |
// Error logs an error with a scope |
| 198 |
func (s *Server) Errors(scope any, e error) { |
| 199 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 200 |
logger.Errorf(format, v...) |
| 201 |
}, "%s - %s", scope, UnpackIfErrorCtx(e)) |
| 202 |
} |
| 203 |
|
| 204 |
// Error logs an error with a context |
| 205 |
func (s *Server) Errorc(ctx string, e error) { |
| 206 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 207 |
logger.Errorf(format, v...) |
| 208 |
}, "%s: %s", ctx, UnpackIfErrorCtx(e)) |
| 209 |
} |
| 210 |
|
| 211 |
// Error logs an error with a scope and context |
| 212 |
func (s *Server) Errorsc(scope any, ctx string, e error) { |
| 213 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 214 |
logger.Errorf(format, v...) |
| 215 |
}, "%s - %s: %s", scope, ctx, UnpackIfErrorCtx(e)) |
| 216 |
} |
| 217 |
|
| 218 |
// Warnf logs a warning error |
| 219 |
func (s *Server) Warnf(format string, v ...any) { |
| 220 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 221 |
logger.Warnf(format, v...) |
| 222 |
}, format, v...) |
| 223 |
} |
| 224 |
|
| 225 |
func (s *Server) rateLimitFormatWarnf(format string, v ...any) { |
| 226 |
if _, loaded := s.rateLimitLogging.LoadOrStore(format, time.Now()); loaded { |
| 227 |
return |
| 228 |
} |
| 229 |
statement := fmt.Sprintf(format, v...) |
| 230 |
s.Warnf("%s", statement) |
| 231 |
} |
| 232 |
|
| 233 |
func (s *Server) RateLimitErrorf(format string, v ...any) { |
| 234 |
statement := fmt.Sprintf(format, v...) |
| 235 |
if _, loaded := s.rateLimitLogging.LoadOrStore(statement, time.Now()); loaded { |
| 236 |
return |
| 237 |
} |
| 238 |
s.Errorf("%s", statement) |
| 239 |
} |
| 240 |
|
| 241 |
func (s *Server) RateLimitWarnf(format string, v ...any) { |
| 242 |
statement := fmt.Sprintf(format, v...) |
| 243 |
if _, loaded := s.rateLimitLogging.LoadOrStore(statement, time.Now()); loaded { |
| 244 |
return |
| 245 |
} |
| 246 |
s.Warnf("%s", statement) |
| 247 |
} |
| 248 |
|
| 249 |
func (s *Server) RateLimitDebugf(format string, v ...any) { |
| 250 |
statement := fmt.Sprintf(format, v...) |
| 251 |
if _, loaded := s.rateLimitLogging.LoadOrStore(statement, time.Now()); loaded { |
| 252 |
return |
| 253 |
} |
| 254 |
s.Debugf("%s", statement) |
| 255 |
} |
| 256 |
|
| 257 |
// Fatalf logs a fatal error |
| 258 |
func (s *Server) Fatalf(format string, v ...any) { |
| 259 |
if s.isShuttingDown() { |
| 260 |
s.Errorf(format, v) |
| 261 |
return |
| 262 |
} |
| 263 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 264 |
logger.Fatalf(format, v...) |
| 265 |
}, format, v...) |
| 266 |
} |
| 267 |
|
| 268 |
// Debugf logs a debug statement |
| 269 |
func (s *Server) Debugf(format string, v ...any) { |
| 270 |
if atomic.LoadInt32(&s.logging.debug) == 0 { |
| 271 |
return |
| 272 |
} |
| 273 |
|
| 274 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 275 |
logger.Debugf(format, v...) |
| 276 |
}, format, v...) |
| 277 |
} |
| 278 |
|
| 279 |
// Tracef logs a trace statement |
| 280 |
func (s *Server) Tracef(format string, v ...any) { |
| 281 |
if atomic.LoadInt32(&s.logging.trace) == 0 { |
| 282 |
return |
| 283 |
} |
| 284 |
|
| 285 |
s.executeLogCall(func(logger Logger, format string, v ...any) { |
| 286 |
logger.Tracef(format, v...) |
| 287 |
}, format, v...) |
| 288 |
} |
| 289 |
|
| 290 |
func (s *Server) executeLogCall(f func(logger Logger, format string, v ...any), format string, args ...any) { |
| 291 |
s.logging.RLock() |
| 292 |
defer s.logging.RUnlock() |
| 293 |
if s.logging.logger == nil { |
| 294 |
return |
| 295 |
} |
| 296 |
|
| 297 |
f(s.logging.logger, format, args...) |
| 298 |
} |