xlog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: Apache-2.0 Imports: 4 Imported by: 23

README

xlog 日志系统

简介

easy-ngo对Golang的fmt和log做了简单封装,同时引入优秀的开源日志组件zap,对zap进行了方便使用的封装,可以让开发者方便的使用日志功能。开发者可以创建自定义配置的日志实例。同时分别在xfmt和nlog包中,提供了默认的日志实例,开发者可以直接使用,无需配置。

代码结构

easy-ngo的日志位于xlog目录下,抽象日志接口Logger,提供了xfmt,nlog,xzap三种实现
type Logger interface {
	Debugf(format string, params ...interface{})
	Infof(format string, params ...interface{})
	Warnf(format string, params ...interface{})
	Errorf(format string, params ...interface{})
	Panicf(format string, params ...interface{})
	DPanicf(format string, params ...interface{})
	Fatalf(format string, params ...interface{})
	Level() Level
}

核心概念

Level 日志级别,默认级别为Info
  • Debug
  • Info
  • Warn
  • Error
  • DPanic
  • Panic
  • Fatal
Option 是一个配置选项,可以用来配置日志的输出格式,输出位置,日志级别等。其中nlog和xfmt的Option较为简单,xzap的Option则复杂一些。
  • xfmt的Option
    • Level 日志级别
  • nlog的Option
    • Name 日志实例名
    • Flag 对应Golang中log的Flag // log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile | log.Lmsgprefix| log.LUTC| log.Llongfile
    • Level 日志级别
  • xzap的Option
    • Name 日志实例名
    • NoFile 是否为开发模式,如果是true,只显示到标准输出
    • Format 日志格式txt、json、blank,分别为文本、json、空格分割的格式
    • WritableStack 是否需要打印error及以上级别的堆栈信息
    • Skip 跳过的日志层数
    • WritableCaller 是否需要打印行号函数信息
    • Level 日志级别
    • Path 日志存储路径
    • FileName 日志文件名称
    • PackageLevel map[string]string // 包级别日志等级设置
    • ErrlogLevel 错误日志级别,默认error
    • ErrorPath 错误日志存储路径
    • MaxAge 保留日志文件的最大天数,默认7天
    • MaxBackups 保留日志文件的最大个数,默认7个
    • MaxSize 在进行切割之前,日志文件的最大大小(以MB为单位)默认1024
    • Compress 是否压缩/归档旧文件
    • packageLogLevel map[string]zapcore.Level

如何使用

使用默认配置的日志实例
  • 默认xfmt
    • 默认配置
    	DefaultName  = "Defaultxfmt"
    	DefaultLevel = "INFO"
    
    • 使用
    	import (
    		"github.com/NetEase-Media/easy-ngo/xlog/xfmt"
    	)
    	xfmt.Info("hello xfmt")
    
  • 默认nlog
    • 默认配置
    	DefaultName  = "Defaultnlog"
    	DefaultFlag  = "Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix"
    	DefaultLevel = "INFO"
    
    • 使用
    	import (
    		"github.com/NetEase-Media/easy-ngo/xlog/nlog"
    	)
    	nlog.Info("hello nlog")
    
使用自定义配置日志实例

第一步,添加日志的相关配置,支持yaml、toml 在 app.yaml 配置文件中添加如下配置

ngo:
 logger:
  nlog:
   -
    Name : "nlog1"
    Level: "DEBUG"
    Flag : "Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix"
  xzap: 
   -
    Name : "xzap2"
    Level: "INFO"
    NoFile : true
    Format : "text"
    WritableCaller : true
    Skip : 2
    WritableStack : false
    Path : "./logs"
    FileName : "esay-ngo"
    ErrlogLevel : "ERROR"
    ErrorPath : "./logs"
    MaxAge : 7
    MaxBackups : 7
    MaxSize : 1024
    Compress : false

或在 app.toml 配置文件中添加如下配置

[[ngo.logger.nlog]]
    Name = "nlog1"
    Level = "DEBUG"
    Flag = "Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix"
[[ngo.logger.xzap]]
    Name = "xzap1"
    Level= "INFO"
    NoFile = true
    Format = "text"
    WritableCaller = true
    Skip = 2
    WritableStack = false
    Path = "./logs"
    FileName = "esay-ngo"
    ErrlogLevel = "ERROR"
    ErrorPath = "./logs"
    MaxAge = 7
    MaxBackups = 7
    MaxSize = 1024
    Compress = false
第二步,使用

在go的启动文件中添加如下代码

package main

import (
	"fmt"

	"github.com/NetEase-Media/easy-ngo/application"
	_ "github.com/NetEase-Media/easy-ngo/application/r/rconfig"
	"github.com/NetEase-Media/easy-ngo/application/r/rlog"
	_ "github.com/NetEase-Media/easy-ngo/application/r/rlog/rzap"
)

// go run main.go -c ./app.yaml
func main() {
	app := application.Default()
	app.Initialize()
	app.Startup()
	nlogLogger := rlog.GetLogger("nlog2")
	if nlogLogger == nil {
		fmt.Print("failed....")
		return
	}
	nlogLogger.Infof("hello world1")

	xzapLogger := rlog.GetLogger("xzap1")
	if xzapLogger == nil {
		fmt.Print("failed....")
		return
	}
	xzapLogger.Infof("hello world2")
	fmt.Print("success")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Level

type Level int8

A Level is a logging priority. Higher levels are more important.

const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel Level = iota - 1
	// InfoLevel is the default logging priority.
	InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel

	// InvalidLevel is an invalid value for Level.
	//
	// Core implementations may panic if they see messages of this level.
	InvalidLevel = _maxLevel + 1
)

func LevelOf

func LevelOf(enab LevelEnabler) Level

LevelOf reports the minimum enabled log level for the given LevelEnabler from Zap's supported log levels, or InvalidLevel if none of them are enabled.

A LevelEnabler may implement a 'Level() Level' method to override the behavior of this function.

func (c *core) Level() Level {
	return c.currentLevel
}

It is recommended that [Core] implementations that wrap other cores use LevelOf to retrieve the level of the wrapped core. For example,

func (c *coreWrapper) Level() Level {
	return zapcore.LevelOf(c.wrappedCore)
}

func ParseLevel

func ParseLevel(text string) (Level, error)

ParseLevel parses a level based on the lower-case or all-caps ASCII representation of the log level. If the provided ASCII representation is invalid an error is returned.

This is particularly useful when dealing with text input to configure log levels.

func (Level) CapitalString

func (l Level) CapitalString() string

CapitalString returns an all-caps ASCII representation of the log level.

func (Level) Enabled

func (l Level) Enabled(lvl Level) bool

Enabled returns true if the given level is at or above this level.

func (*Level) Get

func (l *Level) Get() interface{}

Get gets the level for the flag.Getter interface.

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

MarshalText marshals the Level to text. Note that the text representation drops the -Level suffix (see example).

func (*Level) Set

func (l *Level) Set(s string) error

Set sets the level for the flag.Value interface.

func (Level) String

func (l Level) String() string

String returns a lower-case ASCII representation of the log level.

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(text []byte) error

UnmarshalText unmarshals text to a level. Like MarshalText, UnmarshalText expects the text representation of a Level to drop the -Level suffix (see example).

In particular, this makes it easy to configure logging levels using YAML, TOML, or JSON files.

type LevelEnabler

type LevelEnabler interface {
	Enabled(Level) bool
}

LevelEnabler decides whether a given logging level is enabled when logging a message.

Enablers are intended to be used to implement deterministic filters; concerns like sampling are better implemented as a Core.

Each concrete Level value implements a static LevelEnabler which returns true for itself and all higher logging levels. For example WarnLevel.Enabled() will return true for WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, and FatalLevel, but return false for InfoLevel and DebugLevel.

type Logger

type Logger interface {
	Debugf(format string, params ...interface{})
	Infof(format string, params ...interface{})
	Warnf(format string, params ...interface{})
	Errorf(format string, params ...interface{})
	Panicf(format string, params ...interface{})
	DPanicf(format string, params ...interface{})
	Fatalf(format string, params ...interface{})
	Level() Level
}

func NewNopLogger

func NewNopLogger() Logger

type NopLogger

type NopLogger struct{}

func (NopLogger) DPanicf

func (n NopLogger) DPanicf(format string, params ...interface{})

func (NopLogger) Debugf

func (n NopLogger) Debugf(format string, params ...interface{})

func (NopLogger) Errorf

func (n NopLogger) Errorf(format string, params ...interface{})

func (NopLogger) Fatalf

func (n NopLogger) Fatalf(format string, params ...interface{})

func (NopLogger) Infof

func (n NopLogger) Infof(format string, params ...interface{})

func (NopLogger) Level

func (n NopLogger) Level() Level

func (NopLogger) Panicf

func (n NopLogger) Panicf(format string, params ...interface{})

func (NopLogger) Warnf

func (n NopLogger) Warnf(format string, params ...interface{})

Directories

Path Synopsis
contrib
xzap Module

Jump to

Keyboard shortcuts

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