logng

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

README

logng

Go Reference Maintainability Rating Quality Gate Status

logng is a Go (Golang) package that provides structured and leveled logging.

Features

  • Leveled logging: FATAL, ERROR, WARNING, INFO, DEBUG
  • Verbose support
  • Text and JSON output
  • Customizable output
  • Function and file logging
  • Stack trace logging
  • Field support
  • Performance optimized

Installation

You can install logng using the go get command:

go get github.com/goinsane/logng/v2

Examples

Basic example
package main

import (
	"errors"

	"github.com/goinsane/logng/v2"
)

func main() {
	// log by severity and verbosity.
	// default Logger's severity is SeverityInfo.
	// default Logger's verbose is 0.
	logng.Debug("this is debug log. it won't be shown.")
	logng.Info("this is info log.")
	logng.Warning("this is warning log.")
	logng.V(1).Error("this is error log, verbosity 1. it won't be shown.")

	// set stack trace severity
	logng.SetStackTraceSeverity(logng.SeverityWarning)

	// logs with stack trace, because the log severity is warning
	logng.Warningf("a warning: %w", errors.New("unknown"))

	// log with fields
	logger := logng.WithFieldKeyVals("user_name", "john", "ip", "1.2.3.4")
	logger.Infof("connected user:\n%s", "John Smith")

	// logs with fields and stack trace
	logger.Error("an error")
}

2023/11/21 16:18:37 INFO - this is info log.
2023/11/21 16:18:37 WARNING - this is warning log.
2023/11/21 16:18:37 WARNING - a warning: unknown
    
	main.main(0x104d6f7b0)
		main.go:22 +0x150
	runtime.main(0x104d0a2e0)
		proc.go:250 +0x247
	runtime.goexit(0x104d35390)
		asm_arm64.s:1172 +0x3
    
2023/11/21 16:18:37 INFO - connected user:
                           John Smith
    
	+ "user_name"="john" "ip"="1.2.3.4"
    
2023/11/21 16:18:37 ERROR - an error
    
	+ "user_name"="john" "ip"="1.2.3.4"
    
	main.main(0x104d6f7b0)
		main.go:29 +0x248
	runtime.main(0x104d0a2e0)
		proc.go:250 +0x247
	runtime.goexit(0x104d35390)
		asm_arm64.s:1172 +0x3
    
More examples

To run any example, please use the command like the following:

cd examples/example1/
go run *.go

Tests

To run all tests, please use the following command:

go test -v

To run all examples, please use the following command:

go test -v -run=^Example

To run all benchmarks, please use the following command:

go test -v -run=^Benchmark -bench=.

Contributing

We welcome contributions from the community to improve and expand project capabilities. If you find a bug, have a feature request, or want to contribute code, please follow our guidelines for contributing (CONTRIBUTING.md) and submit a pull request.

License

This project is licensed under the BSD 3-Clause License.

Documentation

Overview

Package logng provides structured and leveled logging.

Example
// reset logng for previous changes.
logng.Reset()
// change writer of default output to stdout from stderr.
logng.SetTextOutputWriter(os.Stdout)

// log by severity and verbosity.
// default Logger's severity is SeverityInfo.
// default Logger's verbose is 0.
logng.Debug("this is debug log. it won't be shown.")
logng.Info("this is info log.")
logng.Warning("this is warning log.")
logng.V(1).Error("this is error log, verbosity 1. it won't be shown.")

// SetSeverity()
logng.SetSeverity(logng.SeverityDebug)
logng.Debug("this is debug log. it will now be shown.")

// SetVerbose() and V()
logng.SetVerbose(1)
logng.V(1).Error("this is error log, verbosity 1. it will now be shown.")
logng.V(2).Warning("this is warning log, verbosity 2. it won't be shown.")

// SetTextOutputFlags()
// default flags is FlagDefault.
logng.SetTextOutputFlags(logng.TextOutputFlagDefault | logng.TextOutputFlagShortFile)
logng.Info("this is info log. you can see file name and line in this log.")

// log using Print.
// default print severity is SeverityInfo.
logng.Print("this log will be shown as info log.")

// SetPrintSeverity()
logng.SetPrintSeverity(logng.SeverityWarning)
logng.Print("this log will now be shown as warning log.")

// SetStackTraceSeverity()
// default stack trace severity is none.
logng.SetStackTraceSeverity(logng.SeverityWarning)
logng.Warning("this is warning log. you can see stack trace end of this log.")
logng.Error("this is error log. you can still see stack trace end of this log.")
logng.Info("this is info log. stack trace won't be shown end of this log.")

// WithTime()
logng.WithTime(testTime).Info("this is info log with custom time.")

// WithPrefix()
logng.WithPrefix("prefix1").Warning("this is warning log with prefix 'prefix1'.")
logng.WithPrefix("prefix1").WithPrefix("prefix2").Error("this is error log with both of prefixes 'prefix1' and 'prefix2'.")

// WithFieldKeyVals()
logng.WithFieldKeyVals("key1", "val1", "key2", "val2", "key3", "val3", "key1", "val1-2", "key2", "val2-2").Info("this is info log with several fields.")

// SetTextOutputFlags()
// default flags is TextOutputFlagDefault.
logng.SetTextOutputFlags(logng.TextOutputFlagDefault | logng.TextOutputFlagShortFile)
logng.Info("this is info log. you can see file name and line in this log.")

// multi-line logs
logng.Info("this is\nmulti-line log with file name")
logng.Info("this is\nmulti-line log")
logng.WithFieldKeyVals("key1", "val1").Info("this is\nmulti-line log with key vals")
Output:

Example (Test1)
// reset logng for previous changes.
logng.Reset()
// change writer of default output to stdout from stderr.
logng.SetTextOutputWriter(os.Stdout)
// just show severity.
logng.SetTextOutputFlags(logng.TextOutputFlagSeverity)

logng.Debug("this is debug log, verbosity 0. it will not be shown.")
logng.Info("this is info log, verbosity 0.")
logng.Warning("this is warning log, verbosity 0.")
logng.Error("this is error log, verbosity 0.")
logng.Print("this is info log, verbosity 0 caused by Print().")
logng.V(1).Info("this is info log, verbosity 1. it will not be shown.")

logng.SetSeverity(logng.SeverityDebug)
logng.Debug("this is debug log, verbosity 0.")

logng.SetVerbose(1)
logng.V(0).Info("this is info log, verbosity 0.")
logng.V(1).Info("this is info log, verbosity 1.")
logng.V(2).Info("this is info log, verbosity 2. it will not be shown.")

logng.SetPrintSeverity(logng.SeverityWarning)
logng.Print("this is warning log, verbosity 0 caused by Print().")

logng.Warning("this is warning log, verbosity 0.\nwithout padding.")
logng.SetTextOutputFlags(logng.TextOutputFlagSeverity | logng.TextOutputFlagPadding)
logng.Warning("this is warning log, verbosity 0.\nwith padding.")

logng.SetTextOutputFlags(logng.TextOutputFlagDefault)
tm, _ := time.ParseInLocation("2006-01-02T15:04:05", "2019-11-13T21:56:24", time.Local)
logng.WithTime(tm).Info("this is info log, verbosity 0.")
Output:

INFO - this is info log, verbosity 0.
WARNING - this is warning log, verbosity 0.
ERROR - this is error log, verbosity 0.
INFO - this is info log, verbosity 0 caused by Print().
DEBUG - this is debug log, verbosity 0.
INFO - this is info log, verbosity 0.
INFO - this is info log, verbosity 1.
WARNING - this is warning log, verbosity 0 caused by Print().
WARNING - this is warning log, verbosity 0.
without padding.
WARNING - this is warning log, verbosity 0.
          with padding.
2019/11/13 21:56:24 INFO - this is info log, verbosity 0.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSeverity = errors.New("invalid severity")
	ErrUnknownSeverity = errors.New("unknown severity")
)

Functions

func Debug

func Debug(args ...interface{})

Debug logs to the DEBUG severity logs to the default Logger.

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs to the DEBUG severity logs to the default Logger.

func Debugln

func Debugln(args ...interface{})

Debugln logs to the DEBUG severity logs to the default Logger.

func Error

func Error(args ...interface{})

Error logs to the ERROR severity logs to the default Logger.

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs to the ERROR severity logs to the default Logger.

func Errorln

func Errorln(args ...interface{})

Errorln logs to the ERROR severity logs to the default Logger.

func Fatal

func Fatal(args ...interface{})

Fatal logs to the FATAL severity logs to the default Logger, then calls os.Exit(1).

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs to the FATAL severity logs to the default Logger, then calls os.Exit(1).

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs to the FATAL severity logs to the default Logger, then calls os.Exit(1).

func Info

func Info(args ...interface{})

Info logs to the INFO severity logs to the default Logger.

func Infof

func Infof(format string, args ...interface{})

Infof logs to the INFO severity logs to the default Logger.

func Infoln

func Infoln(args ...interface{})

Infoln logs to the INFO severity logs to the default Logger.

func Print

func Print(args ...interface{})

Print logs a log which has the default Logger's print severity to the default Logger.

func Printf

func Printf(format string, args ...interface{})

Printf logs a log which has the default Logger's print severity to the default Logger.

func Println

func Println(args ...interface{})

Println logs a log which has the default Logger's print severity to the default Logger.

func Reset

func Reset()

Reset resets the default Logger and the default TextOutput.

func Warning

func Warning(args ...interface{})

Warning logs to the WARNING severity logs to the default Logger.

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs to the WARNING severity logs to the default Logger.

func Warningln

func Warningln(args ...interface{})

Warningln logs to the WARNING severity logs to the default Logger.

Types

type Field

type Field struct {
	Key   string
	Value interface{}
}

Field is the type of field.

type Fields

type Fields []Field

Fields is the slice of fields.

func (Fields) Clone

func (f Fields) Clone() Fields

Clone clones the underlying Fields.

type JSONOutput

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

JSONOutput is an implementation of Output by writing json to io.Writer w.

func NewJSONOutput

func NewJSONOutput(w io.Writer, flags JSONOutputFlag) *JSONOutput

NewJSONOutput creates a new JSONOutput.

func (*JSONOutput) Log

func (o *JSONOutput) Log(log *Log)

Log is the implementation of Output.

func (*JSONOutput) SetFlags

func (o *JSONOutput) SetFlags(flags JSONOutputFlag) *JSONOutput

SetFlags sets flags to override every single Log.Flags if the argument flags different from 0. It returns the underlying JSONOutput.

func (*JSONOutput) SetOnError

func (o *JSONOutput) SetOnError(f func(error)) *JSONOutput

SetOnError sets a function to call when error occurs. It returns the underlying JSONOutput.

func (*JSONOutput) SetTimeLayout

func (o *JSONOutput) SetTimeLayout(timeLayout string) *JSONOutput

SetTimeLayout sets a time layout to format time field. It returns the underlying JSONOutput.

func (*JSONOutput) SetWriter

func (o *JSONOutput) SetWriter(w io.Writer) *JSONOutput

SetWriter sets writer. It returns the underlying JSONOutput.

type JSONOutputFlag

type JSONOutputFlag int

JSONOutputFlag holds single or multiple flags of JSONOutput. A JSONOutput instance uses these flags which are stored by JSONOutputFlag type.

const (
	// JSONOutputFlagSeverity prints the string value of severity into severity field.
	JSONOutputFlagSeverity JSONOutputFlag = 1 << iota

	// JSONOutputFlagTime prints the time in the given time zone into time field.
	JSONOutputFlagTime

	// JSONOutputFlagLocalTZ uses the local time zone rather than the given time zone if JSONOutputFlagTime is set.
	JSONOutputFlagLocalTZ

	// JSONOutputFlagUTC uses UTC rather than the given or local time zone if JSONOutputFlagTime is set.
	JSONOutputFlagUTC

	// JSONOutputFlagTimestamp prints the unix timestamp into timestamp field.
	JSONOutputFlagTimestamp

	// JSONOutputFlagTimestampMicro prints the unix timestamp with microsecond resolution into timestamp field.
	// assumes JSONOutputFlagTimestamp.
	JSONOutputFlagTimestampMicro

	// JSONOutputFlagSeverityLevel prints the numeric value of severity into severity_level field.
	JSONOutputFlagSeverityLevel

	// JSONOutputFlagVerbosity prints verbosity field.
	JSONOutputFlagVerbosity

	// JSONOutputFlagLongFunc prints full package name and function name into func field : a/b/c/d.Func1().
	JSONOutputFlagLongFunc

	// JSONOutputFlagShortFunc prints final package name and function name into func field: d.Func1().
	// overrides JSONOutputFlagLongFunc.
	JSONOutputFlagShortFunc

	// JSONOutputFlagLongFile prints full file name and line number into file field: a/b/c/d.go:23.
	JSONOutputFlagLongFile

	// JSONOutputFlagShortFile prints final file name element and line number into file field: d.go:23.
	// overrides JSONOutputFlagLongFile.
	JSONOutputFlagShortFile

	// JSONOutputFlagStackTrace prints stack_trace field if the stack trace is given.
	JSONOutputFlagStackTrace

	// JSONOutputFlagStackTraceShortFile prints with file name element only.
	// assumes JSONOutputFlagStackTrace.
	JSONOutputFlagStackTraceShortFile

	// JSONOutputFlagFields prints additional fields if given.
	JSONOutputFlagFields

	// JSONOutputFlagDefault holds predefined default flags.
	JSONOutputFlagDefault = JSONOutputFlagSeverity | JSONOutputFlagTime | JSONOutputFlagLocalTZ |
		JSONOutputFlagLongFunc | JSONOutputFlagShortFile | JSONOutputFlagStackTraceShortFile | JSONOutputFlagFields
)

type Log

type Log struct {
	Message     []byte
	Error       error
	Severity    Severity
	Verbosity   Verbose
	Time        time.Time
	Fields      Fields
	StackCaller StackCaller
	StackTrace  *StackTrace
}

Log carries the log.

func (*Log) Clone

func (l *Log) Clone() *Log

Clone clones the underlying Log.

type Logger

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

Logger provides a logger for leveled and structured logging.

Example
logger := logng.NewLogger(logng.NewTextOutput(os.Stdout, logng.TextOutputFlagSeverity),
	logng.SeverityInfo, 2)

logger.Debug("this is debug log, verbosity 0. it won't be shown.")
logger.Info("this is info log, verbosity 0.")
logger.V(0).Debug("this is debug log, verbosity 0. it won't be shown.")
logger.V(1).Info("this is info log, verbosity 1.")
logger.V(2).Warning("this is warning log, verbosity 2.")
logger.V(3).Error("this is error log, verbosity 3. it won't be shown.")
Output:

INFO - this is info log, verbosity 0.
INFO - this is info log, verbosity 1.
WARNING - this is warning log, verbosity 2.

func Clone

func Clone() *Logger

Clone clones the default Logger.

func DefaultLogger

func DefaultLogger() *Logger

DefaultLogger returns the default Logger.

func NewLogger

func NewLogger(output Output, severity Severity, verbose Verbose) *Logger

NewLogger creates a new Logger. If severity is invalid, it sets SeverityInfo.

func SetOutput

func SetOutput(output Output) *Logger

SetOutput sets the default Logger's output. It returns the default Logger. By default, the default TextOutput.

func SetPrintSeverity

func SetPrintSeverity(printSeverity Severity) *Logger

SetPrintSeverity sets the default Logger's severity level which is using with Print methods. If printSeverity is invalid, it sets SeverityInfo. It returns the default Logger. By default, SeverityInfo.

Example
// set logng for this example.
logng.Reset()
logng.SetTextOutputWriter(os.Stdout)
logng.SetTextOutputFlags(logng.TextOutputFlagSeverity)

logng.SetPrintSeverity(logng.SeverityWarning)
logng.Print("this is the log.")
Output:

WARNING - this is the log.

func SetSeverity

func SetSeverity(severity Severity) *Logger

SetSeverity sets the default Logger's severity. If severity is invalid, it sets SeverityInfo. It returns the default Logger. By default, SeverityInfo.

Example
// set logng for this example.
logng.Reset()
logng.SetTextOutputWriter(os.Stdout)
logng.SetTextOutputFlags(logng.TextOutputFlagSeverity)

logng.SetSeverity(logng.SeverityWarning)
logng.Debug("this is debug log. it won't be shown.")
logng.Info("this is info log. it won't be shown.")
logng.Warning("this is warning log.")
logng.Error("this is error log.")
Output:

WARNING - this is warning log.
ERROR - this is error log.

func SetStackTraceSeverity

func SetStackTraceSeverity(stackTraceSeverity Severity) *Logger

SetStackTraceSeverity sets the default Logger's severity level which saves stack trace into Log. If stackTraceSeverity is invalid, it sets SeverityNone. It returns the default Logger. By default, SeverityNone.

func SetStackTraceSize

func SetStackTraceSize(stackTraceSize int) *Logger

SetStackTraceSize sets the maximum program counter size of the stack trace for the default Logger. If stackTraceSize is out of range, it sets 64. The range is 1 to 16384 each included. It returns the default Logger. By default, 64.

func SetVerbose

func SetVerbose(verbose Verbose) *Logger

SetVerbose sets the default Logger's verbose. It returns the default Logger. By default, 0.

Example
// set logng for this example.
logng.Reset()
logng.SetTextOutputWriter(os.Stdout)
logng.SetTextOutputFlags(logng.TextOutputFlagSeverity)

logng.SetVerbose(1)
logng.V(0).Info("this is info log, verbosity 0.")
logng.V(1).Info("this is info log, verbosity 1.")
logng.V(2).Info("this is info log, verbosity 2. it won't be shown.")
logng.V(3).Info("this is info log, verbosity 3. it won't be shown.")
Output:

INFO - this is info log, verbosity 0.
INFO - this is info log, verbosity 1.

func V

func V(verbosity Verbose) *Logger

V clones the default Logger with the given verbosity if the default Logger's verbose is greater or equal to the given verbosity, otherwise returns nil.

func WithCtxErrVerbosity

func WithCtxErrVerbosity(verbosity Verbose) *Logger

WithCtxErrVerbosity clones the default Logger with context error verbosity. If the log has an error and the error is an context error, the given value is used as verbosity.

func WithFieldKeyVals

func WithFieldKeyVals(kvs ...interface{}) *Logger

WithFieldKeyVals clones the default Logger with given keys and values of Field.

func WithFieldMap

func WithFieldMap(fieldMap map[string]interface{}) *Logger

WithFieldMap clones the default Logger with the given field map.

func WithFields

func WithFields(fields ...Field) *Logger

WithFields clones the default Logger with given fields.

func WithPrefix

func WithPrefix(args ...interface{}) *Logger

WithPrefix clones the default Logger and adds the given prefix to the end of the underlying prefix.

Example
// set logng for this example.
logng.Reset()
logng.SetTextOutputWriter(os.Stdout)
logng.SetTextOutputFlags(logng.TextOutputFlagSeverity)

logng.WithPrefix("prefix1: ").WithPrefix("prefix2: ").Info("this is info log.")
Output:

INFO - prefix1: prefix2: this is info log.

func WithPrefixf

func WithPrefixf(format string, args ...interface{}) *Logger

WithPrefixf clones the default Logger and adds the given prefix to the end of the underlying prefix.

func WithSuffix

func WithSuffix(args ...interface{}) *Logger

WithSuffix clones the default Logger and adds the given suffix to the beginning of the underlying suffix.

Example
// set logng for this example.
logng.Reset()
logng.SetTextOutputWriter(os.Stdout)
logng.SetTextOutputFlags(logng.TextOutputFlagSeverity)

logng.WithSuffix(" :suffix1").WithSuffix(" :suffix2").Info("this is info log.")
Output:

INFO - this is info log. :suffix2 :suffix1

func WithSuffixf

func WithSuffixf(format string, args ...interface{}) *Logger

WithSuffixf clones the default Logger and adds the given suffix to the beginning of the underlying suffix.

func WithTime

func WithTime(tm time.Time) *Logger

WithTime clones the default Logger with the given time.

Example
// set logng for this example.
logng.Reset()
logng.SetTextOutputWriter(os.Stdout)
logng.SetTextOutputFlags(logng.TextOutputFlagDefault)

logng.WithTime(testTime).Info("this is info log with the given time.")
Output:

2010/11/12 13:14:15 INFO - this is info log with the given time.

func WithVerbosity

func WithVerbosity(verbosity Verbose) *Logger

WithVerbosity clones the default Logger with the given verbosity.

func WithoutTime

func WithoutTime() *Logger

WithoutTime clones the default Logger without time.

func (*Logger) Clone

func (l *Logger) Clone() *Logger

Clone clones the underlying Logger.

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

Debug logs to the DEBUG severity logs.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

Debugf logs to the DEBUG severity logs.

func (*Logger) Debugln

func (l *Logger) Debugln(args ...interface{})

Debugln logs to the DEBUG severity logs.

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

Error logs to the ERROR severity logs.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

Errorf logs to the ERROR severity logs.

func (*Logger) Errorln

func (l *Logger) Errorln(args ...interface{})

Errorln logs to the ERROR severity logs.

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

Fatal logs to the FATAL severity logs, then calls os.Exit(1).

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

Fatalf logs to the FATAL severity logs, then calls os.Exit(1).

func (*Logger) Fatalln

func (l *Logger) Fatalln(args ...interface{})

Fatalln logs to the FATAL severity logs, then calls os.Exit(1).

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

Info logs to the INFO severity logs.

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

Infof logs to the INFO severity logs.

func (*Logger) Infoln

func (l *Logger) Infoln(args ...interface{})

Infoln logs to the INFO severity logs.

func (*Logger) Print

func (l *Logger) Print(args ...interface{})

Print logs a log which has the underlying Logger's print severity.

func (*Logger) Printf

func (l *Logger) Printf(format string, args ...interface{})

Printf logs a log which has the underlying Logger's print severity.

func (*Logger) Println

func (l *Logger) Println(args ...interface{})

Println logs a log which has the underlying Logger's print severity.

func (*Logger) SetOutput

func (l *Logger) SetOutput(output Output) *Logger

SetOutput sets the underlying Logger's output. It returns the underlying Logger.

func (*Logger) SetPrintSeverity

func (l *Logger) SetPrintSeverity(printSeverity Severity) *Logger

SetPrintSeverity sets the underlying Logger's severity level which is using with Print methods. If printSeverity is invalid or, less or equal than SeverityFatal; it sets SeverityInfo. It returns the underlying Logger. By default, SeverityInfo.

func (*Logger) SetSeverity

func (l *Logger) SetSeverity(severity Severity) *Logger

SetSeverity sets the underlying Logger's severity. If severity is invalid, it sets SeverityInfo. It returns the underlying Logger.

func (*Logger) SetStackTraceSeverity

func (l *Logger) SetStackTraceSeverity(stackTraceSeverity Severity) *Logger

SetStackTraceSeverity sets the underlying Logger's severity level which saves stack trace into Log. If stackTraceSeverity is invalid, it sets SeverityNone. It returns the underlying Logger. By default, SeverityNone.

func (*Logger) SetStackTraceSize

func (l *Logger) SetStackTraceSize(stackTraceSize int) *Logger

SetStackTraceSize sets the maximum program counter size of the stack trace for the underlying Logger. If stackTraceSize is out of range, it sets 64. The range is 1 to 16384 each included. It returns the underlying Logger. By default, 64.

func (*Logger) SetVerbose

func (l *Logger) SetVerbose(verbose Verbose) *Logger

SetVerbose sets the underlying Logger's verbose. It returns the underlying Logger.

func (*Logger) V

func (l *Logger) V(verbosity Verbose) *Logger

V clones the underlying Logger with the given verbosity if the underlying Logger's verbose is greater or equal to the given verbosity, otherwise returns nil.

func (*Logger) Warning

func (l *Logger) Warning(args ...interface{})

Warning logs to the WARNING severity logs.

func (*Logger) Warningf

func (l *Logger) Warningf(format string, args ...interface{})

Warningf logs to the WARNING severity logs.

func (*Logger) Warningln

func (l *Logger) Warningln(args ...interface{})

Warningln logs to the WARNING severity logs.

func (*Logger) WithCtxErrVerbosity

func (l *Logger) WithCtxErrVerbosity(verbosity Verbose) *Logger

WithCtxErrVerbosity clones the underlying Logger with context error verbosity. If the log has an error and the error is an context error, the given value is used as verbosity.

func (*Logger) WithFieldKeyVals

func (l *Logger) WithFieldKeyVals(kvs ...interface{}) *Logger

WithFieldKeyVals clones the underlying Logger with given keys and values of Field.

func (*Logger) WithFieldMap

func (l *Logger) WithFieldMap(fieldMap map[string]interface{}) *Logger

WithFieldMap clones the underlying Logger with the given field map.

func (*Logger) WithFields

func (l *Logger) WithFields(fields ...Field) *Logger

WithFields clones the underlying Logger with given fields.

func (*Logger) WithPrefix

func (l *Logger) WithPrefix(args ...interface{}) *Logger

WithPrefix clones the underlying Logger and adds the given prefix to the end of the underlying prefix.

func (*Logger) WithPrefixf

func (l *Logger) WithPrefixf(format string, args ...interface{}) *Logger

WithPrefixf clones the underlying Logger and adds the given prefix to the end of the underlying prefix.

func (*Logger) WithSuffix

func (l *Logger) WithSuffix(args ...interface{}) *Logger

WithSuffix clones the underlying Logger and adds the given suffix to the beginning of the underlying suffix.

func (*Logger) WithSuffixf

func (l *Logger) WithSuffixf(format string, args ...interface{}) *Logger

WithSuffixf clones the underlying Logger and adds the given suffix to the beginning of the underlying suffix.

func (*Logger) WithTime

func (l *Logger) WithTime(tm time.Time) *Logger

WithTime clones the underlying Logger with the given time.

func (*Logger) WithVerbosity

func (l *Logger) WithVerbosity(verbosity Verbose) *Logger

WithVerbosity clones the underlying Logger with the given verbosity.

func (*Logger) WithoutTime

func (l *Logger) WithoutTime() *Logger

WithoutTime clones the underlying Logger without time.

type Output

type Output interface {
	Log(log *Log)
}

Output is an interface for Logger output. All of Output implementations must be safe for concurrency.

func MultiOutput

func MultiOutput(outputs ...Output) Output

MultiOutput creates an output that clones its logs to all the provided outputs.

type QueuedOutput

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

QueuedOutput is intermediate Output implementation between Logger and given Output. QueuedOutput has queueing for unblocking Log() method.

func NewQueuedOutput

func NewQueuedOutput(output Output, queueLen int) (o *QueuedOutput)

NewQueuedOutput creates a new QueuedOutput by the given output.

func (*QueuedOutput) Close

func (o *QueuedOutput) Close() error

Close stops accepting new logs to the underlying QueuedOutput and waits for the queue to empty. Unused QueuedOutput must be closed for freeing resources.

func (*QueuedOutput) Log

func (o *QueuedOutput) Log(log *Log)

Log is the implementation of Output. If blocking is true, Log method blocks execution until the underlying output has finished execution. Otherwise, Log method sends the log to the queue if the queue is available. When the queue is full, it tries to call OnQueueFull function.

func (*QueuedOutput) SetBlocking

func (o *QueuedOutput) SetBlocking(blocking bool) *QueuedOutput

SetBlocking sets QueuedOutput behavior when the queue is full. It returns the underlying QueuedOutput.

func (*QueuedOutput) SetOnQueueFull

func (o *QueuedOutput) SetOnQueueFull(f func()) *QueuedOutput

SetOnQueueFull sets a function to call when the queue is full. It returns the underlying QueuedOutput.

type Severity

type Severity int

Severity describes the severity level of Log.

const (
	// SeverityNone is none or unspecified severity level.
	SeverityNone Severity = iota

	// SeverityFatal is the fatal severity level.
	SeverityFatal

	// SeverityError is the error severity level.
	SeverityError

	// SeverityWarning is the warning severity level.
	SeverityWarning

	// SeverityInfo is the info severity level.
	SeverityInfo

	// SeverityDebug is the debug severity level.
	SeverityDebug
)

func (Severity) CheckValid

func (s Severity) CheckValid() error

CheckValid returns ErrInvalidSeverity for invalid s.

func (Severity) IsValid

func (s Severity) IsValid() bool

IsValid returns whether s is valid.

func (Severity) MarshalText

func (s Severity) MarshalText() (text []byte, err error)

MarshalText is the implementation of encoding.TextMarshaler. If s is invalid, it returns the error from Severity.CheckValid.

func (Severity) String

func (s Severity) String() string

String is the implementation of fmt.Stringer.

func (*Severity) UnmarshalText

func (s *Severity) UnmarshalText(text []byte) error

UnmarshalText is the implementation of encoding.TextUnmarshaler. If text is unknown, it returns ErrUnknownSeverity.

type StackCaller

type StackCaller struct {
	runtime.Frame
}

StackCaller stores the information of the stack caller. StackCaller can format the given information as string by using String or Format methods.

func (StackCaller) Format

func (c StackCaller) Format(f fmt.State, verb rune)

Format is the implementation of fmt.Formatter.

For '%s' (also '%v'):

%s       just show function and entry without padding and indent.
%+s      show file path, line and program counter. padding char '\t', default padding 0, default indent 1.
% s      show file path, line and program counter. padding char ' ', default padding 0, default indent 2.
%+ s     exact with '% s'.
%#s      same with '%+s', use file name as file path.
%+#s     exact with '%#s'.
% #s     same with '% s', use file name as file path.
%+4s     same with '%+s', padding 4, indent 1 by default.
%+.3s    same with '%+s', padding 0 by default, indent 3.
%+4.3s   same with '%+s', padding 4, indent 3.
%+4.s    same with '%+s', padding 4, indent 0.
% 4s     same with '% s', padding 4, indent 2 by default.
% .3s    same with '% s', padding 0 by default, indent 3.
% 4.3s   same with '% s', padding 4, indent 3.
% 4.s    same with '% s', padding 4, indent 0.
%#4.3s   same with '%#s', padding 4, indent 3.
% #4.3s  same with '% #s', padding 4, indent 3.

func (StackCaller) String

func (c StackCaller) String() string

String is the implementation of fmt.Stringer. It is synonym with fmt.Sprintf("%s", c).

type StackTrace

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

StackTrace stores the information of the stack trace.

func CurrentStackTrace

func CurrentStackTrace(size, skip int) *StackTrace

CurrentStackTrace creates a new StackTrace at this point. size limits maximum program counter size. It uses runtime.Callers and uses skip argument as is. So you should increase skip argument by 1.

func NewStackTrace

func NewStackTrace(programCounters []uintptr) *StackTrace

NewStackTrace creates a new StackTrace from program counters.

func (*StackTrace) Caller

func (t *StackTrace) Caller(index int) StackCaller

Caller returns a caller on the given index. It panics if index is out of range.

func (*StackTrace) Callers

func (t *StackTrace) Callers() []StackCaller

Callers returns StackCaller's.

func (*StackTrace) Clone

func (t *StackTrace) Clone() *StackTrace

Clone clones the underlying StackTrace.

func (*StackTrace) Format

func (t *StackTrace) Format(f fmt.State, verb rune)

Format is the implementation of fmt.Formatter. Format lists all StackCaller's in the underlying StackTrace line by line with the given format.

func (*StackTrace) ProgramCounter

func (t *StackTrace) ProgramCounter(index int) uintptr

ProgramCounter returns a program counter on the given index. It panics if index is out of range.

func (*StackTrace) ProgramCounters

func (t *StackTrace) ProgramCounters() []uintptr

ProgramCounters returns program counters.

func (*StackTrace) SizeOfCallers

func (t *StackTrace) SizeOfCallers() int

SizeOfCallers returns the size of StackCaller's.

func (*StackTrace) SizeOfProgramCounters

func (t *StackTrace) SizeOfProgramCounters() int

SizeOfProgramCounters returns the size of program counters.

func (*StackTrace) String

func (t *StackTrace) String() string

String is the implementation of fmt.Stringer. It is synonym with fmt.Sprintf("%s", t).

type TextOutput

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

TextOutput is an implementation of Output by writing texts to io.Writer w.

func DefaultTextOutput

func DefaultTextOutput() *TextOutput

DefaultTextOutput returns the default TextOutput.

func NewTextOutput

func NewTextOutput(w io.Writer, flags TextOutputFlag) *TextOutput

NewTextOutput creates a new TextOutput.

func SetTextOutputFlags

func SetTextOutputFlags(flags TextOutputFlag) *TextOutput

SetTextOutputFlags sets the default TextOutput's flags. It returns the default TextOutput. By default, TextOutputFlagDefault.

func SetTextOutputWriter

func SetTextOutputWriter(w io.Writer) *TextOutput

SetTextOutputWriter sets the default TextOutput's writer. It returns the default TextOutput. By default, os.Stderr.

func (*TextOutput) Log

func (o *TextOutput) Log(log *Log)

Log is the implementation of Output.

func (*TextOutput) SetFlags

func (o *TextOutput) SetFlags(flags TextOutputFlag) *TextOutput

SetFlags sets flags to override every single Log.Flags if argument flags is different from 0. It returns the underlying TextOutput.

func (*TextOutput) SetOnError

func (o *TextOutput) SetOnError(f func(error)) *TextOutput

SetOnError sets a function to call when error occurs. It returns the underlying TextOutput.

func (*TextOutput) SetWriter

func (o *TextOutput) SetWriter(w io.Writer) *TextOutput

SetWriter sets writer. It returns the underlying TextOutput.

type TextOutputFlag

type TextOutputFlag int

TextOutputFlag holds single or multiple flags of TextOutput. A TextOutput instance uses these flags which are stored by TextOutputFlag type.

const (
	// TextOutputFlagDate prints the date in the local time zone: 2009/01/23.
	TextOutputFlagDate TextOutputFlag = 1 << iota

	// TextOutputFlagTime prints the time in the local time zone: 01:23:23.
	TextOutputFlagTime

	// TextOutputFlagMicroseconds prints microsecond resolution: 01:23:23.123123.
	// assumes TextOutputFlagTime.
	TextOutputFlagMicroseconds

	// TextOutputFlagUTC uses UTC rather than the local time zone if TextOutputFlagDate or TextOutputFlagTime is set.
	TextOutputFlagUTC

	// TextOutputFlagSeverity prints the severity.
	TextOutputFlagSeverity

	// TextOutputFlagPadding prints padding with multiple lines.
	TextOutputFlagPadding

	// TextOutputFlagLongFunc prints full package name and function name: a/b/c/d.Func1().
	TextOutputFlagLongFunc

	// TextOutputFlagShortFunc prints final package name and function name: d.Func1().
	// overrides TextOutputFlagLongFunc.
	TextOutputFlagShortFunc

	// TextOutputFlagLongFile prints full file name and line number: a/b/c/d.go:23.
	TextOutputFlagLongFile

	// TextOutputFlagShortFile prints final file name element and line number: d.go:23.
	// overrides TextOutputFlagLongFile.
	TextOutputFlagShortFile

	// TextOutputFlagFields prints fields if given.
	TextOutputFlagFields

	// TextOutputFlagStackTrace prints the stack trace if given.
	TextOutputFlagStackTrace

	// TextOutputFlagStackTraceShortFile prints with file name element only.
	// assumes TextOutputFlagStackTrace.
	TextOutputFlagStackTraceShortFile

	// TextOutputFlagDefault holds predefined default flags.
	// it used by the default Logger.
	TextOutputFlagDefault = TextOutputFlagDate | TextOutputFlagTime | TextOutputFlagSeverity |
		TextOutputFlagPadding | TextOutputFlagFields | TextOutputFlagStackTraceShortFile
)

type Verbose

type Verbose int

Verbose is the type of verbose level.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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