-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #844 from traPtitech/logging
- Loading branch information
Showing
27 changed files
with
540 additions
and
371 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
type contextKey struct{} | ||
|
||
var ErrLoggerNotSet = errors.New("logger not set in context") | ||
|
||
func SetLogger(ctx context.Context, logger *zap.Logger) context.Context { | ||
return context.WithValue(ctx, contextKey{}, logger) | ||
} | ||
|
||
func GetLoggerMaybe(ctx context.Context) (*zap.Logger, error) { | ||
logger, ok := ctx.Value(contextKey{}).(*zap.Logger) | ||
if !ok { | ||
return nil, ErrLoggerNotSet | ||
} | ||
return logger, nil | ||
} | ||
|
||
func GetLogger(ctx context.Context) *zap.Logger { | ||
logger, err := GetLoggerMaybe(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return 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,36 @@ | ||
package logging | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
type Mode string | ||
|
||
const ( | ||
Development Mode = "development" | ||
Production Mode = "production" | ||
) | ||
|
||
var ErrUnknownMode = errors.New("unknown mode") | ||
|
||
func Load(mode Mode) (*zap.Logger, error) { | ||
switch mode { | ||
case Development: | ||
return zap.NewDevelopment() | ||
case Production: | ||
return zap.NewProduction() | ||
default: | ||
return nil, ErrUnknownMode | ||
} | ||
} | ||
|
||
func ModeFromEnv(varName string) Mode { | ||
// TODO: strconv.ParseBool を使う | ||
if os.Getenv(varName) != "" { | ||
return Development | ||
} | ||
return Production | ||
} |
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
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
Oops, something went wrong.