logng

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: BSD-3-Clause Imports: 15 Imported by: 4

README

logng

Go Reference Maintainability Rating Quality Gate Status

Package logng provides leveled and structured logging.

Documentation

Overview

Package logng provides leveled and structured logging.

Example
package main

import (
	"os"
	"time"

	"github.com/goinsane/logng"
)

var testTime, _ = time.ParseInLocation("2006-01-02T15:04:05", "2010-11-12T13:14:15", time.Local)

func main() {
	// reset logng for previous changes if it is running in go test.
	logng.Reset()
	// change writer of default output to stdout from stderr.
	logng.SetTextOutputWriter(os.Stdout)

	// log by Severity.
	// default severity is SeverityInfo.
	// default verbose is 0.
	logng.Debug("this is debug log. but 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. but 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.")
}
Output:

Example (Test1)
package main

import (
	"os"
	"time"

	"github.com/goinsane/logng"
)

func main() {
	// reset logng for previous changes if it is running in go test.
	logng.Reset()
	// just show severity.
	logng.SetTextOutputFlags(logng.TextOutputFlagSeverity)
	// change writer of default output to stdout from stderr.
	logng.SetTextOutputWriter(os.Stdout)

	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 ProgramCounters

func ProgramCounters(size, skip int) []uintptr

ProgramCounters returns program counters by using runtime.Callers.

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 type of field.

type Fields

type Fields []Field

Fields is slice of fields.

func (Fields) Clone

func (f Fields) Clone() Fields

Clone clones the Fields.

func (Fields) Len

func (f Fields) Len() int

Len is implementation of sort.Interface.

func (Fields) Less

func (f Fields) Less(i, j int) bool

Less is implementation of sort.Interface.

func (Fields) Swap

func (f Fields) Swap(i, j int)

Swap is implementation of sort.Interface.

type JSONOutput added in v0.3.0

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

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

func NewJSONOutput added in v0.3.0

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

NewJSONOutput creates a new JSONOutput.

func (*JSONOutput) Log added in v0.3.0

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

Log is the implementation of Output.

func (*JSONOutput) SetFlags added in v0.3.0

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. By default, 0.

func (*JSONOutput) SetOnError added in v0.3.0

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

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

func (*JSONOutput) SetWriter added in v0.3.0

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

SetWriter sets writer. It returns the underlying JSONOutput.

type JSONOutput2 added in v1.1.0

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

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

func NewJSONOutput2 added in v1.1.0

func NewJSONOutput2(w io.Writer, flags JSONOutput2Flag) *JSONOutput2

NewJSONOutput2 creates a new JSONOutput2.

func (*JSONOutput2) Log added in v1.1.0

func (o *JSONOutput2) Log(log *Log)

Log is the implementation of Output.

func (*JSONOutput2) SetFlags added in v1.1.0

func (o *JSONOutput2) SetFlags(flags JSONOutput2Flag) *JSONOutput2

SetFlags sets flags to override every single Log.Flags if the argument flags different from 0. It returns the underlying JSONOutput2. By default, 0.

func (*JSONOutput2) SetOnError added in v1.1.0

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

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

func (*JSONOutput2) SetTimeLayout added in v1.1.0

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

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

func (*JSONOutput2) SetWriter added in v1.1.0

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

SetWriter sets writer. It returns the underlying JSONOutput2.

type JSONOutput2Flag added in v1.1.0

type JSONOutput2Flag int
const (
	JSONOutput2FlagSeverity JSONOutput2Flag = 1 << iota

	JSONOutput2FlagTime

	JSONOutput2FlagTimestamp

	JSONOutput2FlagTimestampMicro

	JSONOutput2FlagUTC

	JSONOutput2FlagSeverityLevel

	JSONOutput2FlagVerbosity

	JSONOutput2FlagLongFunc

	JSONOutput2FlagShortFunc

	JSONOutput2FlagLongFile

	JSONOutput2FlagShortFile

	JSONOutput2FlagStackTrace

	JSONOutput2FlagFields

	JSONOutput2FlagDefault = JSONOutput2FlagSeverity | JSONOutput2FlagTime | JSONOutput2FlagLongFunc |
		JSONOutput2FlagShortFile | JSONOutput2FlagStackTrace | JSONOutput2FlagFields
)

type JSONOutputFlag added in v0.3.0

type JSONOutputFlag int
const (
	JSONOutputFlagUTC JSONOutputFlag = 1 << iota

	JSONOutputFlagSeverity

	JSONOutputFlagFunc

	JSONOutputFlagFile

	JSONOutputFlagStackTrace

	JSONOutputFlagFields

	JSONOutputFlagDefault = JSONOutputFlagSeverity | JSONOutputFlagFunc | JSONOutputFlagFile | JSONOutputFlagStackTrace | 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 Log.

type Logger

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

Logger provides a logger for leveled and structured logging.

Example
package main

import (
	"os"

	"github.com/goinsane/logng"
)

func main() {
	logger := logng.NewLogger(logng.NewTextOutput(os.Stdout, logng.TextOutputFlagSeverity), logng.SeverityInfo, 2)

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

}
Output:

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

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.

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
package main

import (
	"os"

	"github.com/goinsane/logng"
)

// resetForTest resets logng to run new test.
func resetForTest() {
	logng.Reset()
	logng.SetTextOutputFlags(logng.TextOutputFlagDefault & ^logng.TextOutputFlagDate & ^logng.TextOutputFlagTime & ^logng.TextOutputFlagStackTrace)
	logng.SetTextOutputWriter(os.Stdout)
}

func main() {
	resetForTest()
	logng.SetSeverity(logng.SeverityDebug)
	logng.Debug("this is debug log, verbosity 0.")
	logng.Info("this is info log, verbosity 0.")
	logng.Warning("this is warning log, verbosity 0.")

}
Output:

DEBUG - this is debug log, verbosity 0.
INFO - this is info log, verbosity 0.
WARNING - this is warning log, verbosity 0.

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 SetVerbose

func SetVerbose(verbose Verbose) *Logger

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

Example
package main

import (
	"os"

	"github.com/goinsane/logng"
)

// resetForTest resets logng to run new test.
func resetForTest() {
	logng.Reset()
	logng.SetTextOutputFlags(logng.TextOutputFlagDefault & ^logng.TextOutputFlagDate & ^logng.TextOutputFlagTime & ^logng.TextOutputFlagStackTrace)
	logng.SetTextOutputWriter(os.Stdout)
}

func main() {
	resetForTest()
	logng.SetVerbose(2)
	logng.V(0).Debug("this is debug log, verbosity 0. it won't be shown.")
	logng.V(1).Info("this is info log, verbosity 1.")
	logng.V(2).Warning("this is warning log, verbosity 2.")
	logng.V(3).Error("this is error log, verbosity 3. it won't be shown.")

}
Output:

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

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 added in v0.5.0

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 fieldMap.

func WithFields

func WithFields(fields ...Field) *Logger

WithFields clones the default Logger with given fields.

func WithPrefix added in v0.2.0

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

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

Example
package main

import (
	"os"

	"github.com/goinsane/logng"
)

// resetForTest resets logng to run new test.
func resetForTest() {
	logng.Reset()
	logng.SetTextOutputFlags(logng.TextOutputFlagDefault & ^logng.TextOutputFlagDate & ^logng.TextOutputFlagTime & ^logng.TextOutputFlagStackTrace)
	logng.SetTextOutputWriter(os.Stdout)
}

func main() {
	resetForTest()
	logng.SetTextOutputFlags(0)
	logng.WithPrefix("APP1: ").WithPrefix("APP2: ").Info("this is info log, verbosity 0.")

}
Output:

APP1: APP2: this is info log, verbosity 0.

func WithPrefixf added in v0.2.0

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 added in v0.2.0

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

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

Example
package main

import (
	"os"

	"github.com/goinsane/logng"
)

// resetForTest resets logng to run new test.
func resetForTest() {
	logng.Reset()
	logng.SetTextOutputFlags(logng.TextOutputFlagDefault & ^logng.TextOutputFlagDate & ^logng.TextOutputFlagTime & ^logng.TextOutputFlagStackTrace)
	logng.SetTextOutputWriter(os.Stdout)
}

func main() {
	resetForTest()
	logng.SetTextOutputFlags(0)
	logng.WithSuffix(" *").WithSuffix(" +").Info("this is info log, verbosity 0.")

}
Output:

this is info log, verbosity 0. + *

func WithSuffixf added in v0.2.0

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
package main

import (
	"os"
	"time"

	"github.com/goinsane/logng"
)

var testTime, _ = time.ParseInLocation("2006-01-02T15:04:05", "2010-11-12T13:14:15", time.Local)

// resetForTest resets logng to run new test.
func resetForTest() {
	logng.Reset()
	logng.SetTextOutputFlags(logng.TextOutputFlagDefault & ^logng.TextOutputFlagDate & ^logng.TextOutputFlagTime & ^logng.TextOutputFlagStackTrace)
	logng.SetTextOutputWriter(os.Stdout)
}

func main() {
	resetForTest()
	logng.SetTextOutputFlags(logng.TextOutputFlagDefault)
	logng.WithTime(testTime).Info("this is info log, verbosity 0.")

}
Output:

2010/11/12 13:14:15 INFO - this is info log, verbosity 0.

func WithVerbosity added in v0.5.1

func WithVerbosity(verbosity Verbose) *Logger

WithVerbosity clones the default Logger with the given verbosity.

func (*Logger) Clone

func (l *Logger) Clone() *Logger

Clone clones the 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 Logger's print severity.

func (*Logger) Printf

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

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

func (*Logger) Println

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

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

func (*Logger) SetOutput

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

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

func (*Logger) SetPrintSeverity

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

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

func (*Logger) SetSeverity

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

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

func (*Logger) SetStackTraceSeverity

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

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

func (*Logger) SetVerbose

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

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

func (*Logger) V

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

V clones the Logger with the given verbosity if the 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 added in v0.5.0

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

WithCtxErrVerbosity clones the 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 Logger with given keys and values of Field.

func (*Logger) WithFieldMap

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

WithFieldMap clones the Logger with the given fieldMap.

func (*Logger) WithFields

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

WithFields clones the Logger with given fields.

func (*Logger) WithPrefix added in v0.2.0

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

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

func (*Logger) WithPrefixf added in v0.2.0

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

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

func (*Logger) WithSuffix added in v0.2.0

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

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

func (*Logger) WithSuffixf added in v0.2.0

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

WithSuffixf clones the 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 Logger with the given time.

func (*Logger) WithVerbosity added in v0.5.0

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

WithVerbosity clones the Logger with the given verbosity.

type Output

type Output interface {
	Log(log *Log)
}

Output is an interface for Logger output. All the 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) (q *QueuedOutput)

NewQueuedOutput creates a new QueuedOutput by the given output.

func (*QueuedOutput) Close

func (q *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 (q *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 (q *QueuedOutput) SetBlocking(blocking bool) *QueuedOutput

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

func (*QueuedOutput) SetOnQueueFull

func (q *QueuedOutput) SetOnQueueFull(f func()) *QueuedOutput

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

func (*QueuedOutput) WaitForEmpty

func (q *QueuedOutput) WaitForEmpty(ctx context.Context) error

WaitForEmpty waits until the queue is empty by the given context.

type Severity

type Severity int

Severity describes severity level of Log.

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

	// SeverityFatal is fatal severity level
	SeverityFatal

	// SeverityError is error severity level
	SeverityError

	// SeverityWarning is warning severity level
	SeverityWarning

	// SeverityInfo is info severity level
	SeverityInfo

	// SeverityDebug is 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 implementation of encoding.TextMarshaler. If s is invalid, it returns nil and result of Severity.CheckValid.

func (Severity) String

func (s Severity) String() string

String is implementation of fmt.Stringer.

func (*Severity) UnmarshalText

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

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

type StackCaller

type StackCaller struct {
	runtime.Frame
}

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

func (StackCaller) Format

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

Format is 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 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 stack trace.

func NewStackTrace

func NewStackTrace(programCounters []uintptr) *StackTrace

NewStackTrace creates a new StackTrace object.

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 callers.

func (*StackTrace) Clone

func (t *StackTrace) Clone() *StackTrace

Clone clones the StackTrace object.

func (*StackTrace) Format

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

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

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 all callers.

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 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.

Example
package main

import (
	"os"

	"github.com/goinsane/logng"
)

// resetForTest resets logng to run new test.
func resetForTest() {
	logng.Reset()
	logng.SetTextOutputFlags(logng.TextOutputFlagDefault & ^logng.TextOutputFlagDate & ^logng.TextOutputFlagTime & ^logng.TextOutputFlagStackTrace)
	logng.SetTextOutputWriter(os.Stdout)
}

func main() {
	resetForTest()
	logng.SetTextOutputFlags(0)
	logng.Info("this is info log, verbosity 0.")

}
Output:

this is info log, verbosity 0.

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. By default, 0.

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. An 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
	TextOutputFlagMicroseconds

	// TextOutputFlagUTC uses UTC rather than the local time zone
	TextOutputFlagUTC

	// TextOutputFlagSeverity prints severity level
	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()
	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
	TextOutputFlagShortFile

	// TextOutputFlagFields prints fields if there are
	TextOutputFlagFields

	// TextOutputFlagStackTrace prints the stack trace if there is
	TextOutputFlagStackTrace

	// TextOutputFlagDefault holds initial flags for the Logger
	TextOutputFlagDefault = TextOutputFlagDate | TextOutputFlagTime | TextOutputFlagSeverity | TextOutputFlagPadding | TextOutputFlagFields | TextOutputFlagStackTrace
)

type Verbose

type Verbose int

Verbose is 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