logs

package
v1.0.39 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 15 Imported by: 25

Documentation

Overview

Package logs 日志.

多级日志,文件滚动,不同级别日志可以分别使用不同文件。

Example
package main

import (
	"os"

	"github.com/xuender/kit/logs"
)

func main() {
	// logs.SetLogFile("/var/tmp", "test.log")
	logs.SetLog(os.Stdout)
	logs.SetLevel(logs.Info)
	logs.I.SetFlags(0)

	logs.T.Println("trace")
	logs.D.Printf("debug: %d", 1)
	logs.I.Println("info")

}
Output:

[I] info

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// T 跟踪.
	T = _loggers[Trace].newLog("[T] ")
	// D 调试.
	D = _loggers[Debug].newLog("[D] ")
	// I 消息.
	I = _loggers[Info].newLog("[I] ")
	// W 警告.
	W = _loggers[Warn].newLog("[W] ")
	// E 错误.
	E = _loggers[Error].newLog("[E] ")
	// RetentionDays 日志保留天数,0为永久.
	RetentionDays = base.Seven
)

nolint: gochecknoglobals, varnamelen

Functions

func Close

func Close() error

Close 关闭日志文件.

func CloseFile added in v1.0.5

func CloseFile(file *os.File) error

CloseFile 关闭指定日志文件.

func Expired added in v1.0.3

func Expired(path, name string, reserved int) error

Expired 保留目录特定文件数量.

func File added in v1.0.3

func File(path, name string) (io.Writer, error)

File 生成软连接文件.

Example
package main

import (
	"github.com/samber/lo"
	"github.com/xuender/kit/logs"
)

func main() {
	writer := lo.Must1(logs.File("/tmp/log", "link.log"))
	defer logs.Close()

	lo.Must1(writer.Write([]byte("xxx")))
}
Output:

func GetLevelName added in v1.0.5

func GetLevelName() string

GetLevelName 日志级别名称.

func Log added in v1.0.3

func Log(values ...any)

Log 输出日志.

Example
package main

import (
	"os"

	"github.com/xuender/kit/logs"
)

func main() {
	logs.SetLog(os.Stdout)
	logs.I.SetFlags(0)
	logs.E.SetFlags(0)

	logs.Log(nil)
	logs.Log(1)
	logs.Log(2, os.ErrClosed)

}
Output:

[I] 1
[E] 2 file already closed

func Rotating added in v1.0.4

func Rotating(path, file string, yield func(string, string) error) func()

Rotating 返回旧日志删除方法.

func SetDebug

func SetDebug(writer io.Writer)

SetDebug 设置调试.

func SetDebugFile

func SetDebugFile(path, file string) error

SetDebugFile 设置调试输出.

func SetError

func SetError(writer io.Writer)

SetError 设置错误.

func SetErrorFile

func SetErrorFile(path, file string) error

SetErrorFile 设置错误文件输出.

func SetInfo

func SetInfo(writer io.Writer)

SetInfo 设置信息.

func SetInfoFile

func SetInfoFile(path, file string) error

SetInfoFile 设置文件输出.

func SetLevel

func SetLevel(level Level)

SetLevel 设置日志级别.

func SetLog

func SetLog(writer io.Writer)

SetLog 默认输出.

func SetLogFile

func SetLogFile(path, file string) error

SetLogFile 默认文件输出.

func SetTrace

func SetTrace(writer io.Writer)

SetTrace 设置跟踪.

func SetTraceFile

func SetTraceFile(path, file string) error

SetTraceFile 设置跟踪文件.

func SetWarn

func SetWarn(writer io.Writer)

SetWarn 设置警告.

func SetWarnFile

func SetWarnFile(path, file string) error

SetWarnFile 设置警告文件输出.

Types

type Level

type Level int

Level 日志级别.

const (
	Trace Level = iota
	Debug
	Info
	Warn
	Error
)

func DefaultLevel added in v1.0.3

func DefaultLevel() Level

DefaultLevel 默认日志级别. 读取环境变量 LOGS_LEVEL 设置默认级别.

func GetLevel

func GetLevel() Level

GetLevel 获取日志级别.

Jump to

Keyboard shortcuts

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