-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9b0578
commit fd0f614
Showing
12 changed files
with
152 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package third | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/OpenIMSDK/protocol/third" | ||
uploadfile "github.com/openimsdk/openim-sdk-core/v3/internal/file" | ||
"github.com/openimsdk/openim-sdk-core/v3/internal/util" | ||
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant" | ||
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback" | ||
) | ||
|
||
func (c *Third) UploadLogs(ctx context.Context, params []sdk_params_callback.UploadLogParams) error { | ||
|
||
return c.uploadLogs(ctx, params) | ||
} | ||
|
||
func (t *Third) uploadLogs(ctx context.Context, params []sdk_params_callback.UploadLogParams) error { | ||
|
||
logFilePath := t.LogFilePath | ||
files, err := os.ReadDir(logFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
req := third.UploadLogsReq{} | ||
errsb := strings.Builder{} | ||
for _, file := range files { | ||
if !checkLogPath(file.Name()) { | ||
continue | ||
} | ||
var filename = filepath.Join(logFilePath, file.Name()) | ||
resp, err := t.fileUploader.UploadFile(ctx, &uploadfile.UploadFileReq{Filepath: filename, Name: file.Name(), Cause: "upload_logs"}, nil) | ||
if err != nil { | ||
errsb.WriteString(err.Error()) | ||
} | ||
var fileURL third.FileURL | ||
fileURL.Filename = filename | ||
fileURL.URL = resp.URL | ||
req.FileURLs = append(req.FileURLs, &fileURL) | ||
} | ||
errs := errsb.String() | ||
if errs != "" { | ||
return errors.New(errs) | ||
} | ||
_, err = util.CallApi[third.UploadLogsResp](ctx, constant.UploadLogsRouter, &req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkLogPath(logpath string) bool { | ||
if len(logpath) < len("open-im-sdk-core.yyyy-mm-dd") { | ||
return false | ||
} | ||
logTime := logpath[len(logpath)-len(".yyyy-mm-dd"):] | ||
if _, err := time.Parse(".2006-01-02", logTime); err != nil { | ||
return false | ||
} | ||
if !strings.HasPrefix(logpath, "open-im-sdk-core.") { | ||
return false | ||
} | ||
|
||
return true | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package third | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestLogMatch(t *testing.T) { | ||
|
||
filenames := []string{ | ||
"log1.txt", | ||
"log2.log", | ||
"log3.log.txt", | ||
"log4.log.2022-01-01", | ||
"log5.log.2022-01-01.txt", | ||
"log20230918.log", | ||
"OpenIM.CronTask.log.all.2023-09-18", "OpenIM.log.all.2023-09-18", | ||
} | ||
|
||
expected := []string{ | ||
"OpenIM.CronTask.log.all.2023-09-18", "OpenIM.log.all.2023-09-18", | ||
} | ||
|
||
var actual []string | ||
for _, filename := range filenames { | ||
if checkLogPath(filename) { | ||
actual = append(actual, filename) | ||
} | ||
} | ||
|
||
if len(actual) != len(expected) { | ||
t.Errorf("Expected %d matches, but got %d", len(expected), len(actual)) | ||
} | ||
|
||
for i := range expected { | ||
if actual[i] != expected[i] { | ||
t.Errorf("Expected match %d to be %q, but got %q", i, expected[i], actual[i]) | ||
} | ||
} | ||
} |
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
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
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
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
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
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