github.com/blend/go-sdk@v1.20220411.3/examples/logger/custom_event/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"io"
    14  
    15  	"github.com/blend/go-sdk/logger"
    16  )
    17  
    18  // NewCustomEvent returns a new custom event.
    19  // It is helpful to use a constructor syou can initialize the event meta.
    20  func NewCustomEvent(userID, sessionID, context string) CustomEvent {
    21  	return CustomEvent{
    22  		UserID:    userID,
    23  		SessionID: sessionID,
    24  		Context:   context,
    25  	}
    26  }
    27  
    28  // CustomEvent is a custom logger event.
    29  type CustomEvent struct {
    30  	UserID    string // something domain specific
    31  	SessionID string // something domain specific
    32  	Context   string // something domain specific
    33  }
    34  
    35  // GetFlag implements logger.Event.
    36  func (ce CustomEvent) GetFlag() string { return "custom_event" }
    37  
    38  // WriteText implements logger.TextWritable.
    39  // It is optional, but very much encouraged.
    40  // It takes a formatter and a buffer reference that you push data into.
    41  // This lets the logger re-use buffers.
    42  func (ce CustomEvent) WriteText(tf logger.TextFormatter, wr io.Writer) {
    43  	io.WriteString(wr, ce.UserID)
    44  	io.WriteString(wr, logger.Space)
    45  	io.WriteString(wr, ce.SessionID)
    46  	io.WriteString(wr, logger.Space)
    47  	io.WriteString(wr, ce.Context)
    48  }
    49  
    50  // Decompose implements logger.JSONWritable.
    51  // It is a function that returns just the custom fields on our object as a map,
    52  // to be serialized with the rest of the fields.
    53  func (ce CustomEvent) Decompose() map[string]interface{} {
    54  	return map[string]interface{}{
    55  		"userID":    ce.UserID,
    56  		"sessionID": ce.SessionID,
    57  		"context":   ce.Context,
    58  	}
    59  }
    60  
    61  // NewCustomEventListener returns a type shim for the logger.
    62  func NewCustomEventListener(listener func(context.Context, CustomEvent)) logger.Listener {
    63  	return func(ctx context.Context, e logger.Event) {
    64  		listener(ctx, e.(CustomEvent))
    65  	}
    66  }
    67  
    68  func main() {
    69  	// make a text logger.
    70  	text := logger.All(logger.OptText())
    71  
    72  	// make a json logger
    73  	js := logger.All(logger.OptJSON())
    74  
    75  	ctx := context.Background()
    76  
    77  	event := NewCustomEvent("example-string", "session0", "Console Demo")
    78  
    79  	text.TriggerContext(ctx, event)
    80  	text.Write(ctx, event)
    81  	js.TriggerContext(ctx, event)
    82  	js.Write(ctx, event)
    83  
    84  	done := make(chan struct{})
    85  	listener := logger.All()
    86  	listener.Listen("custom_event", "demo", NewCustomEventListener(func(_ context.Context, ce CustomEvent) {
    87  		fmt.Println("listener got event")
    88  		close(done)
    89  	}))
    90  	listener.TriggerContext(ctx, event)
    91  	<-done
    92  }