rklogger

package module
v1.2.13 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: Apache-2.0 Imports: 20 Imported by: 31

README

rk-logger

build codecov Go Report Card License

Log initializer written with golang. Currently, support zap logger as default logger and lumberjack as log rotation

Table of Contents generated with DocToc

Installation

go get -u github.com/rookie-ninja/rk-logger

Quick Start

We combined zap config and lumberjack config in the same config file Both of the configs could keep same format as it

In order to init zap logger with full log rotation, rk-logger support three different utility functions

  • With zap+lumberjack config file path
  • With zap+lumberjack config as byte array
  • With zap config and lumberjack config
With Config file path

config:

---
level: debug
encoding: console
outputPaths:
  - stdout
  - logs/rk-logger.log
errorOutputPaths:
  - stderr
initialFields:
  initFieldKey: fieldValue
encoderConfig:
  messageKey: messagea
  levelKey: level
  nameKey: logger
  timeKey: time
  callerKey: caller
  stacktraceKey: stacktrace
  callstackKey: callstack
  errorKey: error
  timeEncoder: iso8601
  fileKey: file
  levelEncoder: capital
  durationEncoder: second
  callerEncoder: full
  nameEncoder: full
  sampling:
    initial: '3'
    thereafter: '10'
maxsize: 1
maxage: 7
maxbackups: 3
localtime: true
compress: true

Example:

func NewZapLoggerWithConfPathExample() {
    // get current working directory
    dir, _ := os.Getwd()

    // init logger 
    logger, _, _ := rk_logger.NewZapLoggerWithConfPath(path.Clean(path.Join(dir, "/assets/zap.yaml")), rk_logger.YAML)
    
    // use it 
    logger.Info("NewZapLoggerWithConfPathExample")
}
With Config as byte array

Example:

func NewZapLoggerWithBytesExample() {
    bytes := []byte(`{
      "level": "debug",
      "encoding": "console",
      "outputPaths": ["stdout", "logs/rk-logger.log"],
      "errorOutputPaths": ["stderr"],
      "initialFields": {"initFieldKey": "fieldValue"},
      "encoderConfig": {
        "messageKey": "message",
        "levelKey": "level",
        "nameKey": "logger",
        "timeKey": "time",
        "callerKey": "caller",
        "stacktraceKey": "stacktrace",
        "callstackKey": "callstack",
        "errorKey": "error",
        "timeEncoder": "iso8601",
        "fileKey": "file",
        "levelEncoder": "capital",
        "durationEncoder": "second",
        "callerEncoder": "full",
        "nameEncoder": "full",
        "sampling": {
            "initial": "3",
            "thereafter": "10"
        }
      },
      "maxsize": 1,
      "maxage": 7,
      "maxbackups": 3,
      "localtime": true,
      "compress": true
    }`)

    logger, _, err := rk_logger.NewZapLoggerWithBytes(bytes, rk_logger.JSON)
    
    logger.Info("NewZapLoggerWithBytesExample")
}
With Config
func NewZapLoggerWithConfExample() {
    encodingConfig := zapcore.EncoderConfig{
        TimeKey:        "zap_timestamp",
        LevelKey:       "level",
        NameKey:        "logger",
        CallerKey:      "caller",
        MessageKey:     "msg",
        StacktraceKey:  "stacktrace",
        LineEnding:     zapcore.DefaultLineEnding,
        EncodeLevel:    zapcore.CapitalLevelEncoder,
        EncodeTime:     zapcore.ISO8601TimeEncoder,
        EncodeDuration: zapcore.SecondsDurationEncoder,
        EncodeCaller:   zapcore.ShortCallerEncoder,
    }
    
    config := &zap.Config{
        Level: zap.NewAtomicLevelAt(zap.InfoLevel),
        EncoderConfig: encodingConfig,
        OutputPaths: []string{"stdout", "logs/rk-logger.log"},
    }

    logger, _ := rk_logger.NewZapLoggerWithConf(config, &lumberjack.Logger{})
    logger.Info("NewZapLoggerWithConfExample")
}
Development Status: Stable
Contributing

We encourage and support an active, healthy community of contributors — including you! Details are in the contribution guide and the code of conduct. The rk maintainers keep an eye on issues and pull requests, but you can also report any negative conduct to lark@rkdev.info.


Released under the Apache 2.0 License.

Documentation

Overview

Package rklogger contains couple of utility functions for initializing zap logger.

Index

Constants

View Source
const (
	// JSON https://www.json.org/
	JSON FileType = 0
	// YAML https://yaml.org/
	YAML FileType = 1

	// EncodingConsole console encoding style of logging
	EncodingConsole = "console"
	// EncodingJson console encoding style of logging
	EncodingJson = "json"
)

Variables

View Source
var (
	// StdoutEncoderConfig is default zap logger encoder config whose output path is stdout.
	StdoutEncoderConfig = NewZapStdoutEncoderConfig()
	// StdoutLoggerConfig is default zap logger config whose output path is stdout.
	StdoutLoggerConfig = &zap.Config{
		Level:             zap.NewAtomicLevelAt(zap.InfoLevel),
		Development:       true,
		Encoding:          "console",
		DisableStacktrace: true,
		EncoderConfig:     *StdoutEncoderConfig,
		OutputPaths:       []string{"stdout"},
		ErrorOutputPaths:  []string{"stderr"},
	}
	// StdoutLogger is default zap logger whose output path is stdout.
	StdoutLogger, _ = StdoutLoggerConfig.Build()
	// NoopLogger is default zap noop logger.
	NoopLogger = zap.NewNop()

	// EventLoggerConfigBytes is default zap logger which is used by EventLogger.
	EventLoggerConfigBytes = []byte(`{
     "level": "info",
     "encoding": "console",
     "outputPaths": ["stdout"],
     "errorOutputPaths": ["stderr"],
     "initialFields": {},
     "encoderConfig": {
       "messageKey": "msg",
       "levelKey": "",
       "nameKey": "",
       "timeKey": "",
       "callerKey": "",
       "stacktraceKey": "",
       "callstackKey": "",
       "errorKey": "",
       "timeEncoder": "iso8601",
       "fileKey": "",
       "levelEncoder": "capital",
       "durationEncoder": "second",
       "callerEncoder": "full",
       "nameEncoder": "full"
     },
    "maxsize": 1024,
    "maxage": 7,
    "maxbackups": 3,
    "localtime": true,
    "compress": true
   }`)
	// Default EventLogger and EventLoggerConfig.
	EventLogger, EventLoggerConfig, _ = NewZapLoggerWithBytes(EventLoggerConfigBytes, JSON)

	// LumberjackConfig is default lumberjack config.
	LumberjackConfig = NewLumberjackConfigDefault()
)

Functions

func NewLumberjackConfigDefault added in v1.0.7

func NewLumberjackConfigDefault() *lumberjack.Logger

NewLumberjackConfigDefault creates new default lumberjack config

func NewLumberjackLoggerWithBytes

func NewLumberjackLoggerWithBytes(raw []byte, fileType FileType) (*lumberjack.Logger, error)

NewLumberjackLoggerWithBytes inits lumberjack logger as write sync with raw byte array of config file

func NewLumberjackLoggerWithConfPath

func NewLumberjackLoggerWithConfPath(filePath string, fileType FileType) (*lumberjack.Logger, error)

NewLumberjackLoggerWithConfPath inits lumberjack logger as write sync with lumberjack config file path File path needs to be absolute path

func NewZapEventConfig added in v1.0.7

func NewZapEventConfig() *zap.Config

NewZapEventConfig creates new zap.Config for EventLogger

func NewZapLoggerWithBytes

func NewZapLoggerWithBytes(raw []byte, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error)

NewZapLoggerWithBytes inits zap logger with byte array from content of config file lumberjack.Logger could be empty, if not provided, then, we will use default write sync

func NewZapLoggerWithConf

func NewZapLoggerWithConf(config *zap.Config, lumber *lumberjack.Logger, opts ...zap.Option) (*zap.Logger, error)

NewZapLoggerWithConf inits zap logger with config lumberjack.Logger could be empty, if not provided, then, we will use default write sync

func NewZapLoggerWithConfAndSyncer added in v1.2.5

func NewZapLoggerWithConfAndSyncer(config *zap.Config, lumber *lumberjack.Logger, extraSyncers []zapcore.WriteSyncer, opts ...zap.Option) (*zap.Logger, error)

NewZapLoggerWithConfAndSyncer For backward compatibility with NewZapLoggerWithConf

func NewZapLoggerWithConfPath

func NewZapLoggerWithConfPath(filePath string, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error)

NewZapLoggerWithConfPath init zap logger with config file path File path needs to be absolute path lumberjack.Logger could be empty, if not provided, then, we will use default write sync

func NewZapLoggerWithOverride added in v1.2.4

func NewZapLoggerWithOverride(loggerEncoding string, loggerOutputPath ...string) (*zap.Logger, error)

NewZapLoggerWithOverride create new zap.Logger with override

func NewZapStdoutConfig added in v1.0.7

func NewZapStdoutConfig() *zap.Config

NewZapStdoutConfig creates new stdout config

func NewZapStdoutEncoderConfig added in v1.0.7

func NewZapStdoutEncoderConfig() *zapcore.EncoderConfig

NewZapStdoutEncoderConfig creates new stdout encoder config

func TransformToZapConfig added in v1.0.7

func TransformToZapConfig(wrap *ZapConfigWrap) *zap.Config

TransformToZapConfig transforms wrapped zap config into zap.Config

Types

type FileType

type FileType int

FileType is a config file type which support json and yaml currently.

func (FileType) String

func (fileType FileType) String() string

Stringfy above config file types.

type LokiSyncer added in v1.2.7

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

LokiSyncer which will periodically send logs to Loki

func NewLokiSyncer added in v1.2.5

func NewLokiSyncer(opts ...LokiSyncerOption) *LokiSyncer

NewLokiSyncer create new lokiSyncer

func (*LokiSyncer) AddLabel added in v1.2.7

func (syncer *LokiSyncer) AddLabel(key, value string)

func (*LokiSyncer) Bootstrap added in v1.2.7

func (syncer *LokiSyncer) Bootstrap(context.Context)

Bootstrap run periodic jobs

func (*LokiSyncer) Interrupt added in v1.2.7

func (syncer *LokiSyncer) Interrupt(context.Context)

Interrupt goroutine

func (*LokiSyncer) Sync added in v1.2.7

func (syncer *LokiSyncer) Sync() error

Noop

func (*LokiSyncer) Write added in v1.2.7

func (syncer *LokiSyncer) Write(p []byte) (n int, err error)

Write to logChannel

type LokiSyncerOption added in v1.2.5

type LokiSyncerOption func(syncer *LokiSyncer)

LokiSyncerOption options for lokiSyncer

func WithLokiAddr added in v1.2.5

func WithLokiAddr(addr string) LokiSyncerOption

WithLokiAddr provide loki address

func WithLokiClientTls added in v1.2.6

func WithLokiClientTls(conf *tls.Config) LokiSyncerOption

WithLokiClientTls provide loki http client TLS config

func WithLokiLabel added in v1.2.6

func WithLokiLabel(key, value string) LokiSyncerOption

WithLokiLabel provide labels, should follow isValidLabelName()

func WithLokiMaxBatchSize added in v1.2.6

func WithLokiMaxBatchSize(batchSize int) LokiSyncerOption

WithLokiMaxBatchSize provide max batch size

func WithLokiMaxBatchWaitMs added in v1.2.6

func WithLokiMaxBatchWaitMs(in time.Duration) LokiSyncerOption

WithLokiMaxBatchWaitMs provide max batch wait time in milli

func WithLokiPassword added in v1.2.5

func WithLokiPassword(pass string) LokiSyncerOption

WithLokiPassword provide loki password

func WithLokiPath added in v1.2.5

func WithLokiPath(in string) LokiSyncerOption

WithLokiPath provide loki path

func WithLokiUsername added in v1.2.5

func WithLokiUsername(name string) LokiSyncerOption

WithLokiUsername provide loki username

type ZapConfigWrap added in v1.0.7

type ZapConfigWrap struct {
	// Level is the minimum enabled logging level. Note that this is a dynamic
	// level, so calling Config.Level.SetLevel will atomically change the log
	// level of all loggers descended from this config.
	Level string `json:"level" yaml:"level"`
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stacktraces more liberally.
	Development bool `json:"development" yaml:"development"`
	// DisableCaller stops annotating logs with the calling function's file
	// name and line number. By default, all logs are annotated.
	DisableCaller bool `json:"disableCaller" yaml:"disableCaller"`
	// DisableStacktrace completely disables automatic stacktrace capturing. By
	// default, stacktraces are captured for WarnLevel and above logs in
	// development and ErrorLevel and above in production.
	DisableStacktrace bool `json:"disableStacktrace" yaml:"disableStacktrace"`
	// Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
	Sampling *zap.SamplingConfig `json:"sampling" yaml:"sampling"`
	// Encoding sets the logger's encoding. Valid values are "json" and
	// "console", as well as any third-party encodings registered via
	// RegisterEncoder.
	Encoding string `json:"encoding" yaml:"encoding"`
	// EncoderConfig sets options for the chosen encoder. See
	// zapcore.EncoderConfig for details.
	EncoderConfig zapcore.EncoderConfig `json:"encoderConfig" yaml:"encoderConfig"`
	// OutputPaths is a list of URLs or file paths to write logging output to.
	// See Open for details.
	OutputPaths []string `json:"outputPaths" yaml:"outputPaths"`
	// ErrorOutputPaths is a list of URLs to write internal logger errors to.
	// The default is standard error.
	//
	// Note that this setting only affects internal errors; for sample code that
	// sends error-level logs to a different location from info- and debug-level
	// logs, see the package-level AdvancedConfiguration example.
	ErrorOutputPaths []string `json:"errorOutputPaths" yaml:"errorOutputPaths"`
	// InitialFields is a collection of fields to add to the root logger.
	InitialFields map[string]interface{} `json:"initialFields" yaml:"initialFields"`
}

ZapConfigWrap wraps zap config which copied from zap.Config This is used while parsing zap yaml config to zap.Config with viper because Level would throw an error since it is not a type of string

func TransformToZapConfigWrap added in v1.2.0

func TransformToZapConfigWrap(config *zap.Config) *ZapConfigWrap

TransformToZapConfigWrap unmarshals zap.config

func (*ZapConfigWrap) MarshalJSON added in v1.2.0

func (wrap *ZapConfigWrap) MarshalJSON() ([]byte, error)

MarshalJSON marshals ZapConfigWrap

func (*ZapConfigWrap) UnmarshalJSON added in v1.2.0

func (wrap *ZapConfigWrap) UnmarshalJSON([]byte) error

UnmarshalJSON unmarshal ZapConfigWrap

type ZapEncoderConfigWrap added in v1.2.0

type ZapEncoderConfigWrap struct {
	MessageKey       string `json:"messageKey" yaml:"messageKey"`
	LevelKey         string `json:"levelKey" yaml:"levelKey"`
	TimeKey          string `json:"timeKey" yaml:"timeKey"`
	NameKey          string `json:"nameKey" yaml:"nameKey"`
	CallerKey        string `json:"callerKey" yaml:"callerKey"`
	FunctionKey      string `json:"functionKey" yaml:"functionKey"`
	StacktraceKey    string `json:"stacktraceKey" yaml:"stacktraceKey"`
	LineEnding       string `json:"lineEnding" yaml:"lineEnding"`
	EncodeLevel      string `json:"levelEncoder" yaml:"levelEncoder"`
	EncodeTime       string `json:"timeEncoder" yaml:"timeEncoder"`
	EncodeDuration   string `json:"durationEncoder" yaml:"durationEncoder"`
	EncodeCaller     string `json:"callerEncoder" yaml:"callerEncoder"`
	EncodeName       string `json:"nameEncoder" yaml:"nameEncoder"`
	ConsoleSeparator string `json:"consoleSeparator" yaml:"consoleSeparator"`
}

ZapEncoderConfigWrap wraps zap EncoderConfig which copied from zapcore.EncoderConfig This is used while parsing zap yaml config to zapcore.EncoderConfig with viper because Level would throw an error since it is not a type of string

Directories

Path Synopsis
Package main contains example of rklogger usages.
Package main contains example of rklogger usages.

Jump to

Keyboard shortcuts

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