go 开发属于自己的日志库-文件日志库原型实现

栏目: Go · 发布时间: 7年前

内容简介:上一篇中已经讲述了日志库的需求,这一篇中我们来实现日志库的原型。新建一个项目,这里我用的因为我们是要打印到文件里面去,所以我还需要新建一个

上一篇中已经讲述了日志库的需求,这一篇中我们来实现日志库的原型。

新建一个项目,这里我用的 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 :

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 ,我们需要一些常量。

package hm_log

const (
	DebugLevel = iota
	TraceLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)
复制代码

因为我们是写入文件中的,所以需要进行打开文件操作。为了在后续的日志查看方便,我们需要将普通日志和错误日志分开存储。

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 来测试我们写的文件日志库。

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文件。打开文件,可以看到我们的日志已经成功写入了日志文件里面。下一篇我们完善这个文件日志库原型。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java多线程编程实战指南(设计模式篇)

Java多线程编程实战指南(设计模式篇)

黄文海 / 电子工业出版社 / 2015-10 / 59.00

随着CPU 多核时代的到来,多线程编程在充分利用计算资源、提高软件服务质量方面扮演了越来越重要的角色。而 解决多线程编程中频繁出现的普遍问题可以借鉴设计模式所提供的现成解决方案。然而,多线程编程相关的设计模式书籍多采用C++作为描述语言,且书中所举的例子多与应用开发人员的实际工作相去甚远。《Java多线程编程实战指南(设计模式篇)》采用Java(JDK1.6)语言和UML 为描述语言,并结合作者多......一起来看看 《Java多线程编程实战指南(设计模式篇)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具