Skip to content

Commit

Permalink
NOISSUE - Update logger to slog (#5)
Browse files Browse the repository at this point in the history
* update logger to slog

Signed-off-by: ianmuchyri <[email protected]>

* update loggers

Signed-off-by: ianmuchyri <[email protected]>

* update error to use timestamp not layout

Signed-off-by: ianmuchyri <[email protected]>

---------

Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri authored Feb 2, 2024
1 parent 2e9676f commit cc619e6
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 171 deletions.
37 changes: 25 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/x509"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/absmach/agent/pkg/bootstrap"
"github.com/absmach/agent/pkg/conn"
"github.com/absmach/agent/pkg/edgex"
"github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
"github.com/absmach/magistrala/pkg/messaging/brokers"
"github.com/caarlos0/env/v9"
Expand Down Expand Up @@ -81,19 +81,19 @@ func main() {
log.Fatalf(fmt.Sprintf("Failed to load config: %s", err))
}

logger, err := logger.New(os.Stdout, cfg.Log.Level)
logger, err := initLogger(c.LogLevel)
if err != nil {
log.Fatalf(fmt.Sprintf("Failed to create logger: %s", err))
}

cfg, err = loadBootConfig(c, cfg, logger)
if err != nil {
logger.Error(fmt.Sprintf("Failed to load config: %s", err))
logger.Error("Failed to load config", slog.Any("error", err))
}

pubsub, err := brokers.NewPubSub(ctx, cfg.Server.BrokerURL, logger)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to connect to Broker: %s %s", err, cfg.Server.BrokerURL))
log.Fatal("Failed to connect to Broker", slog.Any("error", err), slog.String("broker_url", cfg.Server.BrokerURL))
}
defer pubsub.Close()

Expand All @@ -106,7 +106,7 @@ func main() {

svc, err := agent.New(ctx, mqttClient, &cfg, edgexClient, pubsub, logger)
if err != nil {
logger.Error(fmt.Sprintf("Error in agent service: %s", err))
logger.Error("Error in agent service", slog.Any("error", err))
return
}

Expand Down Expand Up @@ -138,7 +138,7 @@ func main() {
})

g.Go(func() error {
logger.Info(fmt.Sprintf("Agent service started, exposed port %s", cfg.Server.Port))
logger.Info("Agent service started", slog.String("port", cfg.Server.Port))
return srv.ListenAndServe()
})

Expand All @@ -147,7 +147,7 @@ func main() {
})

if err := g.Wait(); err != nil {
logger.Error(fmt.Sprintf("Agent terminated: %v", err))
logger.Error("Agent terminated", slog.Any("error", err))
}
}

Expand Down Expand Up @@ -225,7 +225,7 @@ func loadEnvConfig(cfg config) (agent.Config, error) {
return c, nil
}

func loadBootConfig(cfg config, c agent.Config, logger logger.Logger) (agent.Config, error) {
func loadBootConfig(cfg config, c agent.Config, logger *slog.Logger) (agent.Config, error) {
file := cfg.ConfigFile
skipTLS, err := strconv.ParseBool(cfg.BootstrapSkipTLS)
if err != nil {
Expand Down Expand Up @@ -267,14 +267,14 @@ func loadBootConfig(cfg config, c agent.Config, logger logger.Logger) (agent.Con
return bsc, nil
}

func connectToMQTTBroker(conf agent.MQTTConfig, logger logger.Logger) (mqtt.Client, error) {
func connectToMQTTBroker(conf agent.MQTTConfig, logger *slog.Logger) (mqtt.Client, error) {
name := fmt.Sprintf("agent-%s", conf.Username)
conn := func(client mqtt.Client) {
logger.Info(fmt.Sprintf("Client %s connected", name))
logger.Info("Client connected", slog.String("client_name", name))
}

lost := func(client mqtt.Client, err error) {
logger.Info(fmt.Sprintf("Client %s disconnected", name))
logger.Info("Client disconnected", slog.String("client_name", name))
}

opts := mqtt.NewClientOptions().
Expand Down Expand Up @@ -373,7 +373,7 @@ func loadCertificate(cnfg agent.MQTTConfig) (agent.MQTTConfig, error) {
return c, nil
}

func StopSignalHandler(ctx context.Context, cancel context.CancelFunc, logger logger.Logger, svcName string, server *http.Server) error {
func StopSignalHandler(ctx context.Context, cancel context.CancelFunc, logger *slog.Logger, svcName string, server *http.Server) error {
c := make(chan os.Signal, 2)
signal.Notify(c, syscall.SIGINT, syscall.SIGABRT)
select {
Expand All @@ -389,3 +389,16 @@ func StopSignalHandler(ctx context.Context, cancel context.CancelFunc, logger lo
return nil
}
}

func initLogger(levelText string) (*slog.Logger, error) {
var level slog.Level
if err := level.UnmarshalText([]byte(levelText)); err != nil {
return &slog.Logger{}, fmt.Errorf(`{"level":"error","message":"%s: %s","ts":"%s"}`, err, levelText, time.Now())
}

logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})

return slog.New(logHandler), nil
}
39 changes: 18 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module github.com/absmach/agent

go 1.21

toolchain go1.21.0
go 1.21.5

require (
github.com/absmach/magistrala v0.0.0-20240110171157-9f573850fc4b
github.com/absmach/magistrala v0.14.0
github.com/absmach/senml v1.0.5
github.com/caarlos0/env/v9 v9.0.0
github.com/creack/pty v1.1.18
Expand All @@ -14,11 +12,11 @@ require (
github.com/go-kit/kit v0.13.0
github.com/go-zoo/bone v1.3.0
github.com/mainflux/export v0.1.1-0.20230724124847-67d0bc7f38cb
github.com/nats-io/nats.go v1.31.0
github.com/nats-io/nats.go v1.32.0
github.com/pelletier/go-toml v1.9.5
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.4.0
golang.org/x/sync v0.6.0
robpike.io/filter v0.0.0-20150108201509-2984852a2183
)

Expand All @@ -35,9 +33,8 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/mainflux/mainflux v0.12.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand All @@ -49,11 +46,11 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/grpc v1.60.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

Expand All @@ -65,19 +62,19 @@ require (
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/nats-io/nkeys v0.4.6 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/ory/dockertest/v3 v3.10.0
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit cc619e6

Please sign in to comment.