-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 优化 survey,移除 All 函数,新增 Flusher 接口,可自行实现其他持久化方式
- Loading branch information
1 parent
c6f8c19
commit d9ba1bc
Showing
5 changed files
with
92 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package survey | ||
|
||
// Flusher 用于刷新缓冲区的接口 | ||
type Flusher interface { | ||
// Flush 将缓冲区的数据持久化 | ||
Flush(records []string) | ||
// Info 返回当前刷新器的信息 | ||
Info() string | ||
} |
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,73 @@ | ||
package survey | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/kercylan98/minotaur/utils/log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// NewFileFlusher 创建一个文件刷新器 | ||
// - layout 为日志文件名的时间戳格式 (默认为 time.DateOnly) | ||
func NewFileFlusher(filePath string, layout ...string) *FileFlusher { | ||
fn := filepath.Base(filePath) | ||
ext := filepath.Ext(fn) | ||
fn = strings.TrimSuffix(fn, ext) | ||
dir := filepath.Dir(filePath) | ||
fl := &FileFlusher{ | ||
dir: dir, | ||
fn: fn, | ||
fe: ext, | ||
layout: time.DateOnly, | ||
layoutLen: len(time.DateOnly), | ||
} | ||
if len(layout) > 0 { | ||
fl.layout = layout[0] | ||
fl.layoutLen = len(fl.layout) | ||
} | ||
return fl | ||
} | ||
|
||
type FileFlusher struct { | ||
dir string | ||
fn string | ||
fe string | ||
layout string | ||
layoutLen int | ||
} | ||
|
||
func (slf *FileFlusher) Flush(records []string) { | ||
var ( | ||
file *os.File | ||
writer *bufio.Writer | ||
err error | ||
last string | ||
) | ||
for _, data := range records { | ||
tick := data[0:slf.layoutLen] | ||
if tick != last { | ||
if file != nil { | ||
_ = writer.Flush() | ||
_ = file.Close() | ||
} | ||
fp := filepath.Join(slf.dir, fmt.Sprintf("%s.%s%s", slf.fn, tick, slf.fe)) | ||
file, err = os.OpenFile(fp, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) | ||
if err != nil { | ||
log.Fatal("Survey", log.String("Action", "DateSwitch"), log.String("FilePath", fp), log.Err(err)) | ||
return | ||
} | ||
writer = bufio.NewWriterSize(file, 1024*10240) | ||
last = tick | ||
} | ||
_, _ = writer.WriteString(data) | ||
} | ||
_ = writer.Flush() | ||
_ = file.Close() | ||
} | ||
|
||
func (slf *FileFlusher) Info() string { | ||
return fmt.Sprintf("%s/%s.${DATE}%s", slf.dir, slf.fn, slf.fe) | ||
} |
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