logging

package module
v0.0.0-...-ea0e1bd Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: MIT Imports: 21 Imported by: 0

README

logging

  • ELK日志搜索终结者
  • 无需数据库,支持超大(GB级别)全链路日志毫秒级别搜索。
  • 更新log在线查看技术架构,由ajax短轮询升级为SSE推送.
  • 统一日志管理界面
  • 支持反向代理日志节点
  • 日志全链路搜索支持分布式、支持单机。
  • 2022/05/14: 重磅更新:路由地址添加接口化
    • 新增支持标准库http包(http.ServerMux)、支持gin框架、(可以灵活添加支持各种go的web框架,实现Router接口即可.)
    • 注:使用gin不再对反向代理模式查看日志支持(支持http.ServerMux),未来考虑淘汰此种方式,请使用log-navi导航查看实时滚动日志
  • 请注意: 如果服务端的http server配置了writer_timeout,SSE的日志滚动推送将断开,
    • 产生错误: net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)
项目结构示意图

avatar

API
  • Init: 通用的初始化方式, http服务启动前进行调用。
  • RegisterGin: 低层级的初始化方式,一般推荐使用Init。
Usage
  • main.go

package main

import (
	"flag"
	"fmt"
	"io"
	"os"

	"github.com/gin-gonic/gin"
	"github.com/vito-go/logging"
	"github.com/vito-go/logging/tid"
	"github.com/vito-go/mylog"
)

func main() {
	port := flag.Int("p", 9899, "specrify port ")
	flag.Parse()
	engine := gin.Default()
	appName := "chat"
	logInfoPath := "chat.log"
	logErrPath := "chat-err.log"
	fInfo, err := os.OpenFile(logInfoPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
	if err != nil {
		panic(err)
	}
	fErr, err := os.OpenFile(logInfoPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
	if err != nil {
		panic(err)
	}
	mylog.Init(fInfo, io.MultiWriter(fInfo, fErr), io.MultiWriter(fInfo, fErr), "tid")
	unilogServerAddr := ""                        // 单机版本,分布式注册中心地址可为空
	basePath := "/universe/api/im/unilog/logging" // 单机版本,分布式注册中心地址可为空
	logging.Init(engine, *port, logging.BasePath(basePath), unilogServerAddr, logging.Config{
		APPName:     appName,
		Token:       "abc123",
		LogInfoPath: logInfoPath,
		LogErrPath:  logErrPath,
	})
	engine.GET("/hello", func(ctx *gin.Context) {
		ctx.Set("tid", tid.Get())
		mylog.Ctx(ctx).WithField("path", ctx.Request.URL.Path).Info("request==>")
		ctx.JSON(200, "hello")
	})
	engine.Run(fmt.Sprintf(":%d", *port))
}

go run ./main.go

http://127.0.0.1:9899/universe/api/v1/im/unilog/chat/chat.log

  • input the login token: abc123

avatar

  • we can see the log stream avatar
通过tid对日志全链路进行搜索

http://127.0.0.1:9899/universe/api/v1/im/unilog/chat/tid-search

avatar

TODO
  • 自动滚屏日志目前采用SSE技术方案推送服务日志,计划支持websocket
  • 目前仅支持gin框架,计划支持所有的web框架
  • 目前不支持切割后的日志文件的搜索(暂且不建议切割,或者有需要的话日志达到特别大如1GB后再进行考虑切割)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoRunTidSearch

func GoRunTidSearch(logPath string)

GoRunTidSearch tid搜索引擎服务. 这里一定要传logPath而不是和日志相同的文件句柄*os.File

func Init

func Init(r router.Router, httpPort int, path BasePath, unilogAddr string, cfg Config)

Init (high-level) r can user router.NewGin or router.NewServeMux as the router.Router. httpPort is the port of the http server listened. unilogAddr can be empty if only used locally.

func MustCheckBasePath

func MustCheckBasePath(path BasePath)

MustCheckBasePath look at the rule of BasePath.

func RegisterRouter

func RegisterRouter(r router.Router, logInfoPath, loginPath, tidSearchPath, token string, logPaths ...LogPath) error

RegisterRouter like Init but it is low-level. logInfoPath 日志路径(包含所有等级的日志, loginPath 日志页面登录路由地址,tidSearchPath 日志搜索页面地址 token 登录授权码, logPaths: 日志与该日志所对应的路由地址

Types

type BasePath

type BasePath string

BasePath the root path, it should:

1. must not be empty;

2. must begin with /

3. must not end with /

4 must not contain one of the basePathNot

type Config

type Config struct {
	APPName     string // APPName 服务名称
	Token       string // Token sha1加密字符串.
	LogInfoPath string // LogInfoPath info级别日志文件路径,info日志应该包括所有等级(warn、err)的日志
	LogErrPath  string // LogInfoPath err级别日志文件路径,有可能包含warn日志
}

Config logging配置

type LogPath

type LogPath struct {
	FileName, RouterPath string
}

Directories

Path Synopsis
Package tid 生成traceId进行日志追踪,基于时间纳秒级别。
Package tid 生成traceId进行日志追踪,基于时间纳秒级别。
Package unilog .分布式日志注册中心.
Package unilog .分布式日志注册中心.

Jump to

Keyboard shortcuts

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