zlog

package
v0.0.0-alpha.11 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2025 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttrsFromContext

func AttrsFromContext(ctx context.Context) []any

AttrsFromContext returns a new context with given log levels. Use ContextWithAttrs to store log attributes in context. AttrsFromContext returns nil slice if the given ctx is nil or no attributes were found in the context.

func ContextWithAttrs

func ContextWithAttrs(parent context.Context, attrs ...any) context.Context

ContextWithAttrs returns a new context with given attributes. Use AttrsFromContext to extract attributes from the context. ContextWithAttrs uses a new context created with context.Background if the given ctx is nil.

Types

type FileManager

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

FileManager manages files that have the specified format in a directory. It archives file from srcDir to dstDir and manages their life. Use NewFileManager to instantiate FileManager.

FileManager has the following features:

  • maxAge: Remove archived files older than the age.
  • maxHistory: Limit the number of archived files.
  • maxTotalSize: Limit the total size of archived files.
  • gzip: Compress archived files.

func NewFileManager

func NewFileManager(c *FileManagerConfig) (*FileManager, error)

NewFileManager returns a new instance of FileManager.

func (*FileManager) Manage

func (m *FileManager) Manage() error

Manage manages archived files. Manage may take longer time depending on the number of files or size of files to be compressed. Manage is safe for concurrent call.

func (*FileManager) NewFile

func (m *FileManager) NewFile() string

NewFile returns a new file path that can be managed by the FileManager. The FileManager does not create the file returned by the NewFile. Callers should open, create, close or remove the file themselves. Note that calling NewFile increments the internal index that may be used in the file name pattern for the value of '%i'.

type FileManagerConfig

type FileManagerConfig struct {
	// MaxAge is the maximum age to keep the archived files.
	// It does not work when time specifiers are not exit
	// in the Pattern. At least %Y, %M and %D, or %u must be specified.
	// Zero or negative value means no limit.
	MaxAge time.Duration
	// MaxHistory is the maximum number of archived files to keep.
	// Zero or negative value means no limit.
	MaxHistory int
	// MaxTotalBytes is the maximum total byte size to keep archived files.
	// Zero or negative value means no limit.
	MaxTotalBytes int64
	// GzipLv specifies the gzip compression level.
	// If non-zero, gzip compression is applied to archived files,
	// and the ".gz" file extension is added.
	// Valid range is from [compress/gzip.HuffmanOnly] to [compress/gzip.BestCompression].
	// If zero, compression is disabled.
	GzipLv int
	// SrcDir is the directory path where source files are exists.
	// File names should have the pattern specified at the Pattern.
	// Current working directory is used when empty.
	SrcDir string
	// DesDir is the destination directory path
	// to place archived files.
	// File names will have the pattern specified at the Pattern
	// with or without additional ".gz" extension.
	// Current working directory is used when empty.
	DstDir string
	// Pattern is the file name pattern to be manged.
	// If empty, "application.%i.log" is used.
	// Following specifiers can be used in the pattern.
	// A ".%i" will be added when no specifiers are found.
	//	%Y : YYYY 4 digits year. 0 <= YYYY
	//	%M : MM 2 digits month. 1 <= MM <= 12
	//	%D : DD 2 digits day of month. 1 <= DD <= 31
	//	%h : hh 2 digits hour. 0 <= hh <= 23
	//	%m : mm 2 digits minute. 0 <= mm <= 59
	//	%s : ss 2 digits second. 0 <= ss <= 59
	//	%u : unix second with free digits. 0 <= unix
	//	%i : index with free digits. 0 <= index
	//	%H : hostname
	//	%U : user id. "-1" on windows.
	//	%G : user group id. "-1" on windows.
	//	%p : pid (process id)
	//	%P : ppid (parent process id)
	Pattern string
}

FileManagerConfig is the configuration for the FileManager. Use NewFileManager to create a new instance of it.

type Logger

type Logger interface {
	DebugEnabled(ctx context.Context) bool
	InfoEnabled(ctx context.Context) bool
	WarnEnabled(ctx context.Context) bool
	ErrorEnabled(ctx context.Context) bool
	DebugContext(ctx context.Context, msg string, args ...any)
	InfoContext(ctx context.Context, msg string, args ...any)
	WarnContext(ctx context.Context, msg string, args ...any)
	ErrorContext(ctx context.Context, msg string, args ...any)
}

Logger is the basic logger interface.

type LogicalFile

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

LogicalFile is a logical file type. It implements io.Writer interface. Users do not need to care physical file management such as open, close, rename or remove. Use NewLogicalFile to create a new instance of LogicalFile.

func NewLogicalFile

func NewLogicalFile(c *LogicalFileConfig) (*LogicalFile, error)

NewLogicalFile returns a new instance of LogicalFile.

func (*LogicalFile) Close

func (f *LogicalFile) Close() error

Close closes the file. It closes the underlying physical file. Unlike LogicalFile.Swap. it does not create a new file. Close implements io.Closer.Close. Close is safe for concurrent call.

func (*LogicalFile) Swap

func (f *LogicalFile) Swap() error

Swap swaps the active file to new one. FileManager.Manage will be called internally in a new goroutine which means archived files are managed asynchronously. Swap is safe for concurrent call.

func (*LogicalFile) Write

func (f *LogicalFile) Write(b []byte) (int, error)

Write writes the given data in to the file. It will be immediately written to the underlying physical file. Write implements io.Writer.Write. Write is safe for concurrent call.

type LogicalFileConfig

type LogicalFileConfig struct {
	// Manager is the archive files manager config.
	Manager *FileManagerConfig
	// RotateBytes is the maximum physical file size in bytes.
	// Physical file will be rotated when reached to the size.
	// Zero or negative means no limit, or no file rotation.
	RotateBytes int64
	// FileName is the physical file name that is
	// actively written to.
	// If empty, "application.log" is used.
	FileName string
	// OnFallback is called when underlying physical file
	// operation failed. Before calling the function,
	// write target will be replaced to [os.Stderr].
	// If not set, [os.Stderr] will be used until
	// next file rotation occur.
	OnFallback func(error)
}

LogicalFileConfig is the configuration for LogicalFile. Use NewLogicalFile to create a new instance of it.

Directories

Path Synopsis
zslog provides extensional logging features to the standard log/slog package.
zslog provides extensional logging features to the standard log/slog package.

Jump to

Keyboard shortcuts

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