disable default log timestamp

This commit is contained in:
2023-05-18 13:34:54 +02:00
parent bc578cf25c
commit 5ea1af7fb1
3 changed files with 520 additions and 495 deletions

View File

@@ -67,7 +67,8 @@ func TestLocalSave(t *testing.T) {
reader := bytes.NewReader(binBytes)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = LocalPut(ctx, SaveMethod, reader, binSize, &params, &result, auth, saveHandler)
@@ -88,7 +89,8 @@ func TestLocalLoad(t *testing.T) {
binBytes := make([]byte, 0)
writer := bytes.NewBuffer(binBytes)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = LocalGet(ctx, LoadMethod, writer, &params, &result, auth, loadHandler)
@@ -101,7 +103,7 @@ func TestLocalLoad(t *testing.T) {
func TestNetExec(t *testing.T) {
go testServ(false)
time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
err := clientHello()
require.NoError(t, err)
@@ -109,21 +111,21 @@ func TestNetExec(t *testing.T) {
func TestNetSave(t *testing.T) {
go testServ(false)
time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
err := clientSave()
require.NoError(t, err)
}
func TestNetLoad(t *testing.T) {
go testServ(false)
time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
err := clientLoad()
require.NoError(t, err)
}
func BenchmarkNetPut(b *testing.B) {
go testServ(true)
time.Sleep(10 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
clientSave()
pBench := func(pb *testing.PB) {
@@ -148,10 +150,11 @@ func clientHello() error {
binBytes := make([]byte, binSize)
rand.Read(binBytes)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = Exec(ctx, "127.0.0.1:8081", HelloMethod, &params, &result, auth)
err = Exec(ctx, "127.0.0.1:18081", HelloMethod, &params, &result, auth)
if err != nil {
logError("method err:", err)
return err
@@ -176,10 +179,11 @@ func clientSave() error {
reader := bytes.NewReader(binBytes)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = Put(ctx, "127.0.0.1:8081", SaveMethod, reader, binSize, &params, &result, auth)
err = Put(ctx, "127.0.0.1:18081", SaveMethod, reader, binSize, &params, &result, auth)
if err != nil {
logError("method err:", err)
return err
@@ -200,10 +204,11 @@ func clientLoad() error {
binBytes := make([]byte, 0)
writer := bytes.NewBuffer(binBytes)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = Get(ctx, "127.0.0.1:8081", LoadMethod, writer, &params, &result, auth)
err = Get(ctx, "127.0.0.1:18081", LoadMethod, writer, &params, &result, auth)
if err != nil {
logError("method err:", err)
return err
@@ -228,6 +233,7 @@ func testServ(quiet bool) error {
SetAccessWriter(io.Discard)
SetMessageWriter(io.Discard)
}
serv := NewService()
serv.Handle(HelloMethod, helloHandler)
serv.Handle(SaveMethod, saveHandler)
@@ -239,7 +245,7 @@ func testServ(quiet bool) error {
serv.PostMiddleware(LogResponse)
serv.PostMiddleware(LogAccess)
err = serv.Listen(":8081")
err = serv.Listen(":18081")
if err != nil {
return err
}
@@ -277,7 +283,8 @@ func helloHandler(content *Content) error {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = content.ReadBin(ctx, io.Discard)
@@ -308,7 +315,8 @@ func saveHandler(content *Content) error {
bufferBytes := make([]byte, 0, 1024)
binWriter := bytes.NewBuffer(bufferBytes)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = content.ReadBin(ctx, binWriter)
@@ -336,7 +344,8 @@ func loadHandler(content *Content) error {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
timeout := time.Duration(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = content.ReadBin(ctx, io.Discard)

View File

@@ -13,26 +13,37 @@ import (
"time"
)
var messageWriter io.Writer = os.Stdout
var accessWriter io.Writer = os.Stdout
var (
messageWriter io.Writer = os.Stdout
accessWriter io.Writer = os.Stdout
logTimestamp bool = false
)
func getLogStamp() string {
var stamp string
if logTimestamp {
stamp = time.Now().Format(time.RFC3339)
}
return stamp
}
func logDebug(messages ...any) {
stamp := time.Now().Format(time.RFC3339)
stamp := getLogStamp()
fmt.Fprintln(messageWriter, stamp, "debug", messages)
}
func logInfo(messages ...any) {
stamp := time.Now().Format(time.RFC3339)
stamp := getLogStamp()
fmt.Fprintln(messageWriter, stamp, "info", messages)
}
func logError(messages ...any) {
stamp := time.Now().Format(time.RFC3339)
stamp := getLogStamp()
fmt.Fprintln(messageWriter, stamp, "error", messages)
}
func logAccess(messages ...any) {
stamp := time.Now().Format(time.RFC3339)
stamp := getLogStamp()
fmt.Fprintln(accessWriter, stamp, "access", messages)
}
@@ -43,3 +54,8 @@ func SetAccessWriter(writer io.Writer) {
func SetMessageWriter(writer io.Writer) {
messageWriter = writer
}
func EnableLogTimestamp(enable bool) {
logTimestamp = enable
}