rzap

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

README

rose-zap

Wrapper class for the logging library uber-go/zap

English | 简体中文

Installation

go get -u github.com/leafney/rose-zap

Import:

import (
    rzap "github.com/leafney/rose-zap"
)

Quick Start

func main(){
    cfg := rzap.NewConfig()
    
    log := rzap.NewLogger(cfg)
    defer log.Sync()
    
    // Use Logger
    log.Info("Say Hello", zap.String("name", "tom"))
    
    // Use SugaredLogger
    log.SInfof("Fetch url: %s", url)
}

Configuration

Default Values
    cfg := rzap.NewConfig()
  • Default log level is info
  • Default output is to console window only
  • Default line ending is \n
  • Default output format is json
  • Default display of file name and line number
  • Default output of stack trace for levels above warning
  • Supports global enable and disable the log output switch
Log Level

Change the default log level using the SetLevel() method. The default value is info.

Valid log levels are: debug, info, warn, error, panic, fatal

    cfg := rzap.NewConfig().
        SetLevel("debug")
Enable switch

Use the SetEnable() method to change whether to enable or disable global log output. The default is true.

Encoder

Change the output encoder using the UseFmtJson() method. The default is Json format, and it can be changed to Plain Text format.

    cfg := rzap.NewConfig().
        UseFmtJson(false)
File Name and Line Number
Toggle Display

Control whether to display the file name and line number using the ShowCaller() method.

    cfg := rzap.NewConfig().
        ShowCaller(false)
Change Caller Skip

If you need to wrap functions like Info, SInfow, etc., you can skip the call to the wrapper function using the SetCallSkip() method. The default value is SetCallSkip(1).

    cfg := rzap.NewConfig().
        SetCallSkip(2).
        ShowCaller(true)
Display Stack Trace

Set whether to display the stack trace using the ShowStacktrace() method. By default, stack traces are displayed for levels above warning.

    cfg := rzap.NewConfig().
        ShowStacktrace(false)
File Output
Default Configuration

Internal use of the third-party library natefinch/lumberjack to implement log rotation. The default configuration is as follows. Custom configurations can be set using SetFileConfig and related methods.

zapcore.AddSync(&lumberjack.Logger{
	Filename:   "logs/rzap.log", // Log file directory, the folder will be created automatically if it does not exist
	MaxSize:    1024,            // File size limit, in MB
	MaxBackups: 0,               // Maximum number of log files to retain
	MaxAge:     1,              // Number of days to retain log files
	LocalTime:  true,            // Whether to use local time for log file rotation, default is UTC
	Compress:   false,           // Whether to compress the log file
})
Single Log File

The OutSingleFile() method writes all log levels to a single log file, and the configuration can include console output.

The default log file name is logs/rzap.log, which can be customized using the SetFileConfig method.

    cfg := rzap.NewConfig().
        OutSingleFile(true)
Multiple Files by Log Level

Typically, for easier troubleshooting by operations personnel, normal logs below the error level are placed in the info.log file, and logs at the error level and above are placed in the error.log file. This can be achieved using the OutMultiFile() method.

    cfg := rzap.NewConfig().
        OutMultiFile(true)

The default log file names are: logs/info.log for normal level logs, and logs/error.log for severe level logs. Custom configurations can be set using the SetFileConfig and related methods.

Separate Console and File Output by Log Level

In some cases, it is necessary to output normal logs below the error level directly to the console, and only place logs at the error level and above in the error.log file. This can be achieved using the OutInfoConsoleErrorFile() method.

    cfg := rzap.NewConfig().
        OutInfoConsoleErrorFile()

The default log file name is logs/error.log, which can be customized using the SetFileConfig and related methods.

Modify Log Rotation Configuration

The internal log rotation operation is implemented using the third-party library Lumberjack. If the default rotation configuration does not meet requirements, custom configurations can be set using the SetFileConfig method.

The following configuration parameters are supported:

  • WithFileName()
  • WithMaxSize()
  • WithMaxBackups()
  • WithMaxAge()
  • WithLocalTime()
  • WithCompress()
    cfg := rzap.NewConfig().
        OutSingleFile(true).
        SetFileConfig(WithMaxBackups(2),WithLocalTime(false))
Log Rotation Configuration by Log Level

For log file rotation configuration by log level, in addition to using the SetFileConfig() method for general settings, you can also use the SetInfoFileConfig() or SetErrorFileConfig() methods to set them separately.

    cfg := rzap.NewConfig().
        OutMultiFile(true).
        SetErrorFileConfig(WithFileName("logs/xyz.log"))
Notes
WithFileName

SetFileConfig(WithFileName()) only supports modifying the log file name for the OutSingleFile() method. For multi-level log output using OutMultiFile() and OutInfoConsoleErrorFile(), use SetInfoFileConfig() or SetErrorFileConfig() to modify it.

WithXXXX

Except for WithFileName(), other custom configuration items are defined based on the following priority:

SetFileConfig() < SetInfoFileConfig()/SetErrorFileConfig()

Documentation

Index

Constants

View Source
const (
	DefaultSingleFilename     = "logs/rzap.log"
	DefaultMultiFilenameInfo  = "logs/info.log"
	DefaultMultiFilenameError = "logs/error.log"
)

Variables

This section is empty.

Functions

func FileWriter added in v0.1.0

func FileWriter(cfg *FileConfig, showStdout bool) zapcore.WriteSyncer

func GetEncode added in v0.1.0

func GetEncode(isJson bool, cfg zapcore.EncoderConfig) zapcore.Encoder

func StdOutWriter added in v0.1.0

func StdOutWriter() zapcore.WriteSyncer

Types

type Config added in v0.0.2

type Config struct {
	// contains filtered or unexported fields
}

func NewConfig added in v0.0.2

func NewConfig() *Config

func (*Config) OutInfoConsoleErrorFile added in v0.1.0

func (c *Config) OutInfoConsoleErrorFile() *Config

OutInfoConsoleErrorFile Default error log path "logs/error.log"

func (*Config) OutMultiFile added in v0.1.0

func (c *Config) OutMultiFile(withConsole bool) *Config

OutMultiFile Default info log path "logs/info.log"; default error log path "logs/error.log"

func (*Config) OutSingleFile added in v0.1.0

func (c *Config) OutSingleFile(withConsole bool) *Config

OutSingleFile Default log path "logs/rzap.log"

func (*Config) SetCallSkip added in v0.1.0

func (c *Config) SetCallSkip(skip int) *Config

func (*Config) SetEnable added in v0.2.0

func (c *Config) SetEnable(enable bool) *Config

func (*Config) SetErrorFileConfig added in v0.1.0

func (c *Config) SetErrorFileConfig(opts ...Option) *Config

func (*Config) SetFileConfig added in v0.1.0

func (c *Config) SetFileConfig(opts ...Option) *Config

func (*Config) SetInfoFileConfig added in v0.1.0

func (c *Config) SetInfoFileConfig(opts ...Option) *Config

func (*Config) SetLevel added in v0.0.2

func (c *Config) SetLevel(level string) *Config

func (*Config) ShowCaller added in v0.1.0

func (c *Config) ShowCaller(enabled bool) *Config

func (*Config) ShowStacktrace added in v0.1.0

func (c *Config) ShowStacktrace(enabled bool) *Config

func (*Config) UseFmtJson added in v0.1.0

func (c *Config) UseFmtJson(enabled bool) *Config

type FileConfig added in v0.1.0

type FileConfig struct {
	FileName   string
	MaxSize    int
	MaxBackups int
	MaxAge     int
	LocalTime  bool
	Compress   bool
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func NewLogger added in v0.0.2

func NewLogger(config *Config) *Logger

func (*Logger) Debug

func (l *Logger) Debug(msg string, fields ...zap.Field)

func (*Logger) Error

func (l *Logger) Error(msg string, fields ...zap.Field)

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, fields ...zap.Field)

func (*Logger) Info

func (l *Logger) Info(msg string, fields ...zap.Field)

func (*Logger) Panic

func (l *Logger) Panic(msg string, fields ...zap.Field)

func (*Logger) SDebug added in v0.0.2

func (l *Logger) SDebug(args ...interface{})

func (*Logger) SDebugf added in v0.0.2

func (l *Logger) SDebugf(template string, args ...interface{})

func (*Logger) SDebugw added in v0.0.2

func (l *Logger) SDebugw(msg string, keysAndValues ...interface{})

func (*Logger) SError added in v0.0.2

func (l *Logger) SError(args ...interface{})

func (*Logger) SErrorf added in v0.0.2

func (l *Logger) SErrorf(template string, args ...interface{})

func (*Logger) SErrorw added in v0.0.2

func (l *Logger) SErrorw(msg string, keysAndValues ...interface{})

func (*Logger) SFatal added in v0.0.2

func (l *Logger) SFatal(args ...interface{})

func (*Logger) SFatalf added in v0.0.2

func (l *Logger) SFatalf(template string, args ...interface{})

func (*Logger) SFatalw added in v0.0.2

func (l *Logger) SFatalw(msg string, keysAndValues ...interface{})

func (*Logger) SInfo added in v0.0.2

func (l *Logger) SInfo(args ...interface{})

func (*Logger) SInfof added in v0.0.2

func (l *Logger) SInfof(template string, args ...interface{})

func (*Logger) SInfow added in v0.0.2

func (l *Logger) SInfow(msg string, keysAndValues ...interface{})

func (*Logger) SPanic added in v0.0.2

func (l *Logger) SPanic(args ...interface{})

func (*Logger) SPanicf added in v0.0.2

func (l *Logger) SPanicf(template string, args ...interface{})

func (*Logger) SPanicw added in v0.0.2

func (l *Logger) SPanicw(msg string, keysAndValues ...interface{})

func (*Logger) SWarn added in v0.0.2

func (l *Logger) SWarn(args ...interface{})

func (*Logger) SWarnf added in v0.0.2

func (l *Logger) SWarnf(template string, args ...interface{})

func (*Logger) SWarnw added in v0.0.2

func (l *Logger) SWarnw(msg string, keysAndValues ...interface{})

func (*Logger) Sync

func (l *Logger) Sync() error

func (*Logger) Warn

func (l *Logger) Warn(msg string, fields ...zap.Field)

type OTP added in v0.1.0

type OTP int8
const (
	OutTypeConsole OTP = iota
	OutTypeSingleFileDefault
	OutTypeSingleFileCustom
	OutTypeMultiFileDefault
	OutTypeMultiFileCustom
	OutTypeInfoError
)

type Option added in v0.1.0

type Option func(f *FileConfig)

func WithCompress added in v0.1.0

func WithCompress(enabled bool) Option

func WithFileName added in v0.1.0

func WithFileName(filename string) Option

func WithLocalTime added in v0.1.0

func WithLocalTime(enabled bool) Option

func WithMaxAge added in v0.1.0

func WithMaxAge(maxAge int) Option

func WithMaxBackups added in v0.1.0

func WithMaxBackups(maxBackups int) Option

func WithMaxSize added in v0.1.0

func WithMaxSize(maxSize int) Option

Jump to

Keyboard shortcuts

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