log

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 9 Imported by: 0

README

通用日志框架

本log只是一个框架,并不处理log实际的输出
具体的输出实现由dirver完成(driver是实现log.FormatWriter的对象)

获取

go get -u github.com/afocus/log

基本使用


import (
    "github.com/afocus/log"
    "github.com/afocus/log/driver/console"
)


func main(){
    // 创建一个向控制台输出的log对象
    g := log.New(log.DEBUG,console.New())
    g.Info("hello,world")
    g.Error("error message")
}

输出到多个输出源


import (
    "github.com/afocus/log"
    "github.com/afocus/log/driver/console"
    "github.com/afocus/log/driver/file"
)


func main(){
    // 同时输出到文件和控制台
    g := log.New(log.DEBUG,console.New(), file.New(&file.Option{...}))
    g.Info("hello,world")
    g.Error("error message")
}

高级(带有关联关系的日志)

Ctx(id string) 用于创建一个关联日志组

包含的方法 Tag 用于设置标签 支持链式调用


import (
    "github.com/afocus/log"
    "github.com/afocus/log/driver/console"
)


func main(){
    // 创建一个向控制台输出的log对象
    g := log.New(log.DEBUG,console.New())

    // 创建一个日志组并关联一个id
    gx := g.Ctx("000000001").Tag("login")
    gx.Info("user:xxx,pwd:xxx")
    gx.Error("pwd is error")
    // 添加标签
    gx.Tag("newtag").Warn("dang")
    // 添加附加字段信息
    gx.Fields(map[string]interface{}{"name":"afocus"})
    // 用完需要释放
    gx.Free()
}

输出级别

分为6级,只有当日志级别大于等于设置的级别才会输出

定义
Level 说明
DEBUG 指明细致的事件信息,对调试应用最有用
INFO 指明描述信息,从粗粒度上描述了应用运行过程
WARN 指明潜在的有害状况
ERROR 指明错误事件,但应用可能还能继续运行
FATAL 指明非常严重的错误事件,可能会导致应用终止执行 (自带调用堆栈信息)
OFF 最高级别,用于关闭日志
输出方法
  • Debug(v ...interface{})
  • Info(v ...interface{})
  • Warn(v ...interface{})
  • Error(v ...interface{})
  • Fatal(v ...interface{})
  • Debugf(s string, v ...interface{})
  • Infof(s string, v ...interface{})
  • Warnf(s string, v ...interface{})
  • Errorf(s string, v ...interface{})
  • Fatalf(s string, v ...interface{})

Driver

格式化输出接口
// Formater 格式化日志事件到字符串
type Formater interface {
	Format(*Event) []byte
}

// FormatWriter 格式化输入
// 用于定制日志输出内容的样式
type FormatWriter interface {
	io.Writer
	Formater
}

实现自己的日志driver 将日志格式输出为json并通过tcp持续发送


type MyDriver struct{
    c net.Conn
}

func (*MyDriver) Format(ev *log.Event)[]byte{
    b,_:=json.Marshal(ev)
    return b
}

func (d *MyDriver) Write(d []byte) int,error{
    return d.c.Write(d)
}

func main(){
    conn,err:=net.Dial("tcp","....")
    myd:=&MyDriver{c:conn}

    // 启用
    g:=log.New(log.DEBUG, myd)
    ...
}



目前已实现的driver
  • 控制台 带颜色的控制台输出(windows暂时无色)
  • 文件 支持文件分割
  • 网络 目前仅实现了通过http发送日志的功能并且高度自定义

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FormatPattern = func(ev *Event) []byte {
	var pre string
	if ev.Action != "" || ev.ID != "" {
		pre = fmt.Sprintf("[%s] %s %s (%s#%s)-> ", ev.Timestamp, ev.Level, ev.File, ev.Action, ev.ID)
	} else {
		pre = fmt.Sprintf("[%s] %s %s-> ", ev.Timestamp, ev.Level, ev.File)
	}
	d := []byte(pre + ev.Message)
	if length := len(d); d[length-1] != '\n' {
		d = append(d, '\n')
	}
	if ev.Data != nil {
		d = append(d, "fields-> "...)
		databytes, _ := json.Marshal(ev.Data)
		d = append(d, databytes...)
		d = append(d, '\n')
	}
	return d
}

FormatPattern 扁平化格式化日志事件

View Source
var TimestampLayout = "2006-01-02 15:04:05"

TimestampLayout 日志时间格式化模板

Functions

func CreateID

func CreateID() string

CreateID 简单的返回一个随机字符串id

Types

type Ctx

type Ctx struct {
	// contains filtered or unexported fields
}

Ctx 携带日志id和事件名的日志对象 主要用于通过id串联一些日志 起到查询方便

func (*Ctx) Debug

func (ctx *Ctx) Debug(s ...interface{}) *Ctx

func (*Ctx) Debugf

func (ctx *Ctx) Debugf(s string, args ...interface{}) *Ctx

func (*Ctx) Error

func (ctx *Ctx) Error(s ...interface{}) *Ctx

func (*Ctx) Errorf

func (ctx *Ctx) Errorf(s string, args ...interface{}) *Ctx

func (*Ctx) Fatal

func (ctx *Ctx) Fatal(s ...interface{}) *Ctx

func (*Ctx) Fatalf

func (ctx *Ctx) Fatalf(s string, args ...interface{}) *Ctx

func (*Ctx) Fields

func (ctx *Ctx) Fields(data interface{}) *Ctx

func (*Ctx) Free

func (ctx *Ctx) Free()

Free 释放

func (*Ctx) Info

func (ctx *Ctx) Info(s ...interface{}) *Ctx

func (*Ctx) Infof

func (ctx *Ctx) Infof(s string, args ...interface{}) *Ctx

func (*Ctx) Print added in v0.0.2

func (ctx *Ctx) Print(s ...interface{})

Print

func (*Ctx) Tag

func (ctx *Ctx) Tag(tag string) *Ctx

Tag 设置标签名

func (*Ctx) Warn

func (ctx *Ctx) Warn(s ...interface{}) *Ctx

func (*Ctx) Warnf

func (ctx *Ctx) Warnf(s string, args ...interface{}) *Ctx

type Event

type Event struct {
	SrvName string `json:"service"`
	// 日志产生时的时间
	Timestamp string `json:"timestamp"`
	// 日志等级
	Level Level `json:"level"`
	// 所在文件行数file:line
	// main:20
	File string `json:"file,omitempty"`
	// 日志id 只有Ctx方式才会使用
	// 主要用于上下文关联
	ID string `json:"guid,omitempty"`
	// 日志动作名称 描述干什么的 如 login,callback...
	Action string `json:"action,omitempty"`
	// 日志内容
	Message string `json:"message"`
	// data
	Data interface{} `json:"data,omitempty"`
}

Event 日志事件 记录了日志的必要信息 可以通过Formater接口输入格式化样式后的数据

type FormatWriter

type FormatWriter interface {
	io.Writer
	Formater
}

FormatWriter 格式化输入 用于定制日志输出内容的样式

type Formater

type Formater interface {
	Format(*Event) []byte
}

Formater 格式化日志事件到字符串

type Level

type Level int32

Level 日志级别类型

const (

	// DEBUG 指明细致的事件信息,对调试应用最有用
	DEBUG Level = iota
	// INFO 指明描述信息,从粗粒度上描述了应用运行过程
	INFO
	// WARN 指明潜在的有害状况
	WARN
	// ERROR 指明错误事件,但应用可能还能继续运行
	ERROR
	// FATAL 指明非常严重的错误事件,可能会导致应用终止执行
	FATAL
	// OFF 最高级别,用于关闭日志
	OFF
)

func (Level) MarshalJSON

func (l Level) MarshalJSON() ([]byte, error)

MarshalJSON 保证json编码level是不是显示数字而是string

func (Level) String

func (l Level) String() string

String 返回日志等级的描述

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger 日志对象

func New

func New(lvl Level, outs ...FormatWriter) *Logger

func (*Logger) Ctx

func (o *Logger) Ctx(id string) *Ctx

Ctx 创建一个包含指定id的ctx对象

func (*Logger) Debug

func (o *Logger) Debug(s ...interface{})

Debug

func (*Logger) Debugf

func (o *Logger) Debugf(s string, args ...interface{})

func (*Logger) Error

func (o *Logger) Error(s ...interface{})

func (*Logger) Errorf

func (o *Logger) Errorf(s string, args ...interface{})

func (*Logger) Fatal

func (o *Logger) Fatal(s ...interface{})

func (*Logger) Fatalf

func (o *Logger) Fatalf(s string, args ...interface{})

func (*Logger) Info

func (o *Logger) Info(s ...interface{})

func (*Logger) Infof

func (o *Logger) Infof(s string, args ...interface{})

func (*Logger) Output

func (o *Logger) Output(calldept int, level Level, acname, id, msg string, data interface{}) error

Output 输出日志消息 核心方法 所有日志输出全部以及此方法

func (*Logger) Print

func (o *Logger) Print(s ...interface{})

Print Debug的别名

func (*Logger) SetSrvName

func (o *Logger) SetSrvName(s string)

func (*Logger) Warn

func (o *Logger) Warn(s ...interface{})

func (*Logger) Warnf

func (o *Logger) Warnf(s string, args ...interface{})

func (*Logger) Write

func (o *Logger) Write(b []byte) (int, error)

Directories

Path Synopsis
driver
net

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL