@@ -121,7 +121,7 @@ all the above actions will be skipped, because the Elastic Agent has already bee
121121occurs on every start of the container set FLEET_FORCE to 1.
122122` ,
123123 Run : func (c * cobra.Command , args []string ) {
124- if err := containerCmd (streams , c ); err != nil {
124+ if err := logContainerCmd (streams , c ); err != nil {
125125 logError (streams , err )
126126 os .Exit (1 )
127127 }
@@ -138,6 +138,25 @@ func logInfo(streams *cli.IOStreams, msg string) {
138138 fmt .Fprintln (streams .Out , msg )
139139}
140140
141+ func logContainerCmd (streams * cli.IOStreams , cmd * cobra.Command ) error {
142+ logsPath := envWithDefault ("" , "LOGS_PATH" )
143+ if logsPath != "" {
144+ // log this entire command to a file as well as to the passed streams
145+ if err := os .MkdirAll (logsPath , 0755 ); err != nil {
146+ return fmt .Errorf ("preparing LOGS_PATH(%s) failed: %s" , logsPath , err )
147+ }
148+ logPath := filepath .Join (logsPath , "elastic-agent-startup.log" )
149+ w , err := os .Create (logPath )
150+ if err != nil {
151+ return fmt .Errorf ("opening startup log(%s) failed: %s" , logPath , err )
152+ }
153+ defer w .Close ()
154+ streams .Out = io .MultiWriter (streams .Out , w )
155+ streams .Err = io .MultiWriter (streams .Out , w )
156+ }
157+ return containerCmd (streams , cmd )
158+ }
159+
141160func containerCmd (streams * cli.IOStreams , cmd * cobra.Command ) error {
142161 // set paths early so all action below use the defined paths
143162 if err := setPaths (); err != nil {
@@ -274,8 +293,8 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig
274293 return err
275294 }
276295 enroll := exec .Command (executable , cmdArgs ... )
277- enroll .Stdout = os . Stdout
278- enroll .Stderr = os . Stderr
296+ enroll .Stdout = streams . Out
297+ enroll .Stderr = streams . Err
279298 err = enroll .Start ()
280299 if err != nil {
281300 return errors .New ("failed to execute enrollment command" , err )
0 commit comments