Documentation
¶
Index ¶
- Constants
- func Debugf(format string, a ...any)
- func Errorf(format string, a ...any)
- func Infof(format string, a ...any)
- func Print(s string)
- func PrintError(err error)
- func PrintErrorS(err string)
- func PrintErrorSf(err string, args ...any)
- func Printf(p string, a ...any)
- func SetLogLevel(level Levels)
- func Warnf(format string, a ...any)
- type Flags
- type Levels
- type LogFields
- type Printer
- func (p *Printer) Close() error
- func (p *Printer) Copy() *Printer
- func (p *Printer) Debugf(format string, a ...any)
- func (p *Printer) Errorf(format string, a ...any)
- func (p *Printer) GetLogLevel() Levels
- func (p *Printer) Infof(format string, a ...any)
- func (p *Printer) Print(s string)
- func (p *Printer) Printf(format string, a ...any)
- func (p *Printer) SetLogLevel(level Levels)
- func (p *Printer) SetMaxFieldLength(length int)
- func (p *Printer) SetMaxLogLength(length int)
- func (p *Printer) Warnf(format string, a ...any)
- func (p *Printer) WithField(key string, value any) *Printer
- func (p *Printer) WithFields(fields LogFields) *Printer
- func (p *Printer) WithoutColor() *Printer
- func (p *Printer) WithoutNewLine() *Printer
- func (p *Printer) Write(buffer []byte) (n int, err error)
- func (p *Printer) WriteToErr(b []byte)
- func (p *Printer) WriteToStd(b []byte)
Constants ¶
const ( Reset = iota Bold Faint Underlined = 4 SlowBlink = 5 )
const ( ForegroundBlack = iota + 30 ForegroundRed ForegroundGreen ForegroundYellow ForegroundBlue ForegroundMagenta ForegroundCyan ForegroundWhite )
const ( BackgroundBlack = iota + 40 BackgroundRed BackgroundGreen BackgroundYellow BackgroundBlue BackgroundMagenta BackgroundCyan BackgroundWhite )
const ( DefaultMaxLogLength = 100 DefaultMaxFieldLength = 50 )
Default values for log and field truncation
Variables ¶
This section is empty.
Functions ¶
func Debugf ¶
Debugf logs a formatted debug message using the global printer.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func Errorf ¶
Errorf logs a formatted error message using the global printer.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func Infof ¶
Infof logs a formatted informational message using the global printer.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func Print ¶
func Print(s string)
Print writes a plain string to the standard output stream using the global printer.
Parameters:
- s: string - The message to write.
func PrintError ¶
func PrintError(err error)
PrintError writes an error message to the error output stream using the global printer.
Parameters:
- err: error - The error to write. If nil, "<nil>" is printed.
func PrintErrorS ¶
func PrintErrorS(err string)
PrintErrorS writes a plain error string to the error output stream using the global printer.
Parameters:
- err: string - The error message to write.
func PrintErrorSf ¶
PrintErrorSf formats and writes an error message to the error output stream using the global printer.
Parameters:
- err: string - The format string.
- args: ...any - The arguments to format.
func Printf ¶
Printf formats and writes a message to the standard output stream using the global printer.
Parameters:
- p: string - The format string.
- a: ...any - The arguments to format.
func SetLogLevel ¶
func SetLogLevel(level Levels)
SetLogLevel updates the global printer's log level.
Parameters:
- level: int - The new log level to set.
Types ¶
type Flags ¶
type Flags uint
Flags represents a set of configurable options for the Printer.
This type is defined as an unsigned integer and uses bitwise operations to enable multiple flags to be combined and checked efficiently.
const ( // WithNoFlags is the default value for Flags, indicating no special options are set. WithNoFlags Flags = 0 // FlagWithDate enables the inclusion of the current date in the output. FlagWithDate Flags = 1 << iota // FlagWithGoroutineID enables the inclusion of the current goroutine ID in the output. FlagWithGoroutineID // FlagWithColor enables colored output for better readability. FlagWithColor // FlagPanicOnError enables panic behavior on error conditions. FlagPanicOnError // FlagWithoutNewLine disables the automatic newline at the end of the output. FlagWithoutNewLine // FlagTruncateLogs enables truncation of log messages to a specified length. FlagTruncateLogs // FlagTruncateFields enables truncation of field values to a specified length. FlagTruncateFields )
type Levels ¶
type Levels int
func GetLogLevel ¶
func GetLogLevel() Levels
GetLogLevel retrieves the current log level of the global printer.
Returns:
- int: The current global log level.
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer provides structured output to various I/O streams with support for log levels, colored output, and concurrency-safe operations.
func NewPrinter ¶ added in v2.4.0
func NewPrinter(loglevel Levels, flags Flags, out, err io.WriteCloser) *Printer
NewPrinter creates a new Printer instance with specified log level and I/O streams.
Parameters:
- loglevel: int - The initial logging level.
- out: io.WriteCloser - The output stream for standard messages.
- err: io.WriteCloser - The output stream for error messages.
Returns:
- *Printer: A new Printer instance.
func WithField ¶
WithField adds a single key-value pair to the global printer's context.
Parameters:
- key: string - The key for the field.
- value: any - The value associated with the key.
Returns:
- *Printer: A new Printer instance with the added field.
func WithFields ¶
WithFields adds multiple key-value pairs to the global printer's context.
Parameters:
- fields: LogFields - A map containing the fields to add.
Returns:
- *Printer: A new Printer instance with the added fields.
func WithoutColor ¶ added in v2.4.0
func WithoutColor() *Printer
WithoutColor returns a new Printer instance that disables colored output.
Returns:
- *Printer: A new Printer instance with color disabled.
func WithoutNewLine ¶ added in v2.4.0
func WithoutNewLine() *Printer
WithoutNewLine returns a new Printer instance that disables appending a newline character at the end of the output.
Returns:
- *Printer: A new Printer instance with newline suppression.
func (*Printer) Close ¶
Close safely closes all associated I/O streams of the Printer.
This method iterates over all associated I/O streams (`out` and `err`) and attempts to close them. If any stream fails to close, the method returns the encountered error. If all streams are closed successfully, it returns nil.
Returns:
- error: An error encountered during the close operation, or nil if all streams are closed successfully.
func (*Printer) Copy ¶ added in v2.2.0
Copy creates a new Printer instance with the same configuration as the current one.
Returns:
- *Printer: A new Printer instance with the same configuration.
func (*Printer) Debugf ¶
Debugf logs a debug message if the log level permits.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func (*Printer) Errorf ¶
Errorf logs an error message if the log level permits.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func (*Printer) GetLogLevel ¶
GetLogLevel retrieves the current log level.
Returns:
- int: The current log level.
func (*Printer) Infof ¶
Infof logs an informational message if the log level permits.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func (*Printer) Print ¶ added in v2.3.0
Print writes a raw string to the standard output stream.
Parameters:
- s: string - The string to write.
func (*Printer) Printf ¶ added in v2.3.0
Printf writes a formatted string to the standard output stream.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func (*Printer) SetLogLevel ¶
SetLogLevel updates the log level of the Printer.
Parameters:
- level: int - The new log level to set.
func (*Printer) SetMaxFieldLength ¶ added in v2.6.0
SetMaxFieldLength sets the maximum field length for truncation.
Parameters:
- length: int - The maximum field length to set.
func (*Printer) SetMaxLogLength ¶ added in v2.6.0
SetMaxLogLength sets the maximum log length for truncation.
Parameters:
- length: int - The maximum log length to set.
func (*Printer) Warnf ¶
Warnf logs a warning message if the log level permits.
Parameters:
- format: string - The format string.
- a: ...any - The arguments to format.
func (*Printer) WithField ¶
WithField creates a new Printer instance with an additional single field.
This method performs a deep copy of the current Printer instance and adds the specified key-value pair to the fields map of the new instance.
Parameters:
- key: string - The key for the new field.
- value: any - The value for the new field.
Returns:
- *Printer: A new Printer instance with the added field.
func (*Printer) WithFields ¶
WithFields creates a new Printer instance with additional fields.
This method performs a deep copy of the current Printer instance and adds the specified key-value pairs to the fields map of the new instance.
Parameters:
- fields: LogFields - A map of key-value pairs to add to the fields.
Returns:
- *Printer: A new Printer instance with the added fields.
func (*Printer) WithoutColor ¶ added in v2.4.0
WithoutColor creates a new Printer instance with color output disabled.
Returns:
- *Printer: A new Printer instance with the color flag disabled.
func (*Printer) WithoutNewLine ¶ added in v2.4.0
WithoutNewLine creates a new Printer instance with the newline flag disabled.
This method performs a deep copy of the current Printer instance and sets the WithoutNewLine flag in the new instance.
Returns:
- *Printer: A new Printer instance with the WithoutNewLine flag set.
func (*Printer) Write ¶
Write writes a byte slice to the standard output stream.
Parameters:
- buffer: []byte - The data to write.
Returns:
- int: Number of bytes written.
- error: Error encountered during write.
func (*Printer) WriteToErr ¶
WriteToErr writes a raw message to the error output stream.
Parameters:
- b: []byte - The message to write.
func (*Printer) WriteToStd ¶
WriteToStd writes a raw message to the standard output stream.
Parameters:
- b: []byte - The message to write.