From d793c7813d254db8218b4f4bed22beb003d8db24 Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Tue, 28 Jan 2020 21:08:43 +0000 Subject: [PATCH] feat(): logs refactoring --- main.go | 16 ++++++++-------- pkg/api/index.go | 4 ++-- pkg/model/work_request.go | 4 ++-- pkg/notification/http_notifier.go | 4 ++-- pkg/notification/main.go | 2 +- pkg/notification/smtp_notifier.go | 4 ++-- pkg/worker/dispatcher.go | 4 ++-- pkg/worker/work_runner.go | 16 ++++++++-------- pkg/worker/worker.go | 4 ++-- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index eb00e09..e406ad8 100644 --- a/main.go +++ b/main.go @@ -38,17 +38,17 @@ func main() { conf.LogDir = os.TempDir() } - logger.Debug.Println("Starting webhookd server...") + logger.Debug.Println("starting webhookd server...") srv := server.NewServer(conf) // Configure notification if err := notification.Init(conf.NotificationURI); err != nil { - logger.Error.Fatalf("Unable to create notification channel: %v\n", err) + logger.Error.Fatalf("unable to create notification channel: %v\n", err) } // Start the dispatcher. - logger.Debug.Printf("Starting the dispatcher (%d workers)...\n", conf.NbWorkers) + logger.Debug.Printf("starting the dispatcher with %d workers...\n", conf.NbWorkers) worker.StartDispatcher(conf.NbWorkers) done := make(chan bool) @@ -57,14 +57,14 @@ func main() { go func() { <-quit - logger.Debug.Println("Server is shutting down...") + logger.Debug.Println("server is shutting down...") api.Shutdown() ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil { - logger.Error.Fatalf("Could not gracefully shutdown the server: %v\n", err) + logger.Error.Fatalf("could not gracefully shutdown the server: %v\n", err) } close(done) }() @@ -73,12 +73,12 @@ func main() { if conf.TLSListenAddr != "" { addr = conf.TLSListenAddr } - logger.Info.Println("Server is ready to handle requests at", addr) + logger.Info.Println("server is ready to handle requests at", addr) api.Start() if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { - logger.Error.Fatalf("Could not listen on %s : %v\n", addr, err) + logger.Error.Fatalf("could not listen on %s : %v\n", addr, err) } <-done - logger.Debug.Println("Server stopped") + logger.Debug.Println("server stopped") } diff --git a/pkg/api/index.go b/pkg/api/index.go index 455dd3a..f391f24 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -67,7 +67,7 @@ func triggerWebhook(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { - logger.Error.Printf("Error reading body: %v", err) + logger.Error.Printf("error reading body: %v", err) http.Error(w, "can't read body", http.StatusBadRequest) return } @@ -108,7 +108,7 @@ func triggerWebhook(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s\n", msg) // Send chunked response } - // Flush the data immediatly instead of buffering it for later. + // Flush the data immediately instead of buffering it for later. flusher.Flush() } } diff --git a/pkg/model/work_request.go b/pkg/model/work_request.go index acb3f40..74cb798 100644 --- a/pkg/model/work_request.go +++ b/pkg/model/work_request.go @@ -69,11 +69,11 @@ func (wr *WorkRequest) Terminate(err error) error { if err != nil { wr.Status = Error wr.Err = err - logger.Info.Printf("Work %s#%d done [ERROR]\n", wr.Name, wr.ID) + logger.Info.Printf("job %s#%d done [ERROR]\n", wr.Name, wr.ID) return err } wr.Status = Success - logger.Info.Printf("Work %s#%d done [SUCCESS]\n", wr.Name, wr.ID) + logger.Info.Printf("job %s#%d done [SUCCESS]\n", wr.Name, wr.ID) return nil } diff --git a/pkg/notification/http_notifier.go b/pkg/notification/http_notifier.go index 4b0fe6a..6088b8b 100644 --- a/pkg/notification/http_notifier.go +++ b/pkg/notification/http_notifier.go @@ -26,7 +26,7 @@ type HTTPNotifier struct { } func newHTTPNotifier(uri *url.URL) *HTTPNotifier { - logger.Info.Println("Using HTTP notification system: ", uri.String()) + logger.Info.Println("using HTTP notification system: ", uri.String()) return &HTTPNotifier{ URL: uri, PrefixFilter: getValueOrAlt(uri.Query(), "prefix", "notify:"), @@ -61,6 +61,6 @@ func (n *HTTPNotifier) Notify(work *model.WorkRequest) error { return err } resp.Body.Close() - logger.Info.Printf("Work %s#%d notified to %s\n", work.Name, work.ID, n.URL.String()) + logger.Info.Printf("job %s#%d notification sent to %s\n", work.Name, work.ID, n.URL.String()) return nil } diff --git a/pkg/notification/main.go b/pkg/notification/main.go index baa8731..9d95b0d 100644 --- a/pkg/notification/main.go +++ b/pkg/notification/main.go @@ -22,7 +22,7 @@ func Notify(work *model.WorkRequest) { return } if err := notifier.Notify(work); err != nil { - logger.Error.Printf("Unable to send notification of work %s#%d: %v\n", work.Name, work.ID, err) + logger.Error.Printf("unable to send notification for webhook %s#%d: %v\n", work.Name, work.ID, err) } } diff --git a/pkg/notification/smtp_notifier.go b/pkg/notification/smtp_notifier.go index 09df02f..ebd6124 100644 --- a/pkg/notification/smtp_notifier.go +++ b/pkg/notification/smtp_notifier.go @@ -20,7 +20,7 @@ type SMTPNotifier struct { } func newSMTPNotifier(uri *url.URL) *SMTPNotifier { - logger.Info.Println("Using SMTP notification system: ", uri.Opaque) + logger.Info.Println("using SMTP notification system: ", uri.Opaque) return &SMTPNotifier{ Host: getValueOrAlt(uri.Query(), "smtp", "localhost:25"), From: getValueOrAlt(uri.Query(), "from", "noreply@nunux.org"), @@ -76,7 +76,7 @@ func (n *SMTPNotifier) Notify(work *model.WorkRequest) error { return err } - logger.Info.Printf("Work %s#%d notified to %s\n", work.Name, work.ID, n.To) + logger.Info.Printf("job %s#%d notification sent to %s\n", work.Name, work.ID, n.To) // Send the QUIT command and close the connection. return c.Quit() diff --git a/pkg/worker/dispatcher.go b/pkg/worker/dispatcher.go index 781a398..16f636c 100644 --- a/pkg/worker/dispatcher.go +++ b/pkg/worker/dispatcher.go @@ -18,7 +18,7 @@ func StartDispatcher(nworkers int) { // Now, create all of our workers. for i := 0; i < nworkers; i++ { - logger.Debug.Println("Starting worker", i+1) + logger.Debug.Printf("starting worker #%d ...\n", i+1) worker := NewWorker(i+1, WorkerQueue) worker.Start() } @@ -30,7 +30,7 @@ func StartDispatcher(nworkers int) { go func() { worker := <-WorkerQueue - logger.Debug.Printf("Dispatching work request: %s#%d", work.Name, work.ID) + logger.Debug.Printf("dispatching job request: %s#%d", work.Name, work.ID) worker <- work }() } diff --git a/pkg/worker/work_runner.go b/pkg/worker/work_runner.go index 70dc6eb..70c0951 100644 --- a/pkg/worker/work_runner.go +++ b/pkg/worker/work_runner.go @@ -25,9 +25,9 @@ func (c *ChanWriter) Write(p []byte) (int, error) { func run(work *model.WorkRequest) error { work.Status = model.Running - logger.Info.Printf("Work %s#%d started...\n", work.Name, work.ID) - logger.Debug.Printf("Work %s#%d script: %s\n", work.Name, work.ID, work.Script) - logger.Debug.Printf("Work %s#%d parameter: %v\n", work.Name, work.ID, work.Args) + logger.Info.Printf("job %s#%d started...\n", work.Name, work.ID) + logger.Debug.Printf("job %s#%d script: %s\n", work.Name, work.ID, work.Script) + logger.Debug.Printf("job %s#%d parameter: %v\n", work.Name, work.ID, work.Args) binary, err := exec.LookPath(work.Script) if err != nil { @@ -47,7 +47,7 @@ func run(work *model.WorkRequest) error { return work.Terminate(err) } defer logFile.Close() - logger.Debug.Printf("Work %s#%d output to file: %s\n", work.Name, work.ID, logFile.Name()) + logger.Debug.Printf("job %s#%d output file: %s\n", work.Name, work.ID, logFile.Name()) wLogFile := bufio.NewWriter(logFile) defer wLogFile.Flush() @@ -82,24 +82,24 @@ func run(work *model.WorkRequest) error { if !work.IsTerminated() { work.MessageChan <- []byte(line) } else { - logger.Error.Printf("Work %s#%d is over. Unable to write more data into the channel: %s\n", work.Name, work.ID, line) + logger.Error.Printf("job %s#%d is over ; unable to write more data into the channel: %s\n", work.Name, work.ID, line) break } // writing to outfile if _, err := wLogFile.WriteString(line + "\n"); err != nil { - logger.Error.Println("Error while writing into the log file:", logFile.Name(), err) + logger.Error.Println("error while writing into the log file:", logFile.Name(), err) break } } if err := scanner.Err(); err != nil { - logger.Error.Printf("Work %s#%d unable to read script stdout: %v\n", work.Name, work.ID, err) + logger.Error.Printf("job %s#%d is unable to read script stdout: %v\n", work.Name, work.ID, err) } wg.Done() }(cmdReader) // Start timeout timer timer := time.AfterFunc(time.Duration(work.Timeout)*time.Second, func() { - logger.Warning.Printf("Work %s#%d has timed out (%ds). Killing process #%d...\n", work.Name, work.ID, work.Timeout, cmd.Process.Pid) + logger.Warning.Printf("job %s#%d has timed out (%ds): killing process #%d ...\n", work.Name, work.ID, work.Timeout, cmd.Process.Pid) syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) }) diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index d2f34a5..297da66 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -43,7 +43,7 @@ func (w Worker) Start() { select { case work := <-w.Work: // Receive a work request. - logger.Debug.Printf("Worker #%d received work request: %s#%d\n", w.ID, work.Name, work.ID) + logger.Debug.Printf("worker #%d received job request: %s#%d\n", w.ID, work.Name, work.ID) metric.Requests.Add(1) err := run(&work) if err != nil { @@ -57,7 +57,7 @@ func (w Worker) Start() { close(work.MessageChan) case <-w.QuitChan: - logger.Debug.Printf("Stopping worker #%d...\n", w.ID) + logger.Debug.Printf("stopping worker #%d...\n", w.ID) return } }