Skip to content

Commit 21cc812

Browse files
committed
feat: make logging configurable
1 parent 3c7cb1b commit 21cc812

File tree

8 files changed

+55
-24
lines changed

8 files changed

+55
-24
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM golang:1.22
2+
3+
WORKDIR /app
4+
COPY go.mod go.sum ./
5+
RUN go mod download
6+
7+
COPY . ./
8+
9+
RUN CGO_ENABLED=0 GOOS=linux go build -o /frigabun
10+
EXPOSE 9595
11+
12+
CMD ["/frigabun"]

config.sample.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
[api]
22
port = 9595
3+
# log /api/status health check endpoint requests
34
enableStatusLog = false
5+
# true for pretty, false for json logging
6+
prettyLog = true
7+
# log level, debug/info
8+
logLevel = "info"
9+
410

511
[gandi]
6-
enabled = true
12+
enabled = false
713
baseUrl = "https://dns.api.gandi.net/api/v5"
814
ttl = 1800
915
apiKey = ""

internal/api/api.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ func NewUpdateApi(dnsServiceFactory factory.ServiceFactory) *UpdateApi {
3535
func (u *UpdateApi) HandleUpdateRequest(c echo.Context) error {
3636
var request UpdateRequest
3737

38-
logger := log.With().Str("subdomains", request.Subdomains).Str("domain", request.Domain).Str("IP", request.IP).Logger()
39-
4038
err := c.Bind(&request)
4139
if err != nil {
4240
log.Error().Err(err).Msg(ErrCannotParseRequest.Error())
4341
return c.String(http.StatusBadRequest, ErrCannotParseRequest.Error())
4442
}
4543

46-
logger.Info().Msg("request received")
44+
logger := log.With().Str("subdomains", request.Subdomains).Str("domain", request.Domain).Str("IP", request.IP).Logger()
45+
logger.Info().Msg("dns update request received")
4746

4847
err = validateRequest(request.Domain, request.IP)
4948
if err != nil {
@@ -53,14 +52,15 @@ func (u *UpdateApi) HandleUpdateRequest(c echo.Context) error {
5352

5453
subdomains := strings.Split(request.Subdomains, ",")
5554

56-
successfulUpdates := 0
55+
count := len(subdomains)
5756

58-
if len(subdomains) == 0 || subdomains[0] == "" {
57+
if count == 0 || subdomains[0] == "" {
5958
logger.Error().Err(ErrMissingParameter).Msg(ErrMissingParameter.Error())
6059
return c.String(http.StatusBadRequest, ErrMissingParameter.Error())
6160
}
6261

6362
for i := range subdomains {
63+
logger.Debug().Msgf("handling subdomain %d of %d", i+1, len(subdomains))
6464

6565
service, err := u.dnsServiceFactory.Find(services.Registrar(request.Registrar))
6666
if err != nil {
@@ -80,13 +80,11 @@ func (u *UpdateApi) HandleUpdateRequest(c echo.Context) error {
8080
return c.String(http.StatusInternalServerError, err.Error())
8181
}
8282

83-
successfulUpdates++
8483
}
8584

86-
logger.Info().Int("updates", successfulUpdates).Msg("successfully created")
87-
88-
return c.String(http.StatusOK, fmt.Sprintf("created %d entries for subdomains %s on %s: %s", successfulUpdates, request.Subdomains, request.Domain, request.IP))
85+
logger.Info().Int("updates", count).Msg("successfully created")
8986

87+
return c.String(http.StatusOK, fmt.Sprintf("created %d entries for subdomains %s on %s: %s", count, request.Subdomains, request.Domain, request.IP))
9088
}
9189

9290
func (u *UpdateApi) HandleStatusCheck(c echo.Context) error {

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"github.com/davidramiro/frigabun/services/factory"
77
"github.com/labstack/echo/v4"
88
"github.com/labstack/echo/v4/middleware"
9+
"github.com/rs/zerolog"
910
"github.com/rs/zerolog/log"
1011
"github.com/spf13/viper"
1112
"strings"
13+
"time"
1214
)
1315

1416
func main() {
@@ -24,7 +26,18 @@ func main() {
2426
log.Fatal().Err(err).Msg("could not read config file")
2527
}
2628

27-
log.Info().Msg("setting up server")
29+
if viper.GetBool("api.prettyLog") {
30+
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
31+
}
32+
33+
switch viper.GetString("api.logLevel") {
34+
case "info":
35+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
36+
case "debug":
37+
zerolog.SetGlobalLevel(zerolog.DebugLevel)
38+
}
39+
40+
log.Info().Msg("starting frigabun")
2841

2942
e := echo.New()
3043
e.HideBanner = true

services/cloudflare.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *CloudflareDnsUpdateService) UpdateRecord(request *DynDnsRequest) error
6868
c.zoneId)
6969

7070
logger := log.With().Str("func", "UpdateRecord").Str("registrar", "cloudflare").Str("endpoint", endpoint).Str("domain", request.Domain).Str("subdomain", request.Subdomain).Logger()
71-
logger.Info().Msg("building update request")
71+
logger.Debug().Msg("building update request")
7272

7373
req, err := http.NewRequest("GET", endpoint, nil)
7474

@@ -99,6 +99,7 @@ func (c *CloudflareDnsUpdateService) UpdateRecord(request *DynDnsRequest) error
9999
var id string
100100

101101
if len(r.Errors) == 0 && len(r.Result) > 0 {
102+
logger.Debug().Int("entries", len(r.Result)).Msg("comparing entries with update request")
102103
for _, e := range r.Result {
103104
if e.Name == fmt.Sprintf("%s.%s", request.Subdomain, request.Domain) {
104105
id = e.Id
@@ -127,7 +128,7 @@ func (c *CloudflareDnsUpdateService) newRecord(request *DynDnsRequest) error {
127128
c.zoneId)
128129

129130
logger := log.With().Str("func", "newRecord").Str("registrar", "cloudflare").Str("subdomain", cloudflareRequest.Subdomain).Str("endpoint", endpoint).Str("IP", cloudflareRequest.IP).Logger()
130-
logger.Info().Msg("building new record request")
131+
logger.Debug().Msg("building new record request")
131132

132133
body, err := json.Marshal(cloudflareRequest)
133134
if err != nil {
@@ -157,7 +158,7 @@ func (c *CloudflareDnsUpdateService) newRecord(request *DynDnsRequest) error {
157158
return ErrRegistrarRejectedRequest
158159
}
159160

160-
logger.Info().Msg("request for new record successful")
161+
logger.Debug().Msg("request for new record successful")
161162

162163
return nil
163164
}

services/factory/dns_update_service_factory.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package factory
22

33
import (
44
"github.com/davidramiro/frigabun/services"
5+
"github.com/rs/zerolog/log"
56
"github.com/spf13/viper"
67
)
78

@@ -16,11 +17,14 @@ type DnsUpdateServiceFactory struct {
1617
}
1718

1819
func NewDnsUpdateServiceFactory() (*DnsUpdateServiceFactory, error) {
20+
log.Debug().Msg("initializing dns service factory")
21+
1922
factory := &DnsUpdateServiceFactory{
2023
services: make(map[services.Registrar]services.DnsUpdateService),
2124
}
2225

2326
if viper.GetBool("cloudflare.enabled") {
27+
log.Debug().Msg("cloudflare enabled, registering")
2428
cloudflareService, err := services.NewCloudflareDnsUpdateService(nil)
2529
if err != nil {
2630
return nil, err
@@ -30,6 +34,7 @@ func NewDnsUpdateServiceFactory() (*DnsUpdateServiceFactory, error) {
3034
}
3135

3236
if viper.GetBool("gandi.enabled") {
37+
log.Debug().Msg("gandi enabled, registering")
3338
gandiService, err := services.NewGandiDnsUpdateService(nil)
3439
if err != nil {
3540
return nil, err
@@ -39,6 +44,7 @@ func NewDnsUpdateServiceFactory() (*DnsUpdateServiceFactory, error) {
3944
}
4045

4146
if viper.GetBool("porkbun.enabled") {
47+
log.Debug().Msg("porkbun enabled, registering")
4248
porkbunService, err := services.NewPorkbunDnsUpdateService(nil)
4349
if err != nil {
4450
return nil, err
@@ -47,6 +53,10 @@ func NewDnsUpdateServiceFactory() (*DnsUpdateServiceFactory, error) {
4753
factory.Register(porkbunService)
4854
}
4955

56+
if len(factory.services) == 0 {
57+
log.Fatal().Msg("no services registered, config invalid")
58+
}
59+
5060
return factory, nil
5161
}
5262

@@ -61,6 +71,7 @@ func (df *DnsUpdateServiceFactory) Register(service services.DnsUpdateService) {
6171
}
6272

6373
func (df *DnsUpdateServiceFactory) Find(registrar services.Registrar) (service services.DnsUpdateService, err error) {
74+
log.Debug().Interface("registrar", registrar).Msg("fetching dns service from factory")
6475

6576
service, ok := df.services[registrar]
6677
if !ok {

services/gandi.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ type GandiApiRequest struct {
4848
IPValues []string `json:"rrset_values"`
4949
}
5050

51-
type GandiUpdateError struct {
52-
Code int
53-
Message string
54-
}
55-
5651
func (g *GandiDnsUpdateService) UpdateRecord(request *DynDnsRequest) error {
5752

5853
gandiRequest := &GandiApiRequest{

services/porkbun.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ type PorkbunQueryResponse struct {
6060
} `json:"records"`
6161
}
6262

63-
type PorkbunUpdateError struct {
64-
Code int
65-
Message string
66-
}
67-
6863
func (p *PorkbunDnsUpdateService) UpdateRecord(request *DynDnsRequest) error {
6964

7065
porkbunRequest := &PorkbunApiRequest{

0 commit comments

Comments
 (0)