×

gobox中的log操作

96
ligang1109
2018.08.11 16:02 字数 325

今天来说下使用gobox中的log操作

log级别定义

const (
    LEVEL_EMERGENCY = 0
    LEVEL_ALERT     = 1
    LEVEL_CRITICAL  = 2
    LEVEL_ERROR     = 3
    LEVEL_WARNING   = 4
    LEVEL_NOTICE    = 5
    LEVEL_INFO      = 6
    LEVEL_DEBUG     = 7
)

var LogLevels map[int][]byte = map[int][]byte{
    LEVEL_EMERGENCY: []byte("emergency"),
    LEVEL_ALERT:     []byte("alert"),
    LEVEL_CRITICAL:  []byte("critical"),
    LEVEL_ERROR:     []byte("error"),
    LEVEL_WARNING:   []byte("warning"),
    LEVEL_NOTICE:    []byte("notice"),
    LEVEL_INFO:      []byte("info"),
    LEVEL_DEBUG:     []byte("debug"),
}

重要的interface

IWriter

定义消息写入到哪里,如磁盘、队列等

type IWriter interface {
    io.Writer

    Flush() error
    Free()
}

包中实现了如下对象:

记录到单个文件

type FileWriter struct

记录到文件,按天或小时切割

const (
    SPLIT_BY_DAY  = 1
    SPLIT_BY_HOUR = 2
)

type FileWithSplitWriter struct

输出到console,这个写工具调试时用处最多

type ConsoleWriter struct

buffer,这个是对writer的一层装饰,有有效提升写入性能

buffer使用示例:

package main

import (
    "github.com/goinbox/golog"

    "time"
)

func main() {
    golog.InitBufferAutoFlushRoutine(1024, time.Second*3)     // buffer会启动一个单独的goroutine去做buffer的定时flush

    path := "/tmp/test_buffer.log"
    bufsize := 1024

    fw, _ := golog.NewFileWriter(path)
    bw := golog.NewBuffer(fw, bufsize)

    bw.Write([]byte("test file writer with buffer and time interval\n"))

    time.Sleep(time.Second * 5)
    bw.Free()

    golog.FreeBuffers()
}

IFormater

formater可以对要记录的消息做一次格式化加工

type IFormater interface {
    Format(level int, msg []byte) []byte
} 

包里预置了几个常用的formater实现:

simpleFormater

简单程序使用

func NewSimpleFormater() *simpleFormater

webFormater

web程序使用

func NewWebFormater(logId, ip []byte) *webFormater

consoleFormater

console输出使用

func NewConsoleFormater() *consoleFormater

ILogger

这个定义程序中负责记录log的对象

type ILogger interface {
    Debug(msg []byte)
    Info(msg []byte)
    Notice(msg []byte)
    Warning(msg []byte)
    Error(msg []byte)
    Critical(msg []byte)
    Alert(msg []byte)
    Emergency(msg []byte)

    Log(level int, msg []byte) error

    Flush() error
    Free()
}

本包中提供了两个对象实现:

简单同步logger

同步操作,写log时阻塞程序执行

func NewSimpleLogger(writer IWriter, globalLevel int, formater IFormater) (*simpleLogger, error)

使用示例:

package main

import (
    "github.com/goinbox/golog"

    "time"
)

func main() {
    golog.InitBufferAutoFlushRoutine(1024, time.Second*7)

    fw, _ := golog.NewFileWriter("/tmp/test_simple_buffer_logger.log")
    bw := golog.NewBuffer(fw, 1024)
    logger, _ := golog.NewSimpleLogger(bw, golog.LEVEL_INFO, golog.NewSimpleFormater())

    msg := []byte("test simple buffer logger")

    logger.Debug(msg)
    logger.Info(msg)
    logger.Notice(msg)
    logger.Warning(msg)
    logger.Error(msg)
    logger.Critical(msg)
    logger.Alert(msg)
    logger.Emergency(msg)

    logger.Free()
}

异步logger

异步logger将导致阻塞的写操作放到一个单独的goroutine中执行,能有效提升系统性能。

笔者在一个web项目中压测了使用同步log和异步log的性能对比:

同步:

gobox-log-sync.png

异步:

gobox-log-async.png

使用示例:

package main

import (
    "github.com/goinbox/golog"

    "time"
)

func main() {
    golog.InitBufferAutoFlushRoutine(1024, time.Second*7)
    golog.InitAsyncLogRoutine(4096)

    defer func() {
        golog.FreeBuffers()
        golog.FreeAsyncLogRoutine()
    }()

    fw, _ := golog.NewFileWriter("/tmp/test_async_web_logger.log")
    bw := golog.NewBuffer(fw, 1024)
    sl, _ := golog.NewSimpleLogger(bw, golog.LEVEL_INFO, golog.NewWebFormater([]byte("async_web"), []byte("127.0.0.1")))
    logger := golog.NewAsyncLogger(sl)

    msg := []byte("test async web logger")

    logger.Debug(msg)
    logger.Info(msg)
    logger.Notice(msg)
    logger.Warning(msg)
    logger.Error(msg)
    logger.Critical(msg)
    logger.Alert(msg)
    logger.Emergency(msg)

    logger.Free()
}

欢迎大家使用,使用中有遇到问题随时反馈,我们会尽快响应,谢谢!

gobox
Web note ad 1