File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- FROM golang:1.21 -alpine AS builder
1+ FROM golang:1.24 -alpine AS builder
22WORKDIR /app
3- COPY main.go .
3+ COPY api_gateway/go.mod ./
4+ COPY utils/ ../utils/
5+ COPY api_gateway/main.go .
46RUN go build -o api_gateway main.go
57
68FROM alpine:latest
7- RUN apk --no-cache add ca-certificates
89WORKDIR /root/
910COPY --from=builder /app/api_gateway .
10- RUN chmod +x ./api_gateway
1111CMD ["./api_gateway" ]
Original file line number Diff line number Diff line change 11module api_gateway
22
3+ replace github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils => ../utils
4+
35go 1.24
6+
7+ require github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils v0.0.0-00010101000000-000000000000
Original file line number Diff line number Diff line change 11package main
22
33import (
4+ "github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils/log"
45 "log/slog"
56)
67
78func 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}
Original file line number Diff line number Diff line change 11services :
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
Original file line number Diff line number Diff line change 1- FROM golang:1.21 -alpine AS builder
1+ FROM golang:1.24 -alpine AS builder
22WORKDIR /app
3- COPY main.go .
3+ COPY service/go.mod ./
4+ COPY utils/ ../utils/
5+ COPY service/main.go .
46RUN go build -o service main.go
57
68FROM alpine:latest
7- RUN apk --no-cache add ca-certificates
89WORKDIR /root/
910COPY --from=builder /app/service .
10- RUN chmod +x ./service
1111CMD ["./service" ]
Original file line number Diff line number Diff line change 11module service
22
3- replace github.com/MarcoFontana48/utils => ../utils
3+ replace github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/ utils => ../utils
44
55go 1.24
6+
7+ require github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils v0.0.0-00010101000000-000000000000
Original file line number Diff line number Diff line change 11package main
22
3+ import (
4+ "github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/utils/log"
5+ "log/slog"
6+ )
7+
38func 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}
Original file line number Diff line number Diff line change 1- module utils
1+ module github.com/MarcoFontana48/AUSL-Romagna-CCE-Microservices-Project-Proposal/ utils
22
33go 1.24
Original file line number Diff line number Diff line change @@ -3,12 +3,18 @@ package log
33import (
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}
You can’t perform that action at this time.
0 commit comments