mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-23 15:32:15 +08:00
docs: 手动实现 simple log 日志库
写入指定日志文件
This commit is contained in:
parent
d1fdc3eacb
commit
2e5e381d70
@ -37,6 +37,7 @@ var (
|
|||||||
|
|
||||||
// Logger 日志类
|
// Logger 日志类
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
|
fn string
|
||||||
w io.Writer
|
w io.Writer
|
||||||
timeFormat string
|
timeFormat string
|
||||||
loggerFormat string
|
loggerFormat string
|
||||||
@ -101,15 +102,18 @@ type Option struct {
|
|||||||
TimeFormat string
|
TimeFormat string
|
||||||
LogFormat string
|
LogFormat string
|
||||||
LevelFlag LevelFlag
|
LevelFlag LevelFlag
|
||||||
|
LogFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
type OptionFunc func(*Option)
|
type OptionFunc func(*Option)
|
||||||
|
|
||||||
func WithWriter(writer io.Writer) OptionFunc {
|
func WithWriter(writer io.Writer) OptionFunc {
|
||||||
return func(option *Option) {
|
return func(option *Option) {
|
||||||
|
if len(option.LogFile) == 0 {
|
||||||
option.Writer = writer
|
option.Writer = writer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithTimeFormat(timeFormat string) OptionFunc {
|
func WithTimeFormat(timeFormat string) OptionFunc {
|
||||||
return func(option *Option) {
|
return func(option *Option) {
|
||||||
@ -129,12 +133,26 @@ func WithLogFlag(flag LevelFlag) OptionFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithLogFile(filePath string) OptionFunc {
|
||||||
|
return func(option *Option) {
|
||||||
|
if len(filePath) > 0 {
|
||||||
|
option.LogFile = filePath
|
||||||
|
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
option.Writer = file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 默认参数
|
// 默认参数
|
||||||
var defaultOption = &Option{
|
var defaultOption = &Option{
|
||||||
Writer: os.Stdout,
|
Writer: os.Stdout,
|
||||||
TimeFormat: "2006-01-02 15:04:05.99",
|
TimeFormat: "2006-01-02 15:04:05.99",
|
||||||
LogFormat: "%-22s %s %v --- [%20s] : %s\n",
|
LogFormat: "%-22s %s %v --- [%20s] : %s\n",
|
||||||
LevelFlag: FlagInfo | FlagWarn | FlagError | FlagDebug,
|
LevelFlag: FlagInfo | FlagWarn | FlagError | FlagDebug,
|
||||||
|
LogFile: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewOption 构造 Option 参数
|
// NewOption 构造 Option 参数
|
||||||
@ -155,5 +173,6 @@ func NewLog(opts ...OptionFunc) *Logger {
|
|||||||
timeFormat: opt.TimeFormat,
|
timeFormat: opt.TimeFormat,
|
||||||
loggerFormat: opt.LogFormat,
|
loggerFormat: opt.LogFormat,
|
||||||
flag: opt.LevelFlag,
|
flag: opt.LevelFlag,
|
||||||
|
fn: opt.LogFile,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ func TestLogToFile(t *testing.T) {
|
|||||||
|
|
||||||
fmt.Printf("写入日志文件 %s\n", logFilePath)
|
fmt.Printf("写入日志文件 %s\n", logFilePath)
|
||||||
log := NewLog(
|
log := NewLog(
|
||||||
WithWriter(f),
|
WithLogFile(logFilePath),
|
||||||
WithLogFlag(FlagInfo|FlagDebug|FlagWarn),
|
WithLogFlag(FlagInfo|FlagDebug|FlagWarn),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user