plogs

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2022 License: MIT Imports: 14 Imported by: 4

README

Feature

  • 格式化日志输出
  • 异步输出日志到文件(终端采用同步输出)
  • 可通过WithWriter()添加自定义Writer
  • temp.log总是当前正在输出的日志文件
  • 日志输出级别可配置(默认输出所有级别的日志)
  • 日志文件切割方式: 达到指定大小后执行切割, 默认不切割
  • 提供日志文件保存时长设置: 超过该时长的文件将被删除, 默认不作删除操作
  • 日志级别划分: Panic(异常, 可以捕获), Fatal(致命错误), Error(错误), Warn(警告), Info(流水), Debug(调试信息)
  • 提供不同的日志记录方式: WriteByLevelSeparated(根据Level记录在不同的子目录下), WriteByLevelMerged(所有Level的日志记录在一起), WriteByBoth(单独记录与归并记录同时存在)

Usage

package main

import (
	"fmt"
	"time"

	"github.com/pyihe/go-pkg/tools"
	"github.com/pyihe/plogs"
)

func main() {
	opts := []plogs.Option{
		plogs.WithName("ALTIMA"),
		plogs.WithFileOption(plogs.WriteByLevelMerged),
		plogs.WithLogPath("./logs"),
		plogs.WithLogLevel(plogs.LevelFatal | plogs.LevelError | plogs.LevelWarn | plogs.LevelInfo | plogs.LevelDebug),
		plogs.WithStdout(true),
		plogs.WithMaxAge(24 * time.Hour),
		plogs.WithMaxSize(10 * 1024 * 1024),
	}

	logger := plogs.NewLogger(opts...)
	defer logger.Close()

	go func() {
		tag := time.Now()
		for n := 0; n < 3; n++ {
			for i := 1; i < 500000; i++ {
				plogs.Debugf("hello, this is level panic!")
				plogs.Infof("hello, this is level panic!")
				plogs.Warnf("hello, this is level panic!")
				plogs.Errorf("hello, this is level panic!")
			}
		}
		fmt.Printf("time consume: %v\n", time.Now().Sub(tag).Milliseconds())
	}()

	tools.Wait()
}

Terminal Screenshot

File Screenshot

Thanks

感谢Jetbrains开源开发许可证 提供的免费开发工具支持!

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 FileOption

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

)

type Level

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

)

type LogConfig

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

LogConfig 配置项

type Logger

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

func NewLogger

func NewLogger(opts ...Option) *Logger

func (*Logger) Close

func (l *Logger) Close()

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) Debugf

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

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) Errorf

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

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf

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

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) Infof

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

func (*Logger) Panic

func (l *Logger) Panic(args ...interface{})

func (*Logger) Panicf

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

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) Warnf

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

type Option

type Option func(c *Logger)

func WithFileOption

func WithFileOption(opt FileOption) Option

WithFileOption 设置写文件选项

func WithLogLevel

func WithLogLevel(level Level) Option

WithLogLevel 日志记录级别: [ LevelFatal | LevelFatal | LevelError | LevelWarn | LevelInfo | LevelDebug ]

func WithLogPath

func WithLogPath(filepath string) Option

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

func WithMaxAge

func WithMaxAge(t time.Duration) Option

WithMaxAge 设置日志文件保存最长时间

func WithMaxSize

func WithMaxSize(size int64) Option

WithMaxSize 设置日志文件保存上限

func WithName

func WithName(name string) Option

WithName 设置app名称

func WithStdout

func WithStdout(b bool) Option

WithStdout 设置是否同步输出到标准输出

func WithWriter

func WithWriter(writer ...internal.LogWriter) Option

WithWriter 添加自定义Writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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