@@ -169,7 +169,7 @@ func (c *DebugCommand) Flags() *FlagSets {
169
169
Usage : "Target to capture, defaulting to all if none specified. " +
170
170
"This can be specified multiple times to capture multiple targets. " +
171
171
"Available targets are: config, host, metrics, pprof, " +
172
- "replication-status, server-status." ,
172
+ "replication-status, server-status, log ." ,
173
173
})
174
174
175
175
return set
@@ -477,7 +477,7 @@ func (c *DebugCommand) preflight(rawArgs []string) (string, error) {
477
477
}
478
478
479
479
func (c * DebugCommand ) defaultTargets () []string {
480
- return []string {"config" , "host" , "metrics" , "pprof" , "replication-status" , "server-status" }
480
+ return []string {"config" , "host" , "metrics" , "pprof" , "replication-status" , "server-status" , "log" }
481
481
}
482
482
483
483
func (c * DebugCommand ) captureStaticTargets () error {
@@ -513,6 +513,7 @@ func (c *DebugCommand) capturePollingTargets() error {
513
513
var g run.Group
514
514
515
515
ctx , cancelFunc := context .WithTimeout (context .Background (), c .flagDuration + debugDurationGrace )
516
+ defer cancelFunc ()
516
517
517
518
// This run group watches for interrupt or duration
518
519
g .Add (func () error {
@@ -576,6 +577,15 @@ func (c *DebugCommand) capturePollingTargets() error {
576
577
})
577
578
}
578
579
580
+ if strutil .StrListContains (c .flagTargets , "log" ) {
581
+ g .Add (func () error {
582
+ _ = c .writeLogs (ctx )
583
+ return nil
584
+ }, func (error ) {
585
+ cancelFunc ()
586
+ })
587
+ }
588
+
579
589
// We shouldn't bump across errors since none is returned by the interrupts,
580
590
// but we error check for sanity here.
581
591
if err := g .Run (); err != nil {
@@ -981,3 +991,28 @@ func (c *DebugCommand) captureError(target string, err error) {
981
991
})
982
992
c .errLock .Unlock ()
983
993
}
994
+
995
+ func (c * DebugCommand ) writeLogs (ctx context.Context ) error {
996
+ out , err := os .Create (filepath .Join (c .flagOutput , "vault.log" ))
997
+ if err != nil {
998
+ return err
999
+ }
1000
+ defer out .Close ()
1001
+
1002
+ logCh , err := c .cachedClient .Sys ().Monitor (ctx , "trace" )
1003
+ if err != nil {
1004
+ return err
1005
+ }
1006
+
1007
+ for {
1008
+ select {
1009
+ case log := <- logCh :
1010
+ _ , err = out .WriteString (log )
1011
+ if err != nil {
1012
+ return err
1013
+ }
1014
+ case <- ctx .Done ():
1015
+ return nil
1016
+ }
1017
+ }
1018
+ }
0 commit comments