xzap

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2023 License: MIT Imports: 5 Imported by: 0

README

xzap

基于 zap 二次封装, 此封装的封装理念为

  1. 提供更常用的创建方式
  2. 固定日志格式/风格
  3. 初始化开箱即用的日志实例

使用示例

开箱即用

package main

import (
    "github.com/tofubuns/gocomx/logger/xzap"
)

func main() {
    zap.L().Info("test message", zap.Int("Code", 1))
    zap.L().Sync()
}

原生配置

支持原始包的原生配置

    loggerOptions := []zap.Option{
    	zap.AddCaller(),
    	zap.AddStacktrace(zapcore.DebugLevel),
    }

    logger := xzap.New(os.Stdout, zapcore.DebugLevel, loggerOptions...)
    logger.Debug("test message", zap.Int("Code", 2))
    logger.Sync()

多日志位置输出

让不同的日志输出道不同的位置/路径

    var (
    	urgentLogsFilename = "urgent.log" // 紧急日志
    	normalLogsFilename = "normal.log" // 平常日志
    )

    urgentFile, err := os.OpenFile(urgentLogsFilename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
    if err != nil {
    	zap.L().Error(err.Error())
    }
    defer urgentFile.Close()

    normalFile, err := os.OpenFile(normalLogsFilename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
    if err != nil {
        zap.L().Error(err.Error())
    }
    defer normalFile.Close()

    logger := xzap.NewTree([]xzap.Tree{
    	{
    		Out: urgentFile,
    		Enabler: func(level zapcore.Level) bool {
    			return level >= zapcore.ErrorLevel
    		},
    	},
    	{
    		Out: normalFile,
    		Enabler: func(level zapcore.Level) bool {
    			return level <= zapcore.WarnLevel
    		},
    	},
    })
    logger.Debug("test message", zap.Int("Code", 3))
    logger.Error("test message", zap.Int("Code", 3))
    logger.Sync()

Documentation

Overview

package xzap 通过对 zap 的二次封装来实现了, 通用的创建, 一致化的日志风格.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(out io.Writer, level zapcore.Level, opts ...zap.Option) *zap.Logger

New 根据指定的参数, 创建一个日志实例, 并将其替换为全局日志实例

创建模式为单例模式, 不可以多次使用此函数创建日志实例, 想要获取新的实例应该使用 日志对象上的 With/WithOptions 方法来获取, 否则 新的日志实例将会覆盖旧的实例

func NewCore

func NewCore(out io.Writer, le zapcore.LevelEnabler) zapcore.Core

NewCore 创建一个基于 zap 的标准日志核心

func NewTree

func NewTree(trees []Tree, opts ...zap.Option) *zap.Logger

NewTree 根据指定的参数, 创建一个多核心的日志实例, 每一个Tree结果都对应一个日志核心

创建模式为单例模式, 不可以多次使用此函数创建日志实例, 想要获取新的实例应该使用 日志对象上的 With/WithOptions 方法来获取, 否则 新的日志实例将会覆盖旧的实例

Types

type Tree

type Tree struct {
	Out     io.Writer                // 输出句柄
	Enabler func(zapcore.Level) bool // 启用器
}

Tree 定义了一个输出和启用器函数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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