@@ -172,7 +172,14 @@ func cmdStart(fl Flags) (int, error) {
172172func cmdRun (fl Flags ) (int , error ) {
173173 caddy .TrapSignals ()
174174
175- logger := caddy .Log ()
175+ // set up buffered logging for early startup
176+ // so that we can hold onto logs until after
177+ // the config is loaded (or fails to load)
178+ // so that we can write the logs to the user's
179+ // configured output. we must be sure to flush
180+ // on any error before the config is loaded.
181+ logger , defaultLogger , logBuffer := caddy .BufferedLog ()
182+
176183 undoMaxProcs := setResourceLimits (logger )
177184 defer undoMaxProcs ()
178185
@@ -187,6 +194,7 @@ func cmdRun(fl Flags) (int, error) {
187194 // load all additional envs as soon as possible
188195 err := handleEnvFileFlag (fl )
189196 if err != nil {
197+ logBuffer .FlushTo (defaultLogger )
190198 return caddy .ExitCodeFailedStartup , err
191199 }
192200
@@ -204,6 +212,7 @@ func cmdRun(fl Flags) (int, error) {
204212 logger .Info ("no autosave file exists" , zap .String ("autosave_file" , caddy .ConfigAutosavePath ))
205213 resumeFlag = false
206214 } else if err != nil {
215+ logBuffer .FlushTo (defaultLogger )
207216 return caddy .ExitCodeFailedStartup , err
208217 } else {
209218 if configFlag == "" {
@@ -222,6 +231,7 @@ func cmdRun(fl Flags) (int, error) {
222231 if ! resumeFlag {
223232 config , configFile , err = LoadConfig (configFlag , configAdapterFlag )
224233 if err != nil {
234+ logBuffer .FlushTo (defaultLogger )
225235 return caddy .ExitCodeFailedStartup , err
226236 }
227237 }
@@ -239,8 +249,15 @@ func cmdRun(fl Flags) (int, error) {
239249 // run the initial config
240250 err = caddy .Load (config , true )
241251 if err != nil {
252+ logBuffer .FlushTo (defaultLogger )
242253 return caddy .ExitCodeFailedStartup , fmt .Errorf ("loading initial config: %v" , err )
243254 }
255+
256+ // at this stage the config will have replaced
257+ // the default logger to the configured one, so
258+ // we can now flush the buffered logs, then log
259+ // that the config is running.
260+ logBuffer .FlushTo (caddy .Log ())
244261 logger .Info ("serving initial configuration" )
245262
246263 // if we are to report to another process the successful start
0 commit comments