Last active
January 2, 2022 21:37
-
-
Save erayakartuna/dd9e9832eac74fa8edcb701f6debc1ae to your computer and use it in GitHub Desktop.
Logv4.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// InitRequestLogs ... | |
func InitRequestLogs() { | |
go func() { | |
for { | |
time.Sleep(time.Second * 5) | |
writeLogs() | |
} | |
}() | |
} | |
func writeLogs() { | |
reqLogs.Lock() | |
tmp := append(reqLogs.logs[:0:0], reqLogs.logs...) | |
reqLogs.logs = nil | |
reqLogs.Unlock() | |
file := new(bytes.Buffer) | |
for _, v := range tmp { | |
_, _ = file.WriteString(v) | |
} | |
// write to s3 | |
sess := session.Must(session.NewSession()) | |
sess.Config.Region = aws.String("eu-west-1") | |
client := s3.New(sess) | |
_, _ = client.PutObject(&s3.PutObjectInput{ | |
Bucket: aws.String("bucket-name"), | |
Key: aws.String(fmt.Sprintf("%s.json", time.Now().String())), | |
Body: bytes.NewReader(file.Bytes()), | |
ContentType: aws.String("text/json"), | |
ContentDisposition: aws.String("attachment"), | |
ServerSideEncryption: aws.String("AES256"), | |
ACL: aws.String("private"), | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment