github.com/goravel/framework@v1.13.9/testing/mock/log.go (about)

     1  package mock
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/goravel/framework/contracts/http"
     8  	"github.com/goravel/framework/contracts/log"
     9  	"github.com/goravel/framework/support/carbon"
    10  )
    11  
    12  type TestLog struct {
    13  	*TestLogWriter
    14  }
    15  
    16  func NewTestLog() log.Log {
    17  	return &TestLog{
    18  		TestLogWriter: NewTestLogWriter(),
    19  	}
    20  }
    21  
    22  func (r *TestLog) WithContext(ctx context.Context) log.Writer {
    23  	return NewTestLogWriter()
    24  }
    25  
    26  type TestLogWriter struct {
    27  	data map[string]any
    28  }
    29  
    30  func NewTestLogWriter() *TestLogWriter {
    31  	return &TestLogWriter{
    32  		data: make(map[string]any),
    33  	}
    34  }
    35  
    36  func (r *TestLogWriter) Debug(args ...any) {
    37  	fmt.Print(prefix("debug"))
    38  	fmt.Println(args...)
    39  	r.printData()
    40  }
    41  
    42  func (r *TestLogWriter) Debugf(format string, args ...any) {
    43  	fmt.Print(prefix("debug"))
    44  	fmt.Printf(format+"\n", args...)
    45  	r.printData()
    46  }
    47  
    48  func (r *TestLogWriter) Info(args ...any) {
    49  	fmt.Print(prefix("info"))
    50  	fmt.Println(args...)
    51  	r.printData()
    52  }
    53  
    54  func (r *TestLogWriter) Infof(format string, args ...any) {
    55  	fmt.Print(prefix("info"))
    56  	fmt.Printf(format+"\n", args...)
    57  	r.printData()
    58  }
    59  
    60  func (r *TestLogWriter) Warning(args ...any) {
    61  	fmt.Print(prefix("warning"))
    62  	fmt.Println(args...)
    63  	r.printData()
    64  }
    65  
    66  func (r *TestLogWriter) Warningf(format string, args ...any) {
    67  	fmt.Print(prefix("warning"))
    68  	fmt.Printf(format+"\n", args...)
    69  	r.printData()
    70  }
    71  
    72  func (r *TestLogWriter) Error(args ...any) {
    73  	fmt.Print(prefix("error"))
    74  	fmt.Println(args...)
    75  	r.printData()
    76  }
    77  
    78  func (r *TestLogWriter) Errorf(format string, args ...any) {
    79  	fmt.Print(prefix("error"))
    80  	fmt.Printf(format+"\n", args...)
    81  	r.printData()
    82  }
    83  
    84  func (r *TestLogWriter) Fatal(args ...any) {
    85  	fmt.Print(prefix("fatal"))
    86  	fmt.Println(args...)
    87  	r.printData()
    88  }
    89  
    90  func (r *TestLogWriter) Fatalf(format string, args ...any) {
    91  	fmt.Print(prefix("fatal"))
    92  	fmt.Printf(format+"\n", args...)
    93  	r.printData()
    94  }
    95  
    96  func (r *TestLogWriter) Panic(args ...any) {
    97  	fmt.Print(prefix("panic"))
    98  	fmt.Println(args...)
    99  	r.printData()
   100  }
   101  
   102  func (r *TestLogWriter) Panicf(format string, args ...any) {
   103  	fmt.Print(prefix("panic"))
   104  	fmt.Printf(format+"\n", args...)
   105  	r.printData()
   106  }
   107  
   108  func (r *TestLogWriter) User(user any) log.Writer {
   109  	r.data["user"] = user
   110  
   111  	return r
   112  }
   113  
   114  func (r *TestLogWriter) Owner(owner any) log.Writer {
   115  	r.data["owner"] = owner
   116  
   117  	return r
   118  }
   119  
   120  func (r *TestLogWriter) Hint(hint string) log.Writer {
   121  	r.data["hint"] = hint
   122  
   123  	return r
   124  }
   125  
   126  func (r *TestLogWriter) Code(code string) log.Writer {
   127  	r.data["code"] = code
   128  
   129  	return r
   130  }
   131  
   132  func (r *TestLogWriter) With(data map[string]any) log.Writer {
   133  	r.data["with"] = data
   134  
   135  	return r
   136  }
   137  
   138  func (r *TestLogWriter) Tags(tags ...string) log.Writer {
   139  	r.data["tags"] = tags
   140  
   141  	return r
   142  }
   143  
   144  func (r *TestLogWriter) Request(req http.ContextRequest) log.Writer {
   145  	r.data["request"] = req
   146  
   147  	return r
   148  }
   149  
   150  func (r *TestLogWriter) Response(res http.ContextResponse) log.Writer {
   151  	r.data["response"] = res
   152  
   153  	return r
   154  }
   155  
   156  func (r *TestLogWriter) In(domain string) log.Writer {
   157  	r.data["in"] = domain
   158  
   159  	return r
   160  }
   161  
   162  func (r *TestLogWriter) printData() {
   163  	if len(r.data) > 0 {
   164  		fmt.Println(r.data)
   165  	}
   166  }
   167  
   168  func prefix(model string) string {
   169  	timestamp := carbon.Now().ToDateTimeString()
   170  
   171  	return fmt.Sprintf("[%s] %s.%s: ", timestamp, "test", model)
   172  }