上一篇中已经讲述了日志库的需求,这一篇中咱们来实现日志库的原型。单元测试
新建一个项目,这里我用的hm_log
,在项目下新建一个log_interface.go
日志规范,定义日志级别的方法:测试
package hm_log
type Log interface {
Debug(format string, args ...interface{}) // ...表示接收可变参数
Trace(format string, args ...interface{})
Info(format string, args ...interface{})
Warn(format string, args ...interface{})
Error(format string, args ...interface{})
Fatal(format string, args ...interface{})
Close() // 文件须要进行关闭操做
}
复制代码
由于咱们是要打印到文件里面去,因此我还须要新建一个file.go
:ui
package hm_log
type FileLog struct {
logPath string
logName string
}
func NewFileLog(logPath, logName string) Log {
// 为何返回的Log,上一篇中已经说明
return &FileLog{
logPath: logPath,
logName: logName,
}
}
func (f *FileLog) Debug(format string, args ...interface{}) {
}
func (f *FileLog) Trace(format string, args ...interface{}) {
}
func (f *FileLog) Info(format string, args ...interface{}) {
}
func (f *FileLog) Warn(format string, args ...interface{}) {
}
func (f *FileLog) Error(format string, args ...interface{}) {
}
func (f *FileLog) Fatal(format string, args ...interface{}) {
}
func (f *FileLog) Close() {
}
复制代码
如今咱们基本实现了file的初始代码了,而后如今来实现file的功能。实现功能以前,新建一个log_const.go
,咱们须要一些常量。this
package hm_log
const (
DebugLevel = iota
TraceLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
)
复制代码
由于咱们是写入文件中的,因此须要进行打开文件操做。为了在后续的日志查看方便,咱们须要将普通日志和错误日志分开存储。spa
type FileLog struct {
logPath string
logName string
file *os.File
warnFile *os.File
}
func NewFileLog(logPath, logName string) Log {
// 为何返回的Log,上一篇中已经说明
log := &FileLog{
logPath: logPath,
logName: logName,
}
log.init()
return log
}
func (f *FileLog) init() {
// 通常日志
filename := fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755) // os.O_CREATE 建立文件 os.O_APPEND 追加写入 os.O_WRONLY 只写操做
if err != nil {
panic(fmt.Sprintf("open faile %s failed, err: %v", filename, err)
}
f.file = file
// 错误日志
warnfilename := fmt.Sprintf("%s/%s.log.wf", f.logPath, f.logName)
warnfile, err := os.OpenFile(warnfilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755) // os.O_CREATE 建立文件 os.O_APPEND 追加写入 os.O_WRONLY 只写操做
if err != nil {
panic(fmt.Sprintf("open faile %s failed, err: %v", warnfilename, err)
}
f.warnFile = warnfile
}
复制代码
而后就能够实现以前的日志方法了。日志
func (f *FileLog) Debug(format string, args ...interface{}) {
fmt.Fprintf(f.file, format, args...)
fmt.Fprintln(f.file)
}
func (f *FileLog) Trace(format string, args ...interface{}) {
fmt.Fprintf(f.file, format, args...)
fmt.Fprintln(f.file)
}
func (f *FileLog) Info(format string, args ...interface{}) {
fmt.Fprintf(f.file, format, args...)
fmt.Fprintln(f.file)
}
func (f *FileLog) Warn(format string, args ...interface{}) {
fmt.Fprintf(f.warnFile, format, args...)
fmt.Fprintln(f.warnFile)
}
func (f *FileLog) Error(format string, args ...interface{}) {
fmt.Fprintf(f.warnFile, format, args...)
fmt.Fprintln(f.warnFile)
}
func (f *FileLog) Fatal(format string, args ...interface{}) {
fmt.Fprintf(f.warnFile, format, args...)
fmt.Fprintln(f.warnFile)
}
func (f *FileLog) Close() {
f.file.Close()
f.warnFile.Close()
}
复制代码
到这里文件日志库基本完成了,当写完一个功能时,咱们须要进行单元测试,因此咱们新建一个log_test.go
来测试咱们写的文件日志库。code
package log
import (
"testing"
)
func TestFileLog(t *testing.T) {
log := NewFileLog(".", "test")
log.Debug("this is file debub test")
log.Warn("this is file warn test")
log.Close()
}
复制代码
使用go test进行单元测试。而后能够看到咱们的项目下多了一个test.log和test.log.wf文件。打开文件,能够看到咱们的日志已经成功写入了日志文件里面。下一篇咱们完善这个文件日志库原型。orm