-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logrus logging example (#265)
* feat: add logrus logging example This commit adds an example that shows how to implement the Momento logging interface using the logrus logging framework, and some example code illustrating that the Momento log messages are successfully routed to logrus.
- Loading branch information
Showing
6 changed files
with
878 additions
and
749 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/momentohq/client-sdk-go/auth" | ||
"github.com/momentohq/client-sdk-go/config" | ||
"github.com/momentohq/client-sdk-go/momento" | ||
"github.com/momentohq/go-example/logging-example/momento_logrus" | ||
logrus "github.com/sirupsen/logrus" | ||
) | ||
|
||
func main() { | ||
fmt.Println(` | ||
This example illustrates how to construct a Momento client that is configured to use logrus for | ||
logging output. You can use a similar approach to integrate with the logging framework of your | ||
choice, so that Momento's logs will be written to the same destinations as the rest of your | ||
application's logs.`) | ||
fmt.Println("") | ||
|
||
creds, err := auth.FromEnvironmentVariable("MOMENTO_AUTH_TOKEN") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
client, err := momento.NewCacheClient( | ||
config.LaptopLatestWithLogger(momento_logrus.NewLogrusMomentoLoggerFactory()), | ||
creds, | ||
60*time.Second, | ||
) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
fmt.Println("Creating and deleting a cache with logrus level set to FATAL; you should not see any momento log messages.") | ||
fmt.Println("") | ||
logrus.SetLevel(logrus.FatalLevel) | ||
createAndDeleteCache(ctx, client) | ||
fmt.Println("Cache created and deleted!") | ||
fmt.Println("") | ||
fmt.Println("Adjusting logrus log level to INFO") | ||
logrus.SetLevel(logrus.InfoLevel) | ||
fmt.Println("Creating and deleting a cache with logrus level set to INFO; this time you SHOULD see some momento log messages.") | ||
fmt.Println("") | ||
createAndDeleteCache(ctx, client) | ||
|
||
fmt.Println(` | ||
Done! See momento_logrus/logrus.go for the details of how we connected the Momento logging to logrus. If you need | ||
a similar integration for another logging framework such as zerolog, zap, etc. please feel free to reach out to us! | ||
`) | ||
} | ||
|
||
func createAndDeleteCache(ctx context.Context, client momento.CacheClient) { | ||
cacheName := "logging-example-cache" | ||
_, err := client.CreateCache(ctx, &momento.CreateCacheRequest{ | ||
CacheName: cacheName, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = client.DeleteCache(ctx, &momento.DeleteCacheRequest{ | ||
CacheName: cacheName, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
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,43 @@ | ||
package momento_logrus | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/momentohq/client-sdk-go/config/logger" | ||
logrus "github.com/sirupsen/logrus" | ||
) | ||
|
||
type LogrusMomentoLogger struct { | ||
logrusLogger *logrus.Entry | ||
} | ||
|
||
func (l LogrusMomentoLogger) Trace(message string, args ...string) { | ||
l.logrusLogger.Tracef(message, args) | ||
} | ||
|
||
func (l LogrusMomentoLogger) Debug(message string, args ...string) { | ||
l.logrusLogger.Debugf(message, args) | ||
} | ||
|
||
func (l LogrusMomentoLogger) Info(message string, args ...string) { | ||
l.logrusLogger.Infof(message, args) | ||
} | ||
|
||
func (l LogrusMomentoLogger) Warn(message string, args ...string) { | ||
l.logrusLogger.Warnf(message, args) | ||
} | ||
|
||
func (l LogrusMomentoLogger) Error(message string, args ...string) { | ||
l.logrusLogger.Errorf(message, args) | ||
} | ||
|
||
type LogrusMomentoLoggerFactory struct{} | ||
|
||
func NewLogrusMomentoLoggerFactory() logger.MomentoLoggerFactory { | ||
return &LogrusMomentoLoggerFactory{} | ||
} | ||
|
||
func (lf LogrusMomentoLoggerFactory) GetLogger(loggerName string) logger.MomentoLogger { | ||
log.SetFlags(0) | ||
return &LogrusMomentoLogger{logrusLogger: logrus.WithFields(logrus.Fields{"library": "Momento", "logger": loggerName})} | ||
} |
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.