实现日志系统

//定义一个日志写入器接口(LogWriter),要求写入设备必须遵守这个接口协议才能被日志器(Logger)注册。
//日志器有一个写入器的注册方法(Logger 的 RegisterWriter()方法)。
package main

//LogWriter 日志写入器接口
type LogWriter interface {
	// Write 写入方法
	Write(data interface{}) error
}

// Logger 日志
type Logger struct {
	//注册的写入器
	logWriters []LogWriter
}

// RegisterWriter 注册日志写入器
func (log *Logger) RegisterWriter(writer LogWriter) {
	log.logWriters = append(log.logWriters, writer)
}

//Log 日志写入
func (log *Logger) Log(data interface{}) {
	for _, writer := range log.logWriters {
		writer.Write(data)
	}
}

// NewLogger 创建日志器实例
func NewLogger() *Logger {
	return &Logger{}
}