You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/pkg/log/file.go

98 lines
1.9 KiB

6 years ago
package log
import (
"context"
"io"
"path/filepath"
"time"
"github.com/go-kratos/kratos/pkg/log/internal/filewriter"
6 years ago
)
// level idx
const (
_debugIdx = iota
_infoIdx
6 years ago
_warnIdx
_errorIdx
_fatalIdx
6 years ago
_totalIdx
)
var _fileNames = map[int]string{
_debugIdx: "debug.log",
6 years ago
_infoIdx: "info.log",
_warnIdx: "warning.log",
_errorIdx: "error.log",
_fatalIdx: "fatal.log",
6 years ago
}
// FileHandler .
type FileHandler struct {
render Render
fws [_totalIdx]*filewriter.FileWriter
}
// NewFile crete a file logger.
func NewFile(dir string, bufferSize, rotateSize int64, maxLogFile int) *FileHandler {
// new info writer
newWriter := func(name string) *filewriter.FileWriter {
var options []filewriter.Option
if rotateSize > 0 {
options = append(options, filewriter.MaxSize(rotateSize))
}
if maxLogFile > 0 {
options = append(options, filewriter.MaxFile(maxLogFile))
}
w, err := filewriter.New(filepath.Join(dir, name), options...)
if err != nil {
panic(err)
}
return w
}
handler := &FileHandler{
render: newPatternRender("[%D %T] [%L] [%S] %M"),
}
for idx, name := range _fileNames {
handler.fws[idx] = newWriter(name)
}
return handler
}
// Log loggint to file .
func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D) {
6 years ago
d := toMap(args...)
6 years ago
// add extra fields
addExtraField(ctx, d)
d[_time] = time.Now().Format(_timeFormat)
var w io.Writer
switch lv {
case _debugLevel:
w = h.fws[_debugIdx]
6 years ago
case _warnLevel:
w = h.fws[_warnIdx]
case _errorLevel:
w = h.fws[_errorIdx]
case _fatalLevel:
w = h.fws[_fatalIdx]
6 years ago
default:
w = h.fws[_infoIdx]
}
h.render.Render(w, d)
w.Write([]byte("\n"))
}
// Close log handler
func (h *FileHandler) Close() error {
for _, fw := range h.fws {
// ignored error
fw.Close()
}
return nil
}
// SetFormat set log format
func (h *FileHandler) SetFormat(format string) {
h.render = newPatternRender(format)
}