Documentation
¶
Index ¶
- Constants
- func AttrsFromContext(ctx context.Context) []any
- func ContextWithAttrs(parent context.Context, attrs ...any) context.Context
- func ContextWithLevel(parent context.Context, lv Level) context.Context
- type FileManager
- type FileManagerConfig
- type Level
- type Logger
- type LoggerOption
- type LogicalFile
- type LogicalFileConfig
- type Range
- type ZLogger
- func (l *ZLogger) Debug(ctx context.Context, msg string, args ...any)
- func (l *ZLogger) Enabled(ctx context.Context, level Level) bool
- func (l *ZLogger) Error(ctx context.Context, msg string, args ...any)
- func (l *ZLogger) Info(ctx context.Context, msg string, args ...any)
- func (l *ZLogger) Logger() *log.Logger
- func (l *ZLogger) Warn(ctx context.Context, msg string, args ...any)
- func (l *ZLogger) Writer() io.Writer
Examples ¶
Constants ¶
const ( LvUndef = lvMin // UNDEFINED LvTrace = lvTrace1 // TRACE LvDebug = lvDebug1 // DEBUG LvInfo = lvInfo1 // INFO LvWarn = lvWarn1 // WARN LvError = lvError1 // ERROR LvFatal = lvFatal1 // FATAL )
Variables ¶
This section is empty.
Functions ¶
func AttrsFromContext ¶
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 ¶
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.
Example ¶
package main
import (
"context"
"fmt"
"github.com/aileron-projects/go/zlog"
)
func main() {
// Store attributes in the context.
ctx := context.Background()
ctx = zlog.ContextWithAttrs(ctx, "foo", 123)
// Extract attributes from the context.
attrs := zlog.AttrsFromContext(ctx)
fmt.Println(attrs)
}
Output: [foo 123]
func ContextWithLevel ¶
ContextWithLevel returns a new context with given log level. Use LevelFromContext to extract log level from the context. ContextWithLevel uses a new context created with context.Background if the given ctx is nil.
Example ¶
package main
import (
"context"
"fmt"
"github.com/aileron-projects/go/zlog"
)
func main() {
// Store log level in the context.
ctx := context.Background()
ctx = zlog.ContextWithLevel(ctx, zlog.LvError)
// Extract attributes from the context.
lv := zlog.LevelFromContext(ctx)
fmt.Println(lv)
}
Output: ERROR
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 Level ¶
type Level int
Level is the log level type. Level implements fmt.Stringer. Defined levels are:
- LvTrace
- LvDebug
- LvInfo
- LvWarn
- LvError
- LvFatal
Example ¶
package main
import (
"fmt"
"github.com/aileron-projects/go/zlog"
)
func main() {
fmt.Printf("%s %d\n", zlog.LvTrace, zlog.LvTrace)
fmt.Printf("%s %d\n", zlog.LvDebug, zlog.LvDebug)
fmt.Printf("%s %d\n", zlog.LvInfo, zlog.LvInfo)
fmt.Printf("%s %d\n", zlog.LvWarn, zlog.LvWarn)
fmt.Printf("%s %d\n", zlog.LvError, zlog.LvError)
fmt.Printf("%s %d\n", zlog.LvFatal, zlog.LvFatal)
fmt.Printf("%s %d\n", zlog.LvUndef, zlog.LvUndef)
fmt.Printf("%s %d\n", zlog.Level(99), zlog.Level(99))
}
Output: TRACE 1 DEBUG 5 INFO 9 WARN 13 ERROR 17 FATAL 21 UNDEFINED 0 UNDEFINED 99
Example (Compare) ¶
package main
import (
"fmt"
"github.com/aileron-projects/go/zlog"
)
func main() {
fmt.Printf("INFO >= WARN : %v\n", zlog.LvInfo.HigherEqual(zlog.LvWarn))
fmt.Printf("INFO >= INFO : %v\n", zlog.LvInfo.HigherEqual(zlog.LvInfo))
fmt.Printf("INFO >= DEBUG : %v\n", zlog.LvInfo.HigherEqual(zlog.LvDebug))
}
Output: INFO >= WARN : false INFO >= INFO : true INFO >= DEBUG : true
func LevelFromContext ¶
LevelFromContext returns a log level stored in the context. Use ContextWithLevel to store a log level in context. LevelFromContext returns LvUndef if the given ctx is nil of no log levels are found in the context.
func (Level) HigherEqual ¶
HigherEqual returns the result of lv>=target.
func (Level) HigherThan ¶
HigherThan returns the result of lv>target.
type Logger ¶
type Logger interface {
Enabled(ctx context.Context, lv Level) bool
Debug(ctx context.Context, msg string, args ...any)
Info(ctx context.Context, msg string, args ...any)
Warn(ctx context.Context, msg string, args ...any)
Error(ctx context.Context, msg string, args ...any)
}
Logger logs given message and values.
type LoggerOption ¶
type LoggerOption struct {
// Lv is the log level of the logger.
Lv Level
// Flag is the flag for standard logger.
Flag int
}
LoggerOption is the options for ZLogger.
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.
type Range ¶
type Range uint
Range is the log range type. Defined ranges are:
- Undefined
- Trace
- Debug
- Info
- Warn
- Error
- Fatal
Example ¶
package main
import (
"fmt"
"github.com/aileron-projects/go/zlog"
)
func main() {
fmt.Printf("%s %d\n", zlog.Trace, zlog.Trace)
fmt.Printf("%s %d\n", zlog.Debug, zlog.Debug)
fmt.Printf("%s %d\n", zlog.Info, zlog.Info)
fmt.Printf("%s %d\n", zlog.Warn, zlog.Warn)
fmt.Printf("%s %d\n", zlog.Error, zlog.Error)
fmt.Printf("%s %d\n", zlog.Fatal, zlog.Fatal)
fmt.Printf("%s %d\n", zlog.Undefined, zlog.Undefined)
fmt.Printf("%s %d\n", zlog.Range(99), zlog.Range(99))
}
Output: TRACE 2 DEBUG 4 INFO 8 WARN 16 ERROR 32 FATAL 64 UNDEFINED 1 UNDEFINED 99
type ZLogger ¶
type ZLogger struct {
// contains filtered or unexported fields
}
ZLogger is a logger type. ZLogger leverages log.Logger internally. ZLogger implements Logger interface.