Skip to content

Commit

Permalink
feat: add logrus logging example (#265)
Browse files Browse the repository at this point in the history
* 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
cprice404 authored Mar 10, 2023
1 parent c4bcc3c commit 2fe47d0
Show file tree
Hide file tree
Showing 6 changed files with 878 additions and 749 deletions.
5 changes: 4 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/momentohq/go-example

require (
github.com/google/uuid v1.3.0
github.com/momentohq/client-sdk-go v0.11.0
github.com/momentohq/client-sdk-go v0.14.0

// logrus is not required to use momento, but it is used in the logging-example
github.com/sirupsen/logrus v1.9.0
)

require (
Expand Down
754 changes: 754 additions & 0 deletions examples/go.sum

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions examples/logging-example/main.go
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)
}
}
43 changes: 43 additions & 0 deletions examples/logging-example/momento_logrus/logrus.go
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})}
}
4 changes: 2 additions & 2 deletions examples/sortedset-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
// Put score for each element to set
// Using counter, element N has score N
for i := 1; i < 11; i++ {
_, err := client.SortedSetPut(ctx, &momento.SortedSetPutRequest{
_, err := client.SortedSetPutElements(ctx, &momento.SortedSetPutElementsRequest{
CacheName: cacheName,
SetName: setName,
Elements: []*momento.SortedSetPutElement{{
Expand Down Expand Up @@ -97,7 +97,7 @@ func setupCache(client momento.CacheClient, ctx context.Context) {
func displayElements(setName string, resp responses.SortedSetFetchResponse) {
switch r := resp.(type) {
case *responses.SortedSetFetchHit:
for _, e := range r.Elements {
for _, e := range r.ValueStringElements() {
fmt.Printf("setName: %s, value: %s, score: %f\n", setName, e.Value, e.Score)
}
fmt.Println("")
Expand Down
Loading

0 comments on commit 2fe47d0

Please sign in to comment.