sugar

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 8 Imported by: 1

README

sugar lib

封装好的一些常用方法

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

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

func main() {
	// 设置简单日志参数
	sugar.SetLog("error", "log.txt")
	sugar.Debug("debug logSugar")
	sugar.Info("info logSugar")
	sugar.Error("error logSugar")
}
复杂使用
package main

import (
	"fmt"
	"os"

	flag "github.com/spf13/pflag"

	"github.com/serialt/sugar"
	"go.uber.org/zap"
	"gopkg.in/yaml.v3"
)

var (
	// 版本信息
	appVersion bool // 控制是否显示版本
	APPVersion = "v0.0.2"
	BuildTime  = "2006-01-02 15:04:05"
	GitCommit  = "xxxxxxxxxxx"

	// Logger   *zap.Logger
	LogSugar *zap.SugaredLogger
)


func init() {
	
	// Logger = sugar.NewLogger(LogLevel, LogFile)
	LogSugar = sugar.NewSugarLogger("info", "ccc", "", false)
}

func main() {
	LogSugar.Debug("debug log")
	LogSugar.Info("info log")
	LogSugar.Error("error log")
}

更多复杂使用可以参考sugar.NewLogger的实现

package main

import (
	"fmt"
	"os"

	"github.com/serialt/sugar"
	flag "github.com/spf13/pflag"
	"go.uber.org/zap"
)

var (
	// 版本信息
	appVersion bool // 控制是否显示版本
	APPVersion = "v0.0.2"
	BuildTime  = "2006-01-02 15:04:05"
	GitCommit  = "xxxxxxxxxxx"

	// 配置文件,置空则表示读取项目根目录里的config.yaml
	ConfigPath = "config.yaml"

	// Logger   *zap.Logger
	LogSugar *zap.SugaredLogger
	Config   *MyConfig
)

type Log struct {
	LogLevel string `yaml:"logLevel"` // 日志级别,支持debug,info,warn,error,panic
	LogFile  string `yaml:"logFile"`  // 日志文件存放路径,如果为空,则输出到控制台
}

type MyConfig struct {
	Log Log    `yaml:"log"`
	Msg string `yaml:"msg"`
}

func init() {
	flag.BoolVarP(&appVersion, "version", "v", false, "Display build and version msg")
	flag.StringVarP(&ConfigPath, "cfgFile", "c", ConfigPath, "")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		fmt.Println("使用说明")
		flag.PrintDefaults()
	}
	flag.ErrHelp = fmt.Errorf("\n\nSome errors have occurred, check and try again !!! ")
	flag.CommandLine.SortFlags = false
	flag.Parse()

	// 读取配置文件
	err := sugar.LoadConfig(ConfigPath, &Config)
	if err != nil {
		Config = new(MyConfig)
	}
}

func main() {
	if appVersion {
		fmt.Printf("APPVersion: %v  BuildTime: %v  GitCommit: %v\n",
			APPVersion,
			BuildTime,
			GitCommit)
		return
	}

	LogSugar.Debug("debug log")
	LogSugar.Info("info log")
	LogSugar.Error("error log")
}


Documentation

Index

Constants

This section is empty.

Variables

Functions

func Debug

func Debug(args ...interface{})

func Debugf

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

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

func Error

func Error(args ...interface{})

func Errorf

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

func Errorw

func Errorw(msg string, keysAndValues ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Fatalw

func Fatalw(msg string, keysAndValues ...interface{})

func Info

func Info(args ...interface{})

func Infof

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

func Infow

func Infow(msg string, keysAndValues ...interface{})

func IsFile added in v0.0.14

func IsFile(path string) bool

IsFile

func LevelToZapLevel

func LevelToZapLevel(level string) zapcore.Level

LevelToZapLevel 转换日志级别

func LoadConfig added in v0.0.13

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

Load Read config from file.

func NewLogger

func NewLogger(logLevel, logFile, logtype string, logColor bool) *zap.Logger

NewLogger 自定日志配置可以参考此方法

func NewSugarLogger

func NewSugarLogger(logLevel, logFile, logType string, logColor bool) *zap.SugaredLogger

NewSugarLogger 创建一个sugar

func SetLog

func SetLog(level string, file string)

SetLog 用于配置简单的日志

func Upgrade added in v0.0.14

func Upgrade(log *zap.Logger)

Upgrade

func Warn

func Warn(args ...interface{})

func Warnf

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

func Warnw

func Warnw(msg string, keysAndValues ...interface{})

Types

type Logger

type Logger struct {
	LogLevel      string // 日志级别
	LogFile       string // 日志文件存放路径,如果为空,则输出到控制台
	LogType       string // 日志类型,支持 txt 和 json ,默认txt
	LogMaxSize    int    //单位M
	LogMaxBackups int    // 日志文件保留个数
	LogMaxAge     int    // 单位天
	LogCompress   bool   // 压缩轮转的日志
	LogColor      bool   // 日志级别分颜色
}

func New

func New() *Logger

New 创建一个默认的 *Logger 结构

func (*Logger) NewCore

func (lg *Logger) NewCore() (core zapcore.Core)

NewCore 创建一个自定义的zapcore.Core

func (*Logger) NewMyLogger

func (lg *Logger) NewMyLogger(core zapcore.Core) *zap.Logger

NewMyLogger 生成一个 *zap.Logger,用于给外部调用

Jump to

Keyboard shortcuts

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