sugar

package module
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 8 Imported by: 1

README

sugar lib

封装好的一些常用方法

使用方法
go get github.com/serialt/sugar/v2

go get golang.org/x/exp/slog

库使用方法
简单使用日志
package main

import (
	"github.com/serialt/sugar/v2"
	"golang.org/x/exp/slog"
)

func init() {
	slog.SetDefault(sugar.New())
}

func main() {
	slog.Debug("debug", "hello", "world")
	slog.Info("info", "hello", "world")
	slog.Error("error", "hello", "world")
}

可选参数
package main

import (
	sugar "github.com/serialt/sugar/v2"
	"golang.org/x/exp/slog"
)

func init() {
	options := []sugar.LogOptions{
		sugar.WithLevel("debug"),
		sugar.WithShort(false),
		sugar.WithType("json"),
	}
	slog.SetDefault(sugar.New(options...))
}
func main() {

	slog.Debug("debug msg")
	slog.Info("info msg")
}

复杂使用
package main

import (
	sugar "github.com/serialt/sugar/v2"
	flag "github.com/spf13/pflag"
	"golang.org/x/exp/slog"
)

type Config struct {
	Server   string
	Port     string
	LogLevel string
}

var (
	cfgfile string
	config  *Config
)

func init() {

	flag.StringVarP(&cfgfile, "config", "c", "config.yaml", "Config file")
	flag.CommandLine.SortFlags = false
	flag.Parse()

	err := sugar.LoadConfig(cfgfile, &config)
	if err != nil {
		config = new(Config)
	}
	slog.SetDefault(sugar.New(
		sugar.WithLevel(config.LogLevel),
		))

}
func main() {
	slog.Info("struct", "cfg", config)
	slog.Debug("debug msg")
	slog.Info("info msg")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsFile

func IsFile(path string) bool

IsFile

func LevelToSlogLevel

func LevelToSlogLevel(level string) slog.Level

LevelToZapLevel 转换日志级别

func LoadConfig

func LoadConfig(configPath string, out interface{}) (err error)

Load Read config from file.

func New

func New(options ...LogOptions) *slog.Logger

func NewSlog

func NewSlog(lg *Log) *slog.Logger

Types

type Log

type Log struct {
	Level      string // 日志级别
	File       string // 日志文件存放路径,如果为空,则输出到控制台
	Type       string // 日志类型,支持 txt 和 json ,默认txt
	MaxSize    int    //单位M
	MaxBackups int    // 日志文件保留个数
	MaxAge     int    // 单位天
	Compress   bool   // 压缩轮转的日志
	Short      bool   // 以包/文件:行号 显示短路径,不显示全路径
}

type LogOptions

type LogOptions func(*Log)

func WithCompress

func WithCompress(compress bool) LogOptions

func WithFile

func WithFile(file string) LogOptions

func WithLevel

func WithLevel(level string) LogOptions

func WithMaxSize

func WithMaxSize(maxSize int) LogOptions

func WithShort

func WithShort(Short bool) LogOptions

func WithType

func WithType(tp string) LogOptions

Directories

Path Synopsis
* @Description : Blue Planet * @Author : serialt * @Email : tserialt@gmail.com * @Created Time : 2023-04-18 08:31:58 * @Last modified : 2023-07-05 13:07:37 * @FilePath : /sugar/exmaple/main.go * @Other : * @ : * * *
* @Description : Blue Planet * @Author : serialt * @Email : tserialt@gmail.com * @Created Time : 2023-04-18 08:31:58 * @Last modified : 2023-07-05 13:07:37 * @FilePath : /sugar/exmaple/main.go * @Other : * @ : * * *

Jump to

Keyboard shortcuts

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