-
Notifications
You must be signed in to change notification settings - Fork 1
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 #384 from green-ecolution/feature/improve-log-mess…
…ages feat: improve log messages
- Loading branch information
Showing
107 changed files
with
1,774 additions
and
942 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,38 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
// GetLogger retrieves a logger instance from the given context. | ||
// | ||
// This function attempts to extract a logger from the context using the key "logger". | ||
// If the key is not present or the value is not of type *slog.Logger, it falls back | ||
// to returning the default logger provided by slog. | ||
// | ||
// Usage: | ||
// 1. To use this function effectively, ensure that the context has a logger stored | ||
// using context.WithValue, with the key "logger" and a value of type *slog.Logger. | ||
// 2. If no logger is found in the context, slog.Default() is returned. | ||
// | ||
// Parameters: | ||
// - ctx (context.Context): The context from which the logger will be extracted. | ||
// | ||
// Returns: | ||
// - *slog.Logger: The logger instance extracted from the context or the default logger. | ||
// | ||
// Example: | ||
// ```go | ||
// logger := slog.New(slog.WithLevel(slog.LevelInfo)) | ||
// ctx := context.WithValue(context.Background(), "logger", logger) | ||
// retrievedLogger := GetLogger(ctx) | ||
// retrievedLogger.Info("Logger successfully retrieved!") | ||
// ``` | ||
func GetLogger(ctx context.Context) *slog.Logger { | ||
log, ok := ctx.Value("logger").(*slog.Logger) | ||
if !ok { | ||
log = slog.Default() | ||
} | ||
return log | ||
} |
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.