Add support for reloading logtime and log_file

This commit is contained in:
Tyler Treat
2017-06-22 12:59:13 -05:00
parent 0e557e215a
commit 8f5aa0433d
4 changed files with 39 additions and 8 deletions

View File

@@ -3,8 +3,8 @@
# logging options
debug: true # enable on reload
trace: true # enable on reload
logtime: false
log_file: "/tmp/gnatsd.log"
logtime: true # enable on reload
log_file: "/tmp/gnatsd-2.log" # change on reload
# Enable TLS on reload
tls {

View File

@@ -712,8 +712,9 @@ func MergeOptions(fileOpts, flagOpts *Options) *Options {
if flagOpts.Trace {
opts.Trace = true
}
if flagOpts.Logtime {
opts.Logtime = true
// Logtime flag defaults to true, so only take precedence if it was set to false.
if !flagOpts.Logtime {
opts.Logtime = false
}
if flagOpts.LogFile != "" {
opts.LogFile = flagOpts.LogFile

View File

@@ -45,8 +45,7 @@ type traceOption struct {
newValue bool
}
// Apply is a no-op because authorization will be reloaded after options are
// applied
// Apply is a no-op because logging will be reloaded after options are applied.
func (t *traceOption) Apply(server *Server) {
server.Noticef("Reloaded: trace = %v", t.newValue)
}
@@ -57,12 +56,33 @@ type debugOption struct {
newValue bool
}
// Apply is a no-op because authorization will be reloaded after options are
// applied
// Apply is a no-op because logging will be reloaded after options are applied
func (d *debugOption) Apply(server *Server) {
server.Noticef("Reloaded: debug = %v", d.newValue)
}
// logtimeOption implements the option interface for the `logtime` setting.
type logtimeOption struct {
loggingOption
newValue bool
}
// Apply is a no-op because logging will be reloaded after options are applied.
func (l *logtimeOption) Apply(server *Server) {
server.Noticef("Reloaded: logtime = %v", l.newValue)
}
// logfileOption implements the option interface for the `log_file` setting.
type logfileOption struct {
loggingOption
newValue string
}
// Apply is a no-op because logging will be reloaded after options are applied.
func (l *logfileOption) Apply(server *Server) {
server.Noticef("Reloaded: log_file = %v", l.newValue)
}
// noopOption is a base struct that provides default no-op behaviors.
type noopOption struct{}
@@ -296,6 +316,10 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) {
diffOpts = append(diffOpts, &traceOption{newValue: newValue.(bool)})
case "debug":
diffOpts = append(diffOpts, &debugOption{newValue: newValue.(bool)})
case "logtime":
diffOpts = append(diffOpts, &logtimeOption{newValue: newValue.(bool)})
case "logfile":
diffOpts = append(diffOpts, &logfileOption{newValue: newValue.(string)})
case "tlsconfig":
diffOpts = append(diffOpts, &tlsOption{newValue: newValue.(*tls.Config)})
case "tlstimeout":

View File

@@ -181,6 +181,12 @@ func TestConfigReload(t *testing.T) {
if !updated.Debug {
t.Fatal("Expected Debug to be true")
}
if !updated.Logtime {
t.Fatal("Expected Logtime to be true")
}
if updated.LogFile != "/tmp/gnatsd-2.log" {
t.Fatalf("LogFile is incorrect.\nexpected /tmp/gnatsd-2.log\ngot: %s", updated.LogFile)
}
if updated.TLSConfig == nil {
t.Fatal("Expected TLSConfig to be non-nil")
}