log

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 9 Imported by: 5

README

go-utils Logging

The go-utils Logging package provides handy methods for standardized logging.

Installation

To use the log package you must import the package.

import "github.com/eliona-smart-building-assistant/go-utils/log"

Optionally you can define an environment variable named LOG_LEVEL which sets the maximum level to be logged. Not defined the log packages takes the info log level as default.

export LOG_LEVEL=debug # This is optionally, default is info log level

Usage

After installation, you can use the logging like this:

import "github.com/eliona-smart-building-assistant/go-utils/log"

log.Info("start", "This could interest %d of %d people", 3, 4)
log.Debug("start", Usually nobody cares %s", "this")
log.Fatal("validate", "The house is on fire!") // exits with code 1

This produces the following output:

INFO    2022-05-03 15:34:41.770515      start   This could interest 3 of 4 people
DEBUG   2022-05-03 15:34:41.749597      start   Usually nobody cares this
FATAL   2022-05-03 15:34:41.770515      validate   The house is on fire!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BufLimit

func BufLimit() int64

BufLimit returns bufLimit for standard Logger.

func Debug

func Debug(tag, format string, v ...interface{})

Debug calls Printf to print to the standard logger with the debug level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func Error

func Error(tag, format string, v ...interface{})

Error calls Printf to print to the standard logger with the error level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func Fatal

func Fatal(tag, format string, v ...interface{})

Fatal calls Printf to print to the standard logger with the debug level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf. After logging aborting with exit status 1

func GetWriter added in v1.0.9

func GetWriter(level Level, tag string) io.Writer

GetWriter returns an io.Writer implementation which log all data to the standard logger with log level and tag

func Info

func Info(tag, format string, v ...interface{})

Info calls Printf to print to the standard logger with the info level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func Output

func Output(level Level, tag string, s string) error

Output writes the output for a logging event. The string s contains the text to print after the tag specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline.

func Print

func Print(level Level, tag string, v ...interface{})

Print calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(level Level, tag, format string, v ...interface{})

Printf calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(level Level, tag string, v ...interface{})

Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.

func SetBufLimit

func SetBufLimit(bufLimit int64)

SetBufLimit sets bufLimit for standard Logger.

func SetLevel

func SetLevel(level Level)

SetLevel sets logging level for standard Logger.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output destination for the standard logger.

func Timestamp added in v1.0.56

func Timestamp(level Level, timestamp time.Time, tag, format string, v ...interface{})

Timestamp calls Printf to print to the standard logger with the trace level and own timestamp. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func Trace added in v1.0.56

func Trace(tag, format string, v ...interface{})

Trace calls Printf to print to the standard logger with the trace level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func Warn

func Warn(tag, format string, v ...interface{})

Warn calls Printf to print to the standard logger with the warning level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func Writer

func Writer() io.Writer

Writer returns the output destination for the standard logger.

Types

type Level

type Level uint32

Level type

const (
	// FatalLevel level. Fatal errors which make it necessary to abort.
	FatalLevel Level = iota
	// ErrorLevel level. Used for errors that should definitely be noted.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the application.
	InfoLevel
	// DebugLevel level. Output information necessary for debugging. Verbose logging.
	DebugLevel
	// TraceLevel level. Enabled when everything should be outputted. Maximum detailed logging.
	TraceLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger.

func Lev

func Lev() Level

Lev returns logging level for standard Logger.

func (Level) MarshalText

func (level Level) MarshalText() ([]byte, error)

func (Level) String

func (level Level) String() string

Convert the Level to a string. E.g. ErrorLevel becomes "ERROR".

type Logger

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

A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

func New

func New(out io.Writer) *Logger

New creates a new Logger. The out variable sets the destination to which log data will be written. The log level is taken from LOG_LEVEL environment variable.

func (*Logger) BufLimit

func (l *Logger) BufLimit() int64

BufLimit returns current bufLimit value.

func (*Logger) Debug

func (l *Logger) Debug(tag, format string, v ...interface{})

Debug calls Printf to print to the standard logger with the debug level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func (*Logger) Error

func (l *Logger) Error(tag, format string, v ...interface{})

Error calls Printf to print to the standard logger with the error level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func (*Logger) Fatal

func (l *Logger) Fatal(tag, format string, v ...interface{})

Fatal calls Printf to print to the standard logger with the debug level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf. After logging aborting with exit status 1

func (*Logger) GetWriter added in v1.0.9

func (l *Logger) GetWriter(level Level, tag string) io.Writer

GetWriter returns an io.Writer implementation which log all data to the standard logger with log level and tag

func (*Logger) Info

func (l *Logger) Info(tag, format string, v ...interface{})

Info calls Printf to print to the standard logger with the info level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func (*Logger) IsLevelEnabled

func (l *Logger) IsLevelEnabled(level Level) bool

IsLevelEnabled checks if the log level of the logger is greater than the level param

func (*Logger) Level

func (l *Logger) Level() Level

Level returns logging level for Logger.

func (*Logger) Output

func (l *Logger) Output(level Level, tag, s string) error

Output writes the output for a logging event. The string s contains the text to print after the tag specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline. Output won't write more bytes than bufLimit (if it's set)

func (*Logger) Print

func (l *Logger) Print(level Level, tag string, v ...interface{})

Print calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(level Level, tag, format string, v ...interface{})

Printf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Println

func (l *Logger) Println(level Level, tag string, v ...interface{})

Println calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetBufLimit

func (l *Logger) SetBufLimit(bufLimit int64)

SetBufLimit sets bufLimit.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets log level.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets the output destination for the logger.

func (*Logger) Timestamp added in v1.0.56

func (l *Logger) Timestamp(level Level, timestamp time.Time, tag, s string) error

Timestamp writes the output with an own timestamp for a logging event. The string s contains the text to print after the tag specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline. Output won't write more bytes than bufLimit (if it's set)

func (*Logger) TimestampPrintf added in v1.0.56

func (l *Logger) TimestampPrintf(level Level, timestamp time.Time, tag, format string, v ...interface{})

TimestampPrintf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Trace added in v1.0.56

func (l *Logger) Trace(tag, format string, v ...interface{})

Trace calls Printf to print to the standard logger with the trace level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func (*Logger) Warn

func (l *Logger) Warn(tag, format string, v ...interface{})

Warn calls Printf to print to the standard logger with the warning level. As tag the app name is taken. Other arguments are handled in the manner of fmt.Printf.

func (*Logger) Writer

func (l *Logger) Writer() io.Writer

Writer returns the output destination for the logger.

Jump to

Keyboard shortcuts

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