Documentation ¶
Overview ¶
Package rklogger contains couple of utility functions for initializing zap logger.
Index ¶
- Constants
- Variables
- func NewLumberjackConfigDefault() *lumberjack.Logger
- func NewLumberjackLoggerWithBytes(raw []byte, fileType FileType) (*lumberjack.Logger, error)
- func NewLumberjackLoggerWithConfPath(filePath string, fileType FileType) (*lumberjack.Logger, error)
- func NewZapEventConfig() *zap.Config
- func NewZapLoggerWithBytes(raw []byte, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error)
- func NewZapLoggerWithConf(config *zap.Config, lumber *lumberjack.Logger, opts ...zap.Option) (*zap.Logger, error)
- func NewZapLoggerWithConfAndSyncer(config *zap.Config, lumber *lumberjack.Logger, ...) (*zap.Logger, error)
- func NewZapLoggerWithConfPath(filePath string, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error)
- func NewZapLoggerWithOverride(loggerEncoding string, loggerOutputPath ...string) (*zap.Logger, error)
- func NewZapStdoutConfig() *zap.Config
- func NewZapStdoutEncoderConfig() *zapcore.EncoderConfig
- func TransformToZapConfig(wrap *ZapConfigWrap) *zap.Config
- type FileType
- type LokiSyncer
- type LokiSyncerOption
- func WithLokiAddr(addr string) LokiSyncerOption
- func WithLokiClientTls(conf *tls.Config) LokiSyncerOption
- func WithLokiLabel(key, value string) LokiSyncerOption
- func WithLokiMaxBatchSize(batchSize int) LokiSyncerOption
- func WithLokiMaxBatchWaitMs(in time.Duration) LokiSyncerOption
- func WithLokiPassword(pass string) LokiSyncerOption
- func WithLokiPath(in string) LokiSyncerOption
- func WithLokiUsername(name string) LokiSyncerOption
- type ZapConfigWrap
- type ZapEncoderConfigWrap
Constants ¶
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 ¶
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 ¶
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
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
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.
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
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