log

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 18 Imported by: 4

Documentation

Index

Constants

View Source
const (
	DefaultFormat = ""
	JSONFormat    = "JSON"
	TextFormat    = "TEXT"
)

Variables

This section is empty.

Functions

func ReopenOnSignal added in v1.2.2

func ReopenOnSignal(logger *Logger, reopener Reopener) func(context.Context) error

ReopenOnSignal returns a function compatible with RunTasks.

Types

type HCLogAdapter

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

HCLogAdapter implements the hclog interface, and wraps it around a Logrus entry

func NewHCLogAdapter added in v0.11.0

func NewHCLogAdapter(log logrus.FieldLogger, name string) *HCLogAdapter

func (*HCLogAdapter) CreateEntry

func (a *HCLogAdapter) CreateEntry(args []any) *logrus.Entry

func (*HCLogAdapter) Debug

func (a *HCLogAdapter) Debug(msg string, args ...any)

func (*HCLogAdapter) Error

func (a *HCLogAdapter) Error(msg string, args ...any)

func (*HCLogAdapter) GetLevel added in v1.5.4

func (a *HCLogAdapter) GetLevel() hclog.Level

func (*HCLogAdapter) ImpliedArgs added in v0.11.0

func (a *HCLogAdapter) ImpliedArgs() []any

ImpliedArgs returns With key/value pairs

func (*HCLogAdapter) Info

func (a *HCLogAdapter) Info(msg string, args ...any)

func (*HCLogAdapter) IsDebug

func (a *HCLogAdapter) IsDebug() bool

func (*HCLogAdapter) IsError

func (a *HCLogAdapter) IsError() bool

func (*HCLogAdapter) IsInfo

func (a *HCLogAdapter) IsInfo() bool

func (*HCLogAdapter) IsTrace

func (a *HCLogAdapter) IsTrace() bool

func (*HCLogAdapter) IsWarn

func (a *HCLogAdapter) IsWarn() bool

func (*HCLogAdapter) Log

func (a *HCLogAdapter) Log(level hclog.Level, msg string, args ...any)

func (*HCLogAdapter) Name

func (a *HCLogAdapter) Name() string

func (*HCLogAdapter) Named

func (a *HCLogAdapter) Named(name string) hclog.Logger

func (*HCLogAdapter) ResetNamed

func (a *HCLogAdapter) ResetNamed(name string) hclog.Logger

func (*HCLogAdapter) SetLevel

func (a *HCLogAdapter) SetLevel(hclog.Level)

func (*HCLogAdapter) StandardLogger

func (a *HCLogAdapter) StandardLogger(*hclog.StandardLoggerOptions) *log.Logger

StandardLogger is meant to return a stdlib Logger type which wraps around hclog. It does this by providing an io.Writer and instantiating a new Logger. It then tries to interpret the log level by parsing the message.

Since we are not using `hclog` in a generic way, and I cannot find any calls to this method from go-plugin, we will poorly support this method. Rather than pull in all of hclog writer parsing logic, pass it a Logrus writer, and hardcode the level to INFO.

Apologies to those who find themselves here.

func (*HCLogAdapter) StandardWriter

func (a *HCLogAdapter) StandardWriter(*hclog.StandardLoggerOptions) io.Writer

func (*HCLogAdapter) Trace

func (*HCLogAdapter) Trace(_ string, _ ...any)

HCLog has one more level than we do. As such, we will never set trace level.

func (*HCLogAdapter) Warn

func (a *HCLogAdapter) Warn(msg string, args ...any)

func (*HCLogAdapter) With

func (a *HCLogAdapter) With(args ...any) hclog.Logger

type LocalTimeHook added in v1.6.0

type LocalTimeHook struct{}

LocalTimeHook is a logrus hook that converts all log fields with type time.Time to local time.

func (LocalTimeHook) Fire added in v1.6.0

func (l LocalTimeHook) Fire(entry *logrus.Entry) error

Fire is called when one of the log levels defined in Levels() is triggered.

func (LocalTimeHook) Levels added in v1.6.0

func (l LocalTimeHook) Levels() []logrus.Level

Levels defines on which log levels this hook would trigger.

type Logger

type Logger struct {
	*logrus.Logger
	io.Closer
}

func NewLogger

func NewLogger(options ...Option) (*Logger, error)

type Option

type Option func(*Logger) error

An Option can change the Logger to apply desired configuration in NewLogger

func WithFormat

func WithFormat(format string) Option

func WithLevel

func WithLevel(logLevel string) Option

func WithOutputFile

func WithOutputFile(file string) Option

WithOutputFile requires lossy copytruncate directive in logrotate.

func WithOutputWriter added in v1.14.0

func WithOutputWriter(w io.Writer) Option

func WithReopenableOutputFile added in v1.2.2

func WithReopenableOutputFile(reopenableFile ReopenableWriteCloser) Option

WithReopenableOutputFile takes either a ReopenableFile, to support handling a signal to rotate log files (e.g. from a logrotate postrotate script), or a RotatableFile, which rotates the file itself.

func WithSourceLocation added in v1.7.1

func WithSourceLocation() Option

type ReopenableFile added in v1.2.2

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

func NewReopenableFile added in v1.2.2

func NewReopenableFile(name string) (*ReopenableFile, error)

func (*ReopenableFile) Close added in v1.2.2

func (r *ReopenableFile) Close() error

func (*ReopenableFile) Name added in v1.2.2

func (r *ReopenableFile) Name() string

Name implements part of os.FileInfo without needing a lock on the underlying file.

func (*ReopenableFile) Reopen added in v1.2.2

func (r *ReopenableFile) Reopen() error

func (*ReopenableFile) Write added in v1.2.2

func (r *ReopenableFile) Write(b []byte) (n int, err error)

type ReopenableWriteCloser added in v1.2.2

type ReopenableWriteCloser interface {
	Reopener
	io.WriteCloser
}

func NewOutputFile added in v1.16.0

func NewOutputFile(name string, cfg *RotationConfig) (ReopenableWriteCloser, error)

NewOutputFile opens the log file, returning a RotatableFile when cfg asks for rotation and a ReopenableFile otherwise. A nil cfg means no rotation.

type Reopener added in v1.2.2

type Reopener interface {
	Reopen() error
}

Reopener inspired by https://github.com/client9/reopen

type RotatableFile added in v1.16.0

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

RotatableFile rotates the log file in process rather than relying on an external tool. An external tool can move the file aside on any platform, but only POSIX has a way to then tell SPIRE to start a new one.

The active file always stays at the configured path; rotation moves the accumulated content aside to a timestamped sibling.

func NewRotatableFile added in v1.16.0

func NewRotatableFile(name string, cfg RotationConfig) (*RotatableFile, error)

NewRotatableFile opens name for appending. It does not rotate on open, even if the file already exceeds MaxSizeMB.

func (*RotatableFile) Close added in v1.16.0

func (r *RotatableFile) Close() error

func (*RotatableFile) Name added in v1.16.0

func (r *RotatableFile) Name() string

Name implements part of os.FileInfo without needing a lock on the underlying file.

func (*RotatableFile) Reopen added in v1.16.0

func (r *RotatableFile) Reopen() error

Reopen forces an immediate rotation. It is what watchLog calls when a reopen is requested.

func (*RotatableFile) TakeRotateError added in v1.16.0

func (r *RotatableFile) TakeRotateError() error

TakeRotateError returns the last rotation failure and clears it. Write cannot report one itself, since logrus hands this writer to plugins as a bare io.Writer and logging from inside it would re-enter logrus while it holds its own lock. Something outside the writer has to drain this.

func (*RotatableFile) Write added in v1.16.0

func (r *RotatableFile) Write(b []byte) (n int, err error)

Write rotates first if this write would take the file past MaxSizeMB. A failed rotation is not returned: logrus hands this writer to plugins as a bare io.Writer, and reporting through the logger would re-enter logrus while it holds its own lock. The write is attempted regardless, so a failure costs an oversized file rather than a lost line. Reopen returns it, and TakeRotateError drains it for the size triggered case.

type RotationConfig added in v1.16.0

type RotationConfig struct {
	// MaxSizeMB is the size the log file may reach before it is rotated.
	// Explicitly 0 disables size based rotation, leaving it to Reopen.
	MaxSizeMB *int `hcl:"max_size_mb"`

	// MaxFiles is how many rotated files to retain, not counting the active
	// one. Explicitly 0 retains all of them.
	MaxFiles *int `hcl:"max_files"`

	UnusedKeyPositions map[string][]token.Pos `hcl:",unusedKeyPositions"`
}

RotationConfig configures rotation and retention. It doubles as the HCL shape of the log_file_rotation block. A key left unset takes a default, so a block with no keys still rotates; an explicit 0 turns that bound off.

func (RotationConfig) SizeRotationDisabled added in v1.16.0

func (c RotationConfig) SizeRotationDisabled() bool

SizeRotationDisabled reports whether nothing will rotate the file on its own, leaving it to an explicit Reopen.

func (RotationConfig) Validate added in v1.16.0

func (c RotationConfig) Validate() error

Jump to

Keyboard shortcuts

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