logrotate

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Example (NamedUsage)
package main

import (
	"path/filepath"
	"syscall"

	"go.ytsaurus.tech/library/go/core/log"
	"go.ytsaurus.tech/library/go/core/log/zap"
	"go.ytsaurus.tech/library/go/core/log/zap/logrotate"
)

func main() {
	// Note: each scheme can be registered only once and can not be unregistered
	// If you want to provide custom unused scheme name(remember to check for errors):
	_ = logrotate.RegisterNamedLogrotateSink("rotate-usr1", syscall.SIGUSR1)
	// Now we create logger using that cheme
	cfg := zap.JSONConfig(log.DebugLevel)
	logPath, _ := filepath.Abs("./example.log")
	cfg.OutputPaths = []string{"rotate-usr1://" + logPath}
	logger, _ := zap.New(cfg)
	// Now file will be reopened by SIGUSR1
	logger.Debug("this log should be reopened by SIGHUP")
}
Output:

Example (SimpleUsage)
package main

import (
	"path/filepath"
	"syscall"

	"go.ytsaurus.tech/library/go/core/log"
	"go.ytsaurus.tech/library/go/core/log/zap"
	"go.ytsaurus.tech/library/go/core/log/zap/logrotate"
)

func main() {
	// Basic usage, when you don't need any custom preferences is quite easy.
	// register our logrotate sink and force it to reopen files on sighup(remember to check for errors)
	_ = logrotate.RegisterLogrotateSink(syscall.SIGHUP)
	// create zap logger as usual, using `logrotate://` instead of omitting it or using `file://`
	cfg := zap.JSONConfig(log.DebugLevel)
	logPath, _ := filepath.Abs("./example.log")
	cfg.OutputPaths = []string{"logrotate://" + logPath}
	logger, _ := zap.New(cfg)
	// That's all, when your process receives SIGHUP file will be reopened
	logger.Debug("this log should be reopened by SIGHUP")
}
Output:

Example (StandaloneUsage)
package main

import (
	"net/url"
	"syscall"

	uberzap "go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"go.ytsaurus.tech/library/go/core/log/zap/logrotate"
)

func main() {
	// If you don't want to register scheme, or use custom logging core you can do this(remember to check for errors):
	u, _ := url.ParseRequestURI("/tmp/example.log")
	sink, _ := logrotate.NewLogrotateSink(u, syscall.SIGHUP)

	encoder := zapcore.NewConsoleEncoder(zapcore.EncoderConfig{MessageKey: "msg"})
	core := zapcore.NewCore(encoder, sink, uberzap.NewAtomicLevel())
	logger := uberzap.New(core)
	// Now file will be reopened by SIGHUP
	logger.Debug("this log should be reopened by SIGHUP")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotSupported = errors.New("logrotate sink is not supported on your platform")

Functions

func NewLogrotateSink

func NewLogrotateSink(u *url.URL, sig ...os.Signal) (zap.Sink, error)

Factory for logrotate sink, which accepts os.Signals to listen to for reloading Generally if you don't build your own core it is used by zap machinery. See RegisterLogrotateSink.

func RegisterLogrotateSink

func RegisterLogrotateSink(sig ...os.Signal) error

Register logrotate sink in zap sink registry. This sink internally is like file sink, but listens to provided logrotate signal and reopens file when that signal is delivered This can be called only once. Any future calls will result in an error

func RegisterNamedLogrotateSink

func RegisterNamedLogrotateSink(schemeName string, sig ...os.Signal) error

Same as RegisterLogrotateSink, but use provided schemeName instead of default `logrotate` Can be useful in special cases for registering different types of sinks for different signal

Types

This section is empty.

Jump to

Keyboard shortcuts

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