glog

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

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

Go to latest
Published: Jun 21, 2022 License: MIT Imports: 9 Imported by: 0

README

About

一款能够应付日常开发的简单异步日志库。

Feature

  • 格式化日志输出
  • 日志缓冲区大小可配置
  • 异步执行日志文件切割
  • 日志缓冲区flush周期可配置
  • 异步记录日志(终端采用同步输出)
  • 默认不输出日志文件
  • 日志输出级别可配置(默认全部日志)
  • 配置项可选, 可根据自己的需求选择不同Option进行初始化
  • 日志级别划分: Panic(异常, 可以捕获), Fatal(致命错误), Error(错误), Warn(警告), Info(流水), Debug(调试信息)
  • 控制台使用颜色对不同级别的日志进行区分: Panic(红色), Fatal(红色), Error(紫红色), Warn(黄色), Info(蓝色), Debug(白色)
  • 提供不同的日志记录方式: WriteByLevel(区分级别记录在不同的文件), WriteByMerged(所有日志记录在一起), WriteByAll(既区分级别同时也记录在同一个文件)
  • 多种日志文件切割周期

Usage

import (
	"github.com/franklihub/glog"
)

func main() {
	msg := "glog msg"

	glog.Debug(msg)
	glog.Info(msg)
	glog.Warn(msg)
	glog.Error(msg)
	// glog.Fatal(msg)
	glog.Panic(msg)
}

image

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(template string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(template string, args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(template string, args ...interface{})

func Info

func Info(args ...interface{})

func Infof

func Infof(template string, args ...interface{})

func Panic

func Panic(args ...interface{})

func Panicf

func Panicf(template string, args ...interface{})

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(template string, args ...interface{})

Types

type CutOption

type CutOption int // CutOption 日志文件切割周期选项
const (
	CutDaily      CutOption // 根据时间周期,每天切割
	CutHourly               // 根据时间周期,每小时切割
	CutHalfAnHour           // 根据时间周期,每半小时切割
	CutTenMin               // 根据时间周期,每10分钟切割
	CutPer10M               // 根据文件大小,每10M切割一次
	CutPer60M               // 根据文件大小,每60M切割一次
	CutPer100M              // 根据文件大小,每100M切割一次

)

type Level

type Level int // Level 日志级别
const (
	LevelPanic   Level = 1 << (iota - 1) // panic
	LevelFatal                           // 致命错误, 程序会退出
	LevelError                           // 错误
	LevelWarning                         // 警告
	LevelInfo                            // 追踪
	LevelDebug                           // 调试

)

type LogConfig

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

LogConfig 配置项

type Logger

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

Logger 日志记录器

func Log

func Log(opts ...Option) (logger *Logger)

NewLogger 初始化defaultLogger

func (*Logger) Close

func (log *Logger) Close()

type Option

type Option func(c *LogConfig)

func WithAppName

func WithAppName(name string) Option

WithAppName 设置app名称

func WithBufferSize

func WithBufferSize(size int) Option

WithBufferSize 设置日志通道buffer大小: [1024, 1024000]

func WithCutOption

func WithCutOption(cutOption CutOption) Option

WithCutOption 日志文件切割方式

func WithFlushDuration

func WithFlushDuration(duration time.Duration) Option

WithFlushDuration 多久刷盘一次, 单位毫秒: [500ms-5000ms]

func WithLogPath

func WithLogPath(filepath string) Option

WithLogPath 设置日志文件存放目录(如果区分级别存放日志,将会在filepath下创建对应级别的目录用于区分)

func WithMaxTime

func WithMaxTime(sec int64) Option

WithMaxTime 设置日志文件最大保存时间, 单位: 秒

func WithStdout

func WithStdout(b bool) Option

WithStdout 设置是否在终端显示日志输出

func WithWriteLevel

func WithWriteLevel(level Level) Option

WithWriteLevel 日志记录级别: [ LevelFatal, LevelFatal | LevelError | LevelWarning | LevelInfo | LevelDebug ]

func WithWriteOption

func WithWriteOption(opt WriteOption) Option

WithWriteOption 设置写文件选项

type WriteOption

type WriteOption int // WriteOption 日志写选项
const (
	WriteByLevel  WriteOption // 区分级别, 不同级别记录在不同目录下相应的文件中
	WriteByMerged             // 不区分级别, 所有日志记录在一个文件中
	WriteByAll                // 既区分级别记录也一起记录

)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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