Skip to content

Commit 838d5a3

Browse files
committed
REORG/MINOR: log: move logging initialization to main
1 parent 79e8477 commit 838d5a3

File tree

2 files changed

+77
-69
lines changed

2 files changed

+77
-69
lines changed

cmd/dataplaneapi/main.go

+73-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ package main
1717

1818
import (
1919
"fmt"
20+
"io/ioutil"
2021
"os"
2122
"path"
23+
"path/filepath"
2224
"syscall"
2325

2426
loads "github.com/go-openapi/loads"
@@ -31,6 +33,7 @@ import (
3133
"github.com/haproxytech/dataplaneapi"
3234
"github.com/haproxytech/dataplaneapi/configuration"
3335
"github.com/haproxytech/dataplaneapi/operations"
36+
"github.com/haproxytech/dataplaneapi/syslog"
3437
)
3538

3639
// GitRepo ...
@@ -140,10 +143,6 @@ func startServer(cfg *configuration.Configuration) (reload configuration.AtomicB
140143
log.Fatalln(err)
141144
}
142145

143-
log.Infof("HAProxy Data Plane API %s %s%s", GitTag, GitCommit, GitDirty)
144-
log.Infof("Build from: %s", GitRepo)
145-
log.Infof("Build date: %s", BuildTime)
146-
147146
configuration.HandlePIDFile(cfg.HAProxy)
148147

149148
if cfg.Mode.Load() == "cluster" {
@@ -173,6 +172,12 @@ func startServer(cfg *configuration.Configuration) (reload configuration.AtomicB
173172
}
174173
}
175174

175+
setupLogging(cfg)
176+
177+
log.Infof("HAProxy Data Plane API %s %s%s", GitTag, GitCommit, GitDirty)
178+
log.Infof("Build from: %s", GitRepo)
179+
log.Infof("Build date: %s", BuildTime)
180+
176181
err = cfg.Save()
177182
if err != nil {
178183
log.Fatalln(err)
@@ -226,3 +231,67 @@ func startServer(cfg *configuration.Configuration) (reload configuration.AtomicB
226231

227232
return reload
228233
}
234+
235+
func setupLogging(cfg *configuration.Configuration) {
236+
appLogger := log.StandardLogger()
237+
configureLogger(appLogger, cfg.Logging, func(opts configuration.SyslogOptions) configuration.SyslogOptions {
238+
opts.SyslogMsgID = "app"
239+
return opts
240+
}(cfg.Syslog))
241+
242+
accLogger := log.StandardLogger()
243+
configureLogger(accLogger, cfg.Logging, func(opts configuration.SyslogOptions) configuration.SyslogOptions {
244+
opts.SyslogMsgID = "accesslog"
245+
return opts
246+
}(cfg.Syslog))
247+
248+
dataplaneapi.AppLogger = appLogger
249+
dataplaneapi.AccLogger = accLogger
250+
}
251+
252+
func configureLogger(logger *log.Logger, loggingOptions configuration.LoggingOptions, syslogOptions configuration.SyslogOptions) {
253+
switch loggingOptions.LogFormat {
254+
case "text":
255+
logger.SetFormatter(&log.TextFormatter{
256+
FullTimestamp: true,
257+
DisableColors: true,
258+
})
259+
case "JSON":
260+
logger.SetFormatter(&log.JSONFormatter{})
261+
}
262+
263+
switch loggingOptions.LogTo {
264+
case "stdout":
265+
logger.SetOutput(os.Stdout)
266+
case "file":
267+
dir := filepath.Dir(loggingOptions.LogFile)
268+
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
269+
log.Warning("Error opening log file, no logging implemented: " + err.Error())
270+
}
271+
//nolint:govet
272+
logFile, err := os.OpenFile(loggingOptions.LogFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
273+
if err != nil {
274+
log.Warning("Error opening log file, no logging implemented: " + err.Error())
275+
}
276+
log.SetOutput(logFile)
277+
case "syslog":
278+
logger.SetOutput(ioutil.Discard)
279+
hook, err := syslog.NewRFC5424Hook(syslogOptions)
280+
if err != nil {
281+
log.Warningf("Error configuring Syslog logging: %s", err.Error())
282+
break
283+
}
284+
logger.AddHook(hook)
285+
}
286+
287+
switch loggingOptions.LogLevel {
288+
case "debug":
289+
logger.SetLevel(log.DebugLevel)
290+
case "info":
291+
logger.SetLevel(log.InfoLevel)
292+
case "warning":
293+
logger.SetLevel(log.WarnLevel)
294+
case "error":
295+
logger.SetLevel(log.ErrorLevel)
296+
}
297+
}

configure_data_plane.go

+4-65
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ import (
2222
"crypto/tls"
2323
"encoding/json"
2424
"fmt"
25-
"io/ioutil"
2625
"net/http"
2726
"os"
2827
"os/signal"
29-
"path/filepath"
3028
"runtime/debug"
3129
"strconv"
3230
"strings"
@@ -47,7 +45,6 @@ import (
4745
"github.com/haproxytech/client-native/v2/storage"
4846
parser "github.com/haproxytech/config-parser/v4"
4947
"github.com/haproxytech/config-parser/v4/types"
50-
"github.com/haproxytech/dataplaneapi/syslog"
5148
"github.com/rs/cors"
5249
log "github.com/sirupsen/logrus"
5350

@@ -76,6 +73,8 @@ var (
7673
BuildTime string
7774
mWorker = false
7875
logFile *os.File
76+
AppLogger *log.Logger
77+
AccLogger *log.Logger
7978
)
8079

8180
func configureFlags(api *operations.DataPlaneAPI) {
@@ -662,26 +661,13 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
662661
}
663662
}()
664663

664+
applicationEntry := log.NewEntry(AppLogger)
665+
accessEntry := log.NewEntry(AccLogger)
665666
al, err := cfg.ApacheLogFormat()
666667
if err != nil {
667668
println("Cannot setup custom Apache Log Format", err.Error())
668669
}
669670

670-
appLogger := log.StandardLogger()
671-
configureLogging(appLogger, cfg.Logging, func(opts dataplaneapi_config.SyslogOptions) dataplaneapi_config.SyslogOptions {
672-
opts.SyslogMsgID = "app"
673-
return opts
674-
}(cfg.Syslog))
675-
676-
accLogger := log.StandardLogger()
677-
configureLogging(accLogger, cfg.Logging, func(opts dataplaneapi_config.SyslogOptions) dataplaneapi_config.SyslogOptions {
678-
opts.SyslogMsgID = "accesslog"
679-
return opts
680-
}(cfg.Syslog))
681-
682-
applicationEntry := log.NewEntry(appLogger)
683-
accessEntry := log.NewEntry(accLogger)
684-
685671
// middlewares
686672
var adpts []adapters.Adapter
687673
adpts = append(adpts,
@@ -738,53 +724,6 @@ func setupGlobalMiddleware(handler http.Handler, adapters ...adapters.Adapter) h
738724
return handler
739725
}
740726

741-
func configureLogging(logger *log.Logger, loggingOptions dataplaneapi_config.LoggingOptions, syslogOptions dataplaneapi_config.SyslogOptions) {
742-
switch loggingOptions.LogFormat {
743-
case "text":
744-
logger.SetFormatter(&log.TextFormatter{
745-
FullTimestamp: true,
746-
DisableColors: true,
747-
})
748-
case "JSON":
749-
logger.SetFormatter(&log.JSONFormatter{})
750-
}
751-
752-
switch loggingOptions.LogTo {
753-
case "stdout":
754-
logger.SetOutput(os.Stdout)
755-
case "file":
756-
dir := filepath.Dir(loggingOptions.LogFile)
757-
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
758-
logger.Warning("Error opening log file, no logging implemented: " + err.Error())
759-
}
760-
//nolint:govet
761-
logFile, err := os.OpenFile(loggingOptions.LogFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
762-
if err != nil {
763-
log.Warning("Error opening log file, no logging implemented: " + err.Error())
764-
}
765-
log.SetOutput(logFile)
766-
case "syslog":
767-
logger.SetOutput(ioutil.Discard)
768-
hook, err := syslog.NewRFC5424Hook(syslogOptions)
769-
if err != nil {
770-
logger.Warningf("Error configuring Syslog logging: %s", err.Error())
771-
break
772-
}
773-
logger.AddHook(hook)
774-
}
775-
776-
switch loggingOptions.LogLevel {
777-
case "debug":
778-
logger.SetLevel(log.DebugLevel)
779-
case "info":
780-
logger.SetLevel(log.InfoLevel)
781-
case "warning":
782-
logger.SetLevel(log.WarnLevel)
783-
case "error":
784-
logger.SetLevel(log.ErrorLevel)
785-
}
786-
}
787-
788727
func serverShutdown() {
789728
cfg := dataplaneapi_config.Get()
790729
if logFile != nil {

0 commit comments

Comments
 (0)