2023-05-04 18:16:41 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
2023-07-11 18:08:00 +00:00
|
|
|
"time"
|
2023-05-04 18:16:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type destinationFile struct {
|
|
|
|
file *os.File
|
|
|
|
buf bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDestinationFile(filePath string) (destination, error) {
|
|
|
|
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &destinationFile{
|
|
|
|
file: f,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-07-11 18:08:00 +00:00
|
|
|
func (d *destinationFile) log(t time.Time, level Level, format string, args ...interface{}) {
|
2023-05-04 18:16:41 +00:00
|
|
|
d.buf.Reset()
|
2023-07-11 18:08:00 +00:00
|
|
|
writeTime(&d.buf, t, false)
|
2023-05-04 18:16:41 +00:00
|
|
|
writeLevel(&d.buf, level, false)
|
|
|
|
writeContent(&d.buf, format, args)
|
|
|
|
d.file.Write(d.buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *destinationFile) close() {
|
|
|
|
d.file.Close()
|
|
|
|
}
|