Skip to content

Commit

Permalink
[Log] Replace log.Logger by zap.Logger (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuma committed May 24, 2023
1 parent 6e53abf commit cac14b9
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 3,171 deletions.
5 changes: 3 additions & 2 deletions examples/2-encoders/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/mwm-io/gapi/examples/2-encoders/internal/err"
"github.com/mwm-io/gapi/examples/2-encoders/internal/hello"
gLog "github.com/mwm-io/gapi/log"
"github.com/mwm-io/gapi/server"

"github.com/mwm-io/gapi/examples/2-encoders/internal/err"
"github.com/mwm-io/gapi/examples/2-encoders/internal/hello"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion examples/3-params/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/mwm-io/gapi/examples/3-params/internal"
gLog "github.com/mwm-io/gapi/log"
"github.com/mwm-io/gapi/server"

"github.com/mwm-io/gapi/examples/3-params/internal"
)

func main() {
Expand Down
8 changes: 0 additions & 8 deletions examples/gcp-logger/go.mod

This file was deleted.

1,126 changes: 0 additions & 1,126 deletions examples/gcp-logger/go.sum

This file was deleted.

63 changes: 0 additions & 63 deletions examples/gcp-logger/main.go

This file was deleted.

4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ module github.com/mwm-io/gapi
go 1.16

require (
cloud.google.com/go/logging v1.5.0
github.com/elnormous/contenttype v1.0.3
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/gorilla/schema v1.2.0
github.com/stretchr/testify v1.8.1
github.com/swaggest/openapi-go v0.2.24
go.opencensus.io v0.23.0
go.opentelemetry.io/otel/trace v1.9.0
google.golang.org/genproto v0.0.0-20221109142239-94d6d90a7d66
go.uber.org/zap v1.24.0
)
933 changes: 17 additions & 916 deletions go.sum

Large diffs are not rendered by default.

85 changes: 0 additions & 85 deletions log/cloud_logging/writer.go

This file was deleted.

111 changes: 10 additions & 101 deletions log/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,115 +2,24 @@ package log

import (
"context"

"go.uber.org/zap"
)

type contextKey int
type contextKey string

// loggerKey is the key for logger.Logger values in Contexts.
// It is unexported; clients use logger.NewContext and logger.FromContext instead of using this key directly.
var loggerKey contextKey
// loggerKey is the key for zap.Logger values in Contexts.
// It is unexported; clients use NewContext and FromContext instead of using this key directly.
var loggerKey contextKey = "gapi-logger"

// NewContext returns a new Context that carries value Logger.
func NewContext(ctx context.Context, l *Logger) context.Context {
func NewContext(ctx context.Context, l *zap.Logger) context.Context {
return context.WithValue(ctx, loggerKey, l)
}

// FromContext returns the Logger value stored in ctx, if interface{}.
func FromContext(ctx context.Context) (*Logger, bool) {
l, ok := ctx.Value(loggerKey).(*Logger)
// FromContext returns the Logger value stored in Context.
func FromContext(ctx context.Context) (*zap.Logger, bool) {
l, ok := ctx.Value(loggerKey).(*zap.Logger)

return l, ok
}

// FromContextOrGlobal returns the Logger value stored in ctx, or the global Logger if none are stored.
func FromContextOrGlobal(ctx context.Context) *Logger {
l, ok := FromContext(ctx)
if ok {
return l
}

return GlobalLogger()
}

// LogC log a message using FromContextOrGlobal.
func LogC(ctx context.Context, msg string, options ...EntryOption) {
FromContextOrGlobal(ctx).WithContext(ctx).
Log(msg, options...)
}

// LogAnyC logs anything using FromContextOrGlobal.
func LogAnyC(ctx context.Context, v interface{}, options ...EntryOption) {
FromContextOrGlobal(ctx).WithContext(ctx).
LogAny(v, options...)
}

// EmergencyC logs an emergency message with additional EntryOptions using FromContextOrGlobal.
func EmergencyC(ctx context.Context, msg string, options ...EntryOption) {
options = append(options, SeverityOpt(EmergencySeverity))
LogC(ctx, msg, options...)
}

// AlertC logs an alert message with additional EntryOptions using FromContextOrGlobal.
func AlertC(ctx context.Context, msg string, options ...EntryOption) {
options = append(options, SeverityOpt(AlertSeverity))
LogC(ctx, msg, options...)
}

// CriticalC logs a critical message with additional EntryOptions using FromContextOrGlobal.
func CriticalC(ctx context.Context, msg string, options ...EntryOption) {
options = append(options, SeverityOpt(CriticalSeverity))
LogC(ctx, msg, options...)
}

// ErrorC logs an error message with additional EntryOptions using FromContextOrGlobal.
func ErrorC(ctx context.Context, msg string, options ...EntryOption) {
options = append(options, SeverityOpt(ErrorSeverity))
LogC(ctx, msg, options...)
}

// WarnC logs a warning message with additional EntryOptions using FromContextOrGlobal.
func WarnC(ctx context.Context, msg string, options ...EntryOption) {
options = append(options, SeverityOpt(WarnSeverity))
LogC(ctx, msg, options...)
}

// InfoC logs an info message with additional EntryOptions using FromContextOrGlobal.
func InfoC(ctx context.Context, msg string, options ...EntryOption) {
options = append(options, SeverityOpt(InfoSeverity))
LogC(ctx, msg, options...)
}

// DebugC logs a debug message with additional EntryOptions using FromContextOrGlobal.
func DebugC(ctx context.Context, msg string, options ...EntryOption) {
options = append(options, SeverityOpt(DebugSeverity))
LogC(ctx, msg, options...)
}

// CtxWithOptions adds options to the logger in the context.
func CtxWithOptions(ctx context.Context, options ...EntryOption) context.Context {
logger := FromContextOrGlobal(ctx)

logger.options = append(logger.options, options...)

return NewContext(ctx, logger)
}

// CtxWithSeverity set the default severity for the logger inside the context.
func CtxWithSeverity(ctx context.Context, severity Severity) context.Context {
return CtxWithOptions(ctx, SeverityOpt(severity))
}

// CtxWithLabels adds labels for the logger inside the context.
func CtxWithLabels(ctx context.Context, labels map[string]string) context.Context {
return CtxWithOptions(ctx, LabelsOpt(labels))
}

// CtxWithFields adds fields for the logger inside the context.
func CtxWithFields(ctx context.Context, fields map[string]interface{}) context.Context {
return CtxWithOptions(ctx, FieldsOpt(fields))
}

// CtxWithContext set the context for the logger inside the context.
func CtxWithContext(ctx context.Context) context.Context {
return CtxWithOptions(ctx, ContextOpt(ctx))
}
Loading

0 comments on commit cac14b9

Please sign in to comment.