Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
MG-234 - Improve Logging (#255)
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
Signed-off-by: Musilah <[email protected]>
Co-authored-by: Musilah <[email protected]>
Co-authored-by: Dušan Borovčanin <[email protected]>
  • Loading branch information
3 people authored Jan 18, 2024
1 parent 95f31d7 commit 8c084b1
Show file tree
Hide file tree
Showing 118 changed files with 411 additions and 1,453 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ jobs:
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 1.21.x
cache-dependency-path: "go.sum"

- name: Check for changes in specific paths
uses: dorny/paths-filter@v2
id: changes
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ linters-settings:
no-unaliased: true
no-extra-aliases: false
alias:
- pkg: github.com/mainflux/callhome/pkg/client
- pkg: github.com/absmach/callhome/pkg/client
alias: chclient
- pkg: github.com/absmach/magistrala/logger
alias: mglog
Expand Down
5 changes: 3 additions & 2 deletions auth/api/http/domains/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
package domains

import (
"log/slog"

"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/internal/api"
"github.com/absmach/magistrala/internal/apiutil"
mglog "github.com/absmach/magistrala/logger"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func MakeHandler(svc auth.Service, mux *chi.Mux, logger mglog.Logger) *chi.Mux {
func MakeHandler(svc auth.Service, mux *chi.Mux, logger *slog.Logger) *chi.Mux {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)),
}
Expand Down
3 changes: 1 addition & 2 deletions auth/api/http/keys/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ func newService() (auth.Service, *mocks.KeyRepository) {
}

func newServer(svc auth.Service) *httptest.Server {
logger := mglog.NewMock()
mux := httpapi.MakeHandler(svc, logger, "")
mux := httpapi.MakeHandler(svc, mglog.NewMock(), "")
return httptest.NewServer(mux)
}

Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/keys/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package keys
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"strings"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/internal/apiutil"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
Expand All @@ -21,7 +21,7 @@ import (
const contentType = "application/json"

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc auth.Service, mux *chi.Mux, logger mglog.Logger) *chi.Mux {
func MakeHandler(svc auth.Service, mux *chi.Mux, logger *slog.Logger) *chi.Mux {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
}
Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
package http

import (
"log/slog"
"net/http"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/auth/api/http/domains"
"github.com/absmach/magistrala/auth/api/http/keys"
mglog "github.com/absmach/magistrala/logger"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc auth.Service, logger mglog.Logger, instanceID string) http.Handler {
func MakeHandler(svc auth.Service, logger *slog.Logger, instanceID string) http.Handler {
mux := chi.NewRouter()

mux = keys.MakeHandler(svc, mux, logger)
Expand Down
6 changes: 3 additions & 3 deletions auth/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ package api
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/absmach/magistrala/auth"
mglog "github.com/absmach/magistrala/logger"
)

var _ auth.Service = (*loggingMiddleware)(nil)

type loggingMiddleware struct {
logger mglog.Logger
logger *slog.Logger
svc auth.Service
}

// LoggingMiddleware adds logging facilities to the core service.
func LoggingMiddleware(svc auth.Service, logger mglog.Logger) auth.Service {
func LoggingMiddleware(svc auth.Service, logger *slog.Logger) auth.Service {
return &loggingMiddleware{logger, svc}
}

Expand Down
6 changes: 3 additions & 3 deletions auth/spicedb/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"context"
"fmt"
"io"
"log/slog"

"github.com/absmach/magistrala/auth"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
Expand All @@ -33,10 +33,10 @@ var (
type policyAgent struct {
client *authzed.ClientWithExperimental
permissionClient v1.PermissionsServiceClient
logger mglog.Logger
logger *slog.Logger
}

func NewPolicyAgent(client *authzed.ClientWithExperimental, logger mglog.Logger) auth.PolicyAgent {
func NewPolicyAgent(client *authzed.ClientWithExperimental, logger *slog.Logger) auth.PolicyAgent {
return &policyAgent{
client: client,
permissionClient: client.PermissionsServiceClient,
Expand Down
6 changes: 3 additions & 3 deletions bootstrap/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ package api
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/absmach/magistrala/bootstrap"
mglog "github.com/absmach/magistrala/logger"
)

var _ bootstrap.Service = (*loggingMiddleware)(nil)

type loggingMiddleware struct {
logger mglog.Logger
logger *slog.Logger
svc bootstrap.Service
}

// LoggingMiddleware adds logging facilities to the bootstrap service.
func LoggingMiddleware(svc bootstrap.Service, logger mglog.Logger) bootstrap.Service {
func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Service {
return &loggingMiddleware{logger, svc}
}

Expand Down
4 changes: 2 additions & 2 deletions bootstrap/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package api
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"net/url"
"strings"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/bootstrap"
"github.com/absmach/magistrala/internal/apiutil"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/go-chi/chi/v5"
Expand All @@ -38,7 +38,7 @@ var (
)

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc bootstrap.Service, reader bootstrap.ConfigReader, logger mglog.Logger, instanceID string) http.Handler {
func MakeHandler(svc bootstrap.Service, reader bootstrap.ConfigReader, logger *slog.Logger, instanceID string) http.Handler {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
}
Expand Down
6 changes: 3 additions & 3 deletions bootstrap/postgres/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"database/sql"
"encoding/json"
"fmt"
"log/slog"
"strings"
"time"

"github.com/absmach/magistrala/bootstrap"
"github.com/absmach/magistrala/internal/postgres"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
"github.com/jackc/pgerrcode"
Expand All @@ -37,12 +37,12 @@ var _ bootstrap.ConfigRepository = (*configRepository)(nil)

type configRepository struct {
db postgres.Database
log mglog.Logger
log *slog.Logger
}

// NewConfigRepository instantiates a PostgreSQL implementation of config
// repository.
func NewConfigRepository(db postgres.Database, log mglog.Logger) bootstrap.ConfigRepository {
func NewConfigRepository(db postgres.Database, log *slog.Logger) bootstrap.ConfigRepository {
return &configRepository{db: db, log: log}
}

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/postgres/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

var (
testLog, _ = mglog.New(os.Stdout, mglog.Info.String())
testLog, _ = mglog.New(os.Stdout, "info")
db *sqlx.DB
)

Expand Down
6 changes: 3 additions & 3 deletions certs/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ package api
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/absmach/magistrala/certs"
mglog "github.com/absmach/magistrala/logger"
)

var _ certs.Service = (*loggingMiddleware)(nil)

type loggingMiddleware struct {
logger mglog.Logger
logger *slog.Logger
svc certs.Service
}

// LoggingMiddleware adds logging facilities to the bootstrap service.
func LoggingMiddleware(svc certs.Service, logger mglog.Logger) certs.Service {
func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service {
return &loggingMiddleware{logger, svc}
}

Expand Down
4 changes: 2 additions & 2 deletions certs/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package api
import (
"context"
"encoding/json"
"log/slog"
"net/http"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/certs"
"github.com/absmach/magistrala/internal/apiutil"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/go-chi/chi/v5"
Expand All @@ -29,7 +29,7 @@ const (
)

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc certs.Service, logger mglog.Logger, instanceID string) http.Handler {
func MakeHandler(svc certs.Service, logger *slog.Logger, instanceID string) http.Handler {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
}
Expand Down
6 changes: 3 additions & 3 deletions certs/postgres/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"context"
"database/sql"
"fmt"
"log/slog"
"time"

"github.com/absmach/magistrala/certs"
"github.com/absmach/magistrala/internal/postgres"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
Expand All @@ -29,12 +29,12 @@ type Cert struct {

type certsRepository struct {
db postgres.Database
log mglog.Logger
log *slog.Logger
}

// NewRepository instantiates a PostgreSQL implementation of certs
// repository.
func NewRepository(db postgres.Database, log mglog.Logger) certs.Repository {
func NewRepository(db postgres.Database, log *slog.Logger) certs.Repository {
return &certsRepository{db: db, log: log}
}

Expand Down
2 changes: 1 addition & 1 deletion certs/postgres/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

var (
testLog, _ = mglog.New(os.Stdout, mglog.Info.String())
testLog, _ = mglog.New(os.Stdout, "info")
db *sqlx.DB
)

Expand Down
9 changes: 5 additions & 4 deletions cmd/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"context"
"fmt"
"log"
"log/slog"
"net/url"
"os"
"time"

chclient "github.com/absmach/callhome/pkg/client"
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/auth"
api "github.com/absmach/magistrala/auth/api"
Expand All @@ -34,7 +36,6 @@ import (
"github.com/authzed/grpcutil"
"github.com/caarlos0/env/v10"
"github.com/jmoiron/sqlx"
chclient "github.com/mainflux/callhome/pkg/client"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
Expand Down Expand Up @@ -79,7 +80,7 @@ func main() {

logger, err := mglog.New(os.Stdout, cfg.LogLevel)
if err != nil {
logger.Fatal(fmt.Sprintf("failed to init logger: %s", err.Error()))
log.Fatalf("failed to init logger: %s", err.Error())
}

var exitCode int
Expand All @@ -95,7 +96,7 @@ func main() {

dbConfig := pgclient.Config{Name: defDB}
if err := env.ParseWithOptions(&dbConfig, env.Options{Prefix: envPrefixDB}); err != nil {
logger.Fatal(err.Error())
logger.Error(err.Error())
}

db, err := pgclient.Setup(dbConfig, *apostgres.Migration())
Expand Down Expand Up @@ -200,7 +201,7 @@ func initSchema(ctx context.Context, client *authzed.ClientWithExperimental, sch
return nil
}

func newService(db *sqlx.DB, tracer trace.Tracer, cfg config, dbConfig pgclient.Config, logger mglog.Logger, spicedbClient *authzed.ClientWithExperimental) auth.Service {
func newService(db *sqlx.DB, tracer trace.Tracer, cfg config, dbConfig pgclient.Config, logger *slog.Logger, spicedbClient *authzed.ClientWithExperimental) auth.Service {
database := postgres.NewDatabase(db, dbConfig, tracer)
keysRepo := apostgres.New(database)
domainsRepo := apostgres.NewDomainRepository(database)
Expand Down
Loading

0 comments on commit 8c084b1

Please sign in to comment.