Skip to content

Commit aafba9b

Browse files
feat: update logging configuration and enhance Docker setup for api_gateway and service modules
1 parent 945cb25 commit aafba9b

10 files changed

Lines changed: 421 additions & 25 deletions

File tree

api_gateway/Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FROM golang:1.21-alpine AS builder
1+
FROM golang:1.24-alpine AS builder
22
WORKDIR /app
3-
COPY main.go .
3+
COPY api_gateway/go.mod ./
4+
COPY utils/ ../utils/
5+
COPY api_gateway/main.go .
46
RUN go build -o api_gateway main.go
57

68
FROM alpine:latest
7-
RUN apk --no-cache add ca-certificates
89
WORKDIR /root/
910
COPY --from=builder /app/api_gateway .
10-
RUN chmod +x ./api_gateway
1111
CMD ["./api_gateway"]

api_gateway/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module api_gateway
22

3+
replace github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils => ../utils
4+
35
go 1.24
6+
7+
require github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils v0.0.0-00010101000000-000000000000

api_gateway/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package main
22

33
import (
4+
"github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils/log"
45
"log/slog"
56
)
67

78
func main() {
8-
slog.Info("api_gateway module started")
9+
log.InitAsJson()
10+
11+
slog.Info("api_gateway module started", "module", "api_gateway")
12+
slog.Debug("api_gateway module started", "module", "api_gateway")
13+
slog.Warn("api_gateway module warning", "module", "api_gateway")
14+
slog.Error("api_gateway module error", "module", "api_gateway")
915
}

docker-compose.yml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
services:
2-
api-gateway:
3-
build:
4-
context: ./api_gateway
5-
dockerfile: Dockerfile
6-
container_name: api_gateway
7-
environment:
8-
- LOG_LEVEL=info
9-
102
service:
113
build:
12-
context: ./service
13-
dockerfile: Dockerfile
4+
context: .
5+
dockerfile: service/Dockerfile
146
container_name: service
157
environment:
168
- LOG_LEVEL=debug
9+
- LOG_ADD_SOURCE=true
10+
11+
api-gateway:
12+
build:
13+
context: .
14+
dockerfile: api_gateway/Dockerfile
15+
container_name: api_gateway
16+
environment:
17+
- LOG_LEVEL=info
18+
- LOG_ADD_SOURCE=true

service/Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FROM golang:1.21-alpine AS builder
1+
FROM golang:1.24-alpine AS builder
22
WORKDIR /app
3-
COPY main.go .
3+
COPY service/go.mod ./
4+
COPY utils/ ../utils/
5+
COPY service/main.go .
46
RUN go build -o service main.go
57

68
FROM alpine:latest
7-
RUN apk --no-cache add ca-certificates
89
WORKDIR /root/
910
COPY --from=builder /app/service .
10-
RUN chmod +x ./service
1111
CMD ["./service"]

service/go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module service
22

3-
replace github.com/MarcoFontana48/utils => ../utils
3+
replace github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils => ../utils
44

55
go 1.24
6+
7+
require github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils v0.0.0-00010101000000-000000000000

service/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package main
22

3+
import (
4+
"github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils/log"
5+
"log/slog"
6+
)
7+
38
func main() {
4-
//TODO
9+
log.InitAsJson()
10+
11+
slog.Info("service module started", "module", "service")
12+
slog.Debug("service module started", "module", "service")
13+
slog.Warn("service module warning", "module", "service")
14+
slog.Error("service module error", "module", "service")
515
}

utils/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module utils
1+
module github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils
22

33
go 1.24

utils/log.go renamed to utils/log/log.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ package log
33
import (
44
"log/slog"
55
"os"
6+
"strconv"
67
"strings"
78
)
89

9-
func Init() {
10-
levelStr := os.Getenv("LOG_LEVEL")
10+
func InitAsJson() {
11+
addSource, err := strconv.ParseBool(os.Getenv("LOG_ADD_SOURCE"))
12+
if err != nil {
13+
addSource = false // default to false if parsing fails
14+
}
15+
1116
var level slog.Level
17+
levelStr := os.Getenv("LOG_LEVEL")
1218
switch strings.ToLower(levelStr) {
1319
case "debug":
1420
level = slog.LevelDebug
@@ -21,7 +27,9 @@ func Init() {
2127
}
2228

2329
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
24-
Level: level,
30+
Level: level,
31+
AddSource: addSource, // includes function, file and line number in log entries (default: false)
2532
})
33+
2634
slog.SetDefault(slog.New(handler))
2735
}

0 commit comments

Comments
 (0)