log

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: Apache-2.0 Imports: 9 Imported by: 1

README

log

Log is a simple logging framework for golang.

Getting Started

import "git.martin-riedl.de/golang/log"

func main(){
	log.Default.Info("My First Info Message")
}

Check out this simple example file for the basic usage.

Multiple Log Instance

Create a new log instance (instead of using the Default).

import "git.martin-riedl.de/golang/log"

var myLog := log.NewLoggerDefault()
myLog.Info("My First Info Message")

Outputs / Formatter / Printer

  • Formatter defines the output format (e.g. Key/Value or JSON)
  • Printer defines the message output location (e.g. stdout or file)
  • Output is a combination of Formatter and Printer

The following sample creates 2 outputs. The first sends a key/value format to stdout. The second writes messages as JSON format to stdout.

import "git.martin-riedl.de/golang/log"

var output1 := NewOutput(log.LevelInfo, NewFormatterKeyValue(), NewPrinterStdout()
var output2 := NewOutput(log.LevelError, NewFormatterJSON(), NewPrinterStdout())

var myLog := log.NewLogger(
	[]*Output{output1, output2}
)
myLog.Info("My First Info Message")

Custom Formatter / Printer

If you need a special Formatter or Printer: simply implement the Interface (same name) and assign it to an Output.

License

Copyright 2023 Martin Riedl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = NewLoggerDefault()

Functions

This section is empty.

Types

type Content

type Content struct {
	Key   string `json:"key"`
	Value any    `json:"value"`
}

type Entry

type Entry struct {
	Logger    *Logger
	Content   []Content
	Message   string
	Level     Level
	Time      time.Time
	CallStack []runtime.Frame
}

func NewEntry

func NewEntry(logger *Logger) *Entry

func (*Entry) Debug

func (entry *Entry) Debug(args ...any)

func (*Entry) Error

func (entry *Entry) Error(args ...any)

func (*Entry) Fatal

func (entry *Entry) Fatal(args ...any)

func (*Entry) Info

func (entry *Entry) Info(args ...any)

func (*Entry) Log

func (entry *Entry) Log(level Level, args ...any)

func (*Entry) Trace

func (entry *Entry) Trace(args ...any)

func (*Entry) Warning

func (entry *Entry) Warning(args ...any)

func (*Entry) With

func (entry *Entry) With(key string, value any) *Entry

func (*Entry) WithContent

func (entry *Entry) WithContent(content []Content) *Entry

func (*Entry) WithMap added in v0.5.0

func (entry *Entry) WithMap(entries Map) *Entry

type Formatter

type Formatter interface {
	Begin(*Entry)
	Process(*Entry)
	End(*Entry) []byte
}

type FormatterJSON

type FormatterJSON struct {
	FlatContent bool
	TimeFormat  string
	// contains filtered or unexported fields
}

func NewFormatterJSON

func NewFormatterJSON() *FormatterJSON

func (*FormatterJSON) Begin

func (formatter *FormatterJSON) Begin(entry *Entry)

func (*FormatterJSON) End

func (formatter *FormatterJSON) End(_ *Entry) []byte

func (*FormatterJSON) Process

func (formatter *FormatterJSON) Process(entry *Entry)

type FormatterKeyValue

type FormatterKeyValue struct {
	TimeFormat string
	// HighPriorityKeys are printed before the actual log message
	HighPriorityKeys []string
	// PriorityKeys are printed after the log message
	PriorityKeys []string
	// contains filtered or unexported fields
}

func NewFormatterKeyValue

func NewFormatterKeyValue() *FormatterKeyValue

func (*FormatterKeyValue) Begin

func (formatter *FormatterKeyValue) Begin(entry *Entry)

func (*FormatterKeyValue) End

func (formatter *FormatterKeyValue) End(_ *Entry) []byte

func (*FormatterKeyValue) Process

func (formatter *FormatterKeyValue) Process(entry *Entry)

type Level

type Level uint32
const (
	LevelFatal   Level = 0
	LevelError   Level = 10
	LevelWarning Level = 20
	LevelInfo    Level = 30
	LevelDebug   Level = 40
	LevelTrace   Level = 50
)

func (Level) String

func (level Level) String() string

type Logger

type Logger struct {
	Outputs []*Output
}

func NewLogger

func NewLogger(outputs []*Output) *Logger

func NewLoggerDefault

func NewLoggerDefault() *Logger

func (*Logger) Debug

func (logger *Logger) Debug(args ...any)

func (*Logger) Error

func (logger *Logger) Error(args ...any)

func (*Logger) Fatal

func (logger *Logger) Fatal(args ...any)

func (*Logger) Info

func (logger *Logger) Info(args ...any)

func (*Logger) Log

func (logger *Logger) Log(level Level, args ...any)

func (*Logger) LogEntry

func (logger *Logger) LogEntry(entry *Entry)

func (*Logger) Trace

func (logger *Logger) Trace(args ...any)

func (*Logger) Warning

func (logger *Logger) Warning(args ...any)

func (*Logger) With

func (logger *Logger) With(key string, value any) *Entry

func (*Logger) WithContent

func (logger *Logger) WithContent(content []Content) *Entry

func (*Logger) WithMap added in v0.5.0

func (logger *Logger) WithMap(entries Map) *Entry

type Map added in v0.6.0

type Map map[string]any

type Output

type Output struct {
	Level     Level
	Formatter Formatter

	Printer Printer
	// contains filtered or unexported fields
}

func NewOutput

func NewOutput(level Level, formatter Formatter, printer Printer) *Output

func NewOutputDefault

func NewOutputDefault() *Output

func (*Output) Send

func (output *Output) Send(entry *Entry)

type Printer

type Printer interface {
	Write(p []byte)
}

type PrinterStdout

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

func NewPrinterStdout

func NewPrinterStdout() *PrinterStdout

func (*PrinterStdout) Write

func (printer *PrinterStdout) Write(p []byte)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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