github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/examples/error_logging/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/v2pro/plz"
     7  	"github.com/v2pro/plz/countlog"
     8  	"os"
     9  	"time"
    10  )
    11  
    12  func main() {
    13  	plz.PlugAndPlay()
    14  	ctx := context.WithValue(context.Background(), "traceId", "abcd")
    15  	//err := doSomething(ctx)
    16  	//countlog.TraceCall("callee!main.doSomething", err, "ctx", ctx)
    17  	doZ(countlog.Ctx(ctx))
    18  }
    19  
    20  func doX(ctx context.Context) error {
    21  	file, err := os.OpenFile("/tmp/my-dir/abc", os.O_RDWR, 0666)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	defer file.Close()
    26  	_, err = file.Write([]byte("hello"))
    27  	if err != nil {
    28  		return err
    29  	}
    30  	return nil
    31  }
    32  
    33  func doA(ctx context.Context) error {
    34  	file, err := os.OpenFile("/tmp/my-dir/abc", os.O_RDWR, 0666)
    35  	if err != nil {
    36  		return fmt.Errorf("failed to open file: %v", err)
    37  	}
    38  	defer file.Close()
    39  	_, err = file.Write([]byte("hello"))
    40  	if err != nil {
    41  		return fmt.Errorf("failed to write: %v", err)
    42  	}
    43  	return nil
    44  }
    45  
    46  func doY(ctx context.Context) error {
    47  	defer func() {
    48  		recovered := recover()
    49  		if recovered != nil {
    50  			countlog.Fatal("event!doY.panic",
    51  				"err", recovered)
    52  		}
    53  	}()
    54  	start := time.Now()
    55  	file, err := os.OpenFile("/tmp/my-dir/abc", os.O_RDWR, 0666)
    56  	if err != nil {
    57  		countlog.Error("event!metric",
    58  			"callee", "ioutil.WriteFile", "ctx", ctx, "latency", time.Since(start))
    59  		countlog.Error("event!doY.failed to open file", "err", err)
    60  		return err
    61  	}
    62  	countlog.Trace("event!metric",
    63  		"callee", "ioutil.WriteFile", "ctx", ctx, "latency", time.Since(start))
    64  	defer func() {
    65  		err = file.Close()
    66  		if err != nil {
    67  			countlog.Error("event!doY.failed to close file", "err", err)
    68  		}
    69  	}()
    70  	_, err = file.Write([]byte("hello"))
    71  	if err != nil {
    72  		return err
    73  	}
    74  	return nil
    75  }
    76  
    77  func doZ(ctx *countlog.Context) error {
    78  	defer func() {
    79  		countlog.LogPanic(recover())
    80  	}()
    81  	path := "/tmp/abc"
    82  	file, err := os.OpenFile(path, os.O_RDWR, 0666)
    83  	// add event! prefix to make log message more findable
    84  	ctx.TraceCall("event!doZ os.OpenFile", err)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	defer plz.Close(file)
    89  	_, err = file.Write([]byte("hello"))
    90  	// without event! prefix also works,
    91  	// but event name must not be dynamic formatted string
    92  	// TraceCall will also generate new error object with more context
    93  	// return this error will give user a better clue about what happened
    94  	err = ctx.TraceCall("doZ write file {path}", err,
    95  		"path", path)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	return nil
   100  }