-
Notifications
You must be signed in to change notification settings - Fork 11
feature(slog): support slog logger #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| # ide | ||
| .idea | ||
|
|
||
| # binary | ||
| /bin | ||
| # development | ||
| .env | ||
| .DS_Store | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,40 @@ | ||
| module testapp | ||
|
|
||
| go 1.21.4 | ||
| go 1.21.5 | ||
|
|
||
| require ( | ||
| github.com/go-sql-driver/mysql v1.7.1 | ||
| github.com/google/uuid v1.4.0 | ||
| github.com/google/uuid v1.5.0 | ||
| github.com/gorilla/mux v1.8.1 | ||
| github.com/hanagantig/gracy v0.0.0-20231003055507-4d3ebe0e0a0a | ||
| github.com/jmoiron/sqlx v1.3.5 | ||
| github.com/lib/pq v1.10.9 | ||
| github.com/spf13/cobra v1.8.0 | ||
| github.com/spf13/viper v1.17.0 | ||
| github.com/spf13/viper v1.18.1 | ||
| github.com/stretchr/testify v1.8.4 | ||
| go.uber.org/zap v1.26.0 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
| github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
| github.com/fsnotify/fsnotify v1.7.0 // indirect | ||
| github.com/hashicorp/hcl v1.0.0 // indirect | ||
| github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
| github.com/magiconair/properties v1.8.7 // indirect | ||
| github.com/mitchellh/mapstructure v1.5.0 // indirect | ||
| github.com/pelletier/go-toml/v2 v2.1.0 // indirect | ||
| github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
| github.com/sagikazarmark/locafero v0.3.0 // indirect | ||
| github.com/sagikazarmark/locafero v0.4.0 // indirect | ||
| github.com/sagikazarmark/slog-shim v0.1.0 // indirect | ||
| github.com/sourcegraph/conc v0.3.0 // indirect | ||
| github.com/spf13/afero v1.10.0 // indirect | ||
| github.com/spf13/cast v1.5.1 // indirect | ||
| github.com/spf13/afero v1.11.0 // indirect | ||
| github.com/spf13/cast v1.6.0 // indirect | ||
| github.com/spf13/pflag v1.0.5 // indirect | ||
| github.com/subosito/gotenv v1.6.0 // indirect | ||
| go.uber.org/multierr v1.10.0 // indirect | ||
| golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect | ||
| golang.org/x/sys v0.12.0 // indirect | ||
| golang.org/x/text v0.13.0 // indirect | ||
| golang.org/x/sys v0.15.0 // indirect | ||
| golang.org/x/text v0.14.0 // indirect | ||
| gopkg.in/ini.v1 v1.67.0 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) |
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,24 @@ | ||
| package logger | ||
|
|
||
| import ( | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| "context" | ||
| "log/slog" | ||
| "os" | ||
| ) | ||
|
|
||
| type Logger interface { | ||
| Debug(string, ...zapcore.Field) | ||
| Info(string, ...zapcore.Field) | ||
| Error(string, ...zapcore.Field) | ||
| Fatal(string, ...zapcore.Field) | ||
| Debug(string, ...any) | ||
| DebugContext(context.Context, string, ...any) | ||
| Info(string, ...any) | ||
| InfoContext(context.Context, string, ...any) | ||
| Error(string, ...any) | ||
| ErrorContext(context.Context, string, ...any) | ||
| Warn(string, ...any) | ||
| WarnContext(context.Context, string, ...any) | ||
| } | ||
|
|
||
| func NewLogger() (*zap.Logger, error) { | ||
| return zap.NewProduction() | ||
| func NewLogger() *slog.Logger { | ||
| handler := slog.NewJSONHandler(os.Stdout, nil) | ||
|
|
||
| return slog.New(handler) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package config | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no needs in all of this thing since we will keep only slog implementation in our code (check this comment ) |
||
|
|
||
| var loggerPackges = map[Logger]string{ | ||
| "zap": "\"go.uber.org/zap\"", | ||
| "slog": "\"log/slog\"", | ||
| } | ||
|
|
||
| var serverHTTPStartFailMessage = map[Logger]string{ | ||
| "zap": "a.logger.Fatal(\n\"Fail to start %s http server:\",\nzap.String(\"app\", a.cfg.App.Name),\nzap.Error(err),\n)", | ||
| "slog": "a.logger.Error(\n\"Fail to start %s http server:\",\nslog.String(\"app\", a.cfg.App.Name),\nslog.String(\"err\", err.Error()),\n)", | ||
| } | ||
|
|
||
| var serverHTTPStopFailMessage = map[Logger]string{ | ||
| "zap": "a.logger.Error(\"failed to gracefully shutdown server\", zap.Error(err))", | ||
| "slog": "a.logger.Error(\"failed to gracefully shutdown server\", slog.String(\"err\", err.Error()))", | ||
| } | ||
|
|
||
| var examplePongHttpFailMessage = map[Logger]string{ | ||
| "zap": "h.logger.Error(\"failed to encode json\", zap.Error(err))", | ||
| "slog": "h.logger.Error(\"failed to encode json\", slog.String(\"err\", err.Error()))", | ||
| } | ||
|
|
||
| var supportedLoggers = map[Logger]struct{}{ | ||
| "zap": {}, | ||
| "slog": {}, | ||
| } | ||
|
|
||
| type Logger string | ||
|
|
||
| func (l Logger) String() string { | ||
| return string(l) | ||
| } | ||
|
|
||
| func (l Logger) GetImportPackage() string { | ||
| return loggerPackges[l] | ||
| } | ||
|
|
||
| func (l Logger) GetFailStartHTTPServerMessage() string { | ||
| return serverHTTPStartFailMessage[l] | ||
| } | ||
|
|
||
| func (l Logger) GetFailStopHTTPServerMessage() string { | ||
| return serverHTTPStopFailMessage[l] | ||
| } | ||
|
|
||
| func (l Logger) GetExamplePongHTTPFailMessage() string { | ||
| return examplePongHttpFailMessage[l] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| func NewLogger() *slog.Logger { | ||
| handler := slog.NewJSONHandler(os.Stdout, nil) | ||
|
|
||
| return slog.New(handler) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package slogchunk | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep only slog implementation in the generated code. Basically we will use slog everywhere and if somebody want to use different loggers - they will just implement their own handler for the slog |
||
|
|
||
| import ( | ||
| _ "embed" | ||
|
|
||
| "github.com/hanagantig/goro/internal/config" | ||
| ) | ||
|
|
||
| //go:embed build.tpl | ||
| var buildTmpl string | ||
|
|
||
| //go:embed pkginterface.tpl | ||
| var pkgInterface string | ||
|
|
||
| const name = "slog" | ||
| const initName = "l" | ||
|
|
||
| func NewSlogLoggerChunk() config.Chunk { | ||
| return config.Chunk{ | ||
| Name: name, | ||
| Scope: "logger.slog", | ||
| ArgName: initName, | ||
| ReturnType: "*slog.Logger", | ||
| DefinitionImports: "\"context\"", | ||
| BuildImports: "\"log/slog\"\n\"os\"\n\"context\"", | ||
| InitFunc: "NewLogger", | ||
| Build: buildTmpl, | ||
| Configs: "logger configs", | ||
| InitConfig: "", | ||
| PkgInterface: pkgInterface, | ||
| InitHasErr: false, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| type Logger interface { | ||
| Debug(string, ...any) | ||
| DebugContext(context.Context, string, ...any) | ||
| Info(string, ...any) | ||
| InfoContext(context.Context, string, ...any) | ||
| Error(string, ...any) | ||
| ErrorContext(context.Context, string, ...any) | ||
| Warn(string, ...any) | ||
| WarnContext(context.Context, string, ...any) | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| func NewLogger() (*zap.Logger, error) { | ||
| return zap.NewProduction() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package zapchunk | ||
|
|
||
| import ( | ||
| _ "embed" | ||
|
|
||
| "github.com/hanagantig/goro/internal/config" | ||
| ) | ||
|
|
||
| //go:embed build.tpl | ||
| var buildTmpl string | ||
|
|
||
| //go:embed pkginteface.tpl | ||
| var pkgInterface string | ||
|
|
||
| const name = "zap" | ||
| const initName = "l" | ||
|
|
||
| func NewZapLoggerChunk() config.Chunk { | ||
| return config.Chunk{ | ||
| Name: name, | ||
| Scope: "logger.zap", | ||
| ArgName: initName, | ||
| ReturnType: "*zap.Logger", | ||
| DefinitionImports: "\"go.uber.org/zap/zapcore\"", | ||
| BuildImports: "\"go.uber.org/zap/zapcore\"\n\"go.uber.org/zap\"", | ||
| InitFunc: "NewLogger", | ||
| Build: buildTmpl, | ||
| Configs: "logger configs", | ||
| InitConfig: "", | ||
| PkgInterface: pkgInterface, | ||
| InitHasErr: true, | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| type Logger interface { | ||
| Debug(string, ...zapcore.Field) | ||
| Info(string, ...zapcore.Field) | ||
| Error(string, ...zapcore.Field) | ||
| Fatal(string, ...zapcore.Field) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's good to add here some default fields, such as
env=production,app=goro,version=0.0.1