-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Quang nd
committed
Sep 19, 2024
1 parent
75a7135
commit 470a56c
Showing
7 changed files
with
161 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: basic-go-deployment | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: basic-go | ||
template: | ||
metadata: | ||
labels: | ||
app: basic-go | ||
spec: | ||
containers: | ||
- name: basic-go | ||
image: darthmalgus1997/basic-go-service:v1.0.2 | ||
resources: | ||
limits: | ||
memory: "128Mi" | ||
cpu: "500m" | ||
ports: | ||
- containerPort: 8080 | ||
env: | ||
- name: service-name | ||
valueFrom: | ||
fieldRef: | ||
fieldPath: spec.selector.matchLabels.app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,65 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
"time" | ||
// import ( | ||
// "os" | ||
// "time" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
// "go.uber.org/zap" | ||
// "go.uber.org/zap/zapcore" | ||
// ) | ||
|
||
type zapLogger struct { | ||
logger *zap.Logger | ||
} | ||
// type zapLogger struct { | ||
// logger *zap.Logger | ||
// } | ||
|
||
func newZapLogger() (Logger, error) { | ||
config := zap.NewProductionConfig() | ||
config.EncoderConfig.TimeKey = "" | ||
customConfig := zapcore.EncoderConfig{ | ||
TimeKey: "timestamp", | ||
LevelKey: "level", | ||
NameKey: "logger", | ||
CallerKey: "caller", | ||
FunctionKey: zapcore.OmitKey, | ||
MessageKey: "msg", | ||
StacktraceKey: "stacktrace", | ||
LineEnding: zapcore.DefaultLineEnding, | ||
EncodeLevel: zapcore.LowercaseLevelEncoder, | ||
EncodeTime: zapcore.TimeEncoderOfLayout(time.RFC3339), | ||
EncodeDuration: zapcore.StringDurationEncoder, | ||
EncodeCaller: zapcore.ShortCallerEncoder, | ||
} | ||
customCore := zapcore.NewCore( | ||
zapcore.NewJSONEncoder(customConfig), | ||
zapcore.AddSync(os.Stdout), | ||
config.Level, | ||
) | ||
// func newZapLogger() (Logger, error) { | ||
// config := zap.NewProductionConfig() | ||
// config.EncoderConfig.TimeKey = "" | ||
// customConfig := zapcore.EncoderConfig{ | ||
// TimeKey: "timestamp", | ||
// LevelKey: "level", | ||
// NameKey: "logger", | ||
// CallerKey: "caller", | ||
// FunctionKey: zapcore.OmitKey, | ||
// MessageKey: "msg", | ||
// StacktraceKey: "stacktrace", | ||
// LineEnding: zapcore.DefaultLineEnding, | ||
// EncodeLevel: zapcore.LowercaseLevelEncoder, | ||
// EncodeTime: zapcore.TimeEncoderOfLayout(time.RFC3339), | ||
// EncodeDuration: zapcore.StringDurationEncoder, | ||
// EncodeCaller: zapcore.ShortCallerEncoder, | ||
// } | ||
// customCore := zapcore.NewCore( | ||
// zapcore.NewJSONEncoder(customConfig), | ||
// zapcore.AddSync(os.Stdout), | ||
// config.Level, | ||
// ) | ||
|
||
// Combine the custom core with the default core | ||
core := zapcore.NewTee( | ||
zapcore.NewCore(zapcore.NewJSONEncoder(config.EncoderConfig), zapcore.AddSync(os.Stdout), config.Level), | ||
customCore, | ||
) | ||
logger := zap.New(core) | ||
return &zapLogger{logger: logger}, nil | ||
} | ||
// // Combine the custom core with the default core | ||
// core := zapcore.NewTee( | ||
// zapcore.NewCore(zapcore.NewJSONEncoder(config.EncoderConfig), zapcore.AddSync(os.Stdout), config.Level), | ||
// customCore, | ||
// ) | ||
// logger := zap.New(core) | ||
// return &zapLogger{logger: logger}, nil | ||
// } | ||
|
||
func (z *zapLogger) Info(msg string, fields ...Field) { | ||
z.logger.Info(msg, convertToZapFields(fields)...) | ||
} | ||
// func (z *zapLogger) Info(msg string, fields ...Field) { | ||
// z.logger.Info(msg, convertToZapFields(fields)...) | ||
// } | ||
|
||
func (z *zapLogger) Error(msg string, fields ...Field) { | ||
z.logger.Error(msg, convertToZapFields(fields)...) | ||
} | ||
// func (z *zapLogger) Error(msg string, fields ...Field) { | ||
// z.logger.Error(msg, convertToZapFields(fields)...) | ||
// } | ||
|
||
func (z *zapLogger) Debug(msg string, fields ...Field) { | ||
z.logger.Debug(msg, convertToZapFields(fields)...) | ||
} | ||
// func (z *zapLogger) Debug(msg string, fields ...Field) { | ||
// z.logger.Debug(msg, convertToZapFields(fields)...) | ||
// } | ||
|
||
func convertToZapFields(fields []Field) []zap.Field { | ||
zapFields := make([]zap.Field, len(fields)) | ||
for i, f := range fields { | ||
zapFields[i] = zap.Any(f.Key, f.Value) | ||
} | ||
return zapFields | ||
} | ||
// func convertToZapFields(fields []Field) []zap.Field { | ||
// zapFields := make([]zap.Field, len(fields)) | ||
// for i, f := range fields { | ||
// zapFields[i] = zap.Any(f.Key, f.Value) | ||
// } | ||
// return zapFields | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func SetupZeroLogger() { | ||
service_name := os.Getenv("service_name") | ||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix | ||
log.Logger = log.With().Str("service", service_name).Logger() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package middleware | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func Logger() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
start := time.Now() | ||
raw := c.Request.URL.RawQuery | ||
|
||
c.Next() | ||
|
||
latency := time.Since(start).Milliseconds() | ||
var zeroLog *zerolog.Event | ||
status := c.Writer.Status() | ||
switch { | ||
case status < 400: | ||
zeroLog = log.Info() | ||
case status < 500: | ||
zeroLog = log.Warn() | ||
default: | ||
zeroLog = log.Error() | ||
} | ||
zeroLog. | ||
Int("status", c.Writer.Status()). | ||
Int64("latency", latency). | ||
Str("method", c.Request.Method). | ||
Str("path", c.Request.URL.Path). | ||
Str("method", c.Request.Method). | ||
Str("query", raw). | ||
Msg("HTTP request completed") | ||
} | ||
} | ||
|
||
func ErrorHandler() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
c.Next() | ||
|
||
status := c.Writer.Status() | ||
if status < 500 { | ||
return | ||
} | ||
c.Errors = c.Errors[:0] | ||
c.JSON(500, gin.H{ | ||
"error": "An internal server error occurred", | ||
}) | ||
c.Abort() | ||
} | ||
} |