docs: 手动实现 simple log 日志库

This commit is contained in:
Shikong 2021-10-06 15:31:33 +08:00
parent 11361a4dab
commit cd1c2fe26d
3 changed files with 186 additions and 0 deletions

3
simplelog/go.mod Normal file
View File

@ -0,0 +1,3 @@
module simplelog
go 1.16

148
simplelog/simplelog.go Normal file
View File

@ -0,0 +1,148 @@
package simplelog
import (
"fmt"
"io"
"os"
"runtime"
"time"
)
// 自定义日志
type LogLevel struct {
s string
flag LevelFlag
}
func (l LogLevel) String() string {
return l.s
}
type LevelFlag uint8
const (
FlagInfo LevelFlag = 0x1
FlagWarn LevelFlag = 0x2
FlagError LevelFlag = 0x4
FlagDebug LevelFlag = 0x8
)
var (
INFO = LogLevel{"INFO", FlagInfo}
WARN = LogLevel{"WARN", FlagWarn}
ERROR = LogLevel{"ERROR", FlagError}
DEBUG = LogLevel{"DEBUG", FlagDebug}
)
// Logger 日志类
type Logger struct {
w io.Writer
timeFormat string
loggerFormat string
flag LevelFlag
}
func (l *Logger) time() string {
now := time.Now()
return now.Format(l.timeFormat)
}
func (l *Logger) PrintLog(level LogLevel, log string) {
// 根据 flag 位 限制日志级别
if l.flag&level.flag != level.flag {
return
}
pc, _, _, ok := runtime.Caller(2)
var f *runtime.Func
if ok {
f = runtime.FuncForPC(pc)
_, _ = fmt.Fprintf(l.w, l.loggerFormat, l.time(), level, pc, f.Name(), log)
} else {
_, _ = fmt.Fprintf(l.w, l.loggerFormat, l.time(), level, "--", "Unknown", log)
}
}
// Debug 调试输出
func (l *Logger) Debug(log string) {
l.PrintLog(DEBUG, log)
}
// Info 消息输出
func (l *Logger) Info(log string) {
l.PrintLog(INFO, log)
}
// Warn 消息输出
func (l *Logger) Warn(log string) {
l.PrintLog(WARN, log)
}
// Error 消息输出
func (l *Logger) Error(log string) {
l.PrintLog(ERROR, log)
}
// 函数式 选项模式
type Option struct {
Writer io.Writer
TimeFormat string
LogFormat string
LevelFlag LevelFlag
}
type OptionFunc func(*Option)
func WithWriter(writer io.Writer) OptionFunc {
return func(option *Option) {
option.Writer = writer
}
}
func WithTimeFormat(timeFormat string) OptionFunc {
return func(option *Option) {
option.TimeFormat = timeFormat
}
}
func WithLogFormat(logFormat string) OptionFunc {
return func(option *Option) {
option.LogFormat = logFormat
}
}
func WithLogFlag(flag LevelFlag) OptionFunc {
return func(option *Option) {
option.LevelFlag = flag
}
}
// 默认参数
var defaultOption = &Option{
Writer: os.Stdout,
TimeFormat: "2006-01-02 15:04:05.99",
LogFormat: "%-23s %s %v --- [%20s] : %s\n",
LevelFlag: FlagInfo | FlagWarn | FlagError | FlagDebug,
}
func NewOption(opts ...OptionFunc) (opt *Option) {
opt = defaultOption
for _, o := range opts {
o(opt)
}
return
}
// NewLog 构造函数
func NewLog(opts ...OptionFunc) *Logger {
opt := NewOption(opts...)
return &Logger{
w: opt.Writer,
timeFormat: opt.TimeFormat,
loggerFormat: opt.LogFormat,
flag: opt.LevelFlag,
}
}

View File

@ -0,0 +1,35 @@
package simplelog
import (
"fmt"
"os"
"testing"
"time"
)
func TestLog(t *testing.T) {
log := NewLog()
log2 := NewLog(
WithWriter(os.Stdout),
WithTimeFormat("2006-01-02"),
WithLogFormat("%-10s %s %v %s =-=-> %s\n"),
WithLogFlag(FlagInfo|FlagDebug),
)
fmt.Printf("simpleLog => %#v\n", log)
fmt.Println("=========================================================")
for i := 0; i < 5; i++ {
log.Debug("测试 Debug 输出")
time.Sleep(100 * time.Millisecond)
}
fmt.Println("=========================================================")
for i := 0; i < 5; i++ {
log2.Debug("测试 Debug 输出")
time.Sleep(100 * time.Millisecond)
}
}