log

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: MIT Imports: 14 Imported by: 1

Documentation

Overview

Package log implements a simple logging package

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetId

func GetId(ctx context.Context) string

GetId returns a logID which is fetched either from the provided context or auto-generated.

Types

type F

type F map[string]interface{}

F is the fields to use as a log event/message.

type Level added in v0.0.14

type Level string

Level indicates the severity of a log event/message.

const (
	// INFO is the log severity indicating an issue that is informational in nature.
	INFO Level = "info"
	// ERROR is the log severity indicating an issue that is critical in nature.
	ERROR Level = "error"
	// CtxKey is the name of the context key used to store the logID.
	CtxKey = logContextKeyType("Ong-logID")
)

type Logger

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

Logger represents an active logging object that generates lines of output to an io.Writer. It stores log messages into a circular buffer. All those log events are only flushed to the underlying io.Writer when a message with level ERROR is logged.

It can be used simultaneously from multiple goroutines. Use New to get a valid Logger.

func New

func New(w io.Writer, maxMsgs int) Logger

New creates a new logger. The returned logger buffers upto maxMsgs log messages in a circular buffer.

func (Logger) Error

func (l Logger) Error(e error, fs ...F)

Error will log at the ERROR level.

Example
package main

import (
	"errors"
	"os"

	"github.com/komuw/ong/log"
)

func main() {
	l := log.New(os.Stdout, 1000)

	l.Info(log.F{"msg": "sending email", "email": "jane@example.com"})
	l.Error(errors.New("sending email failed."), log.F{"email": "jane@example.com"})

	// example output:
	//   {"email":"jane@example.com","level":"info","logID":"r73RdRZEExH7cnax2faY7A","msg":"sending email","timestamp":"2022-09-16T12:56:05.471496845Z"}
	//   {"email":"jane@example.com","err":"sending email failed.","level":"error","logID":"r73RdRZEExH7cnax2faY7A","timestamp":"2022-09-16T12:56:05.471500752Z"}
}
Output:

func (Logger) Info

func (l Logger) Info(f F)

Info will log at the INFO level.

func (Logger) StdLogger

func (l Logger) StdLogger() *stdLog.Logger

StdLogger returns a logger from the Go standard library log package.

That logger will use l as its output.

example usage:

l := log.New(ctx, os.Stdout, 100, true)
stdLogger := l.StdLogger()
stdLogger.Println("hey")

func (Logger) WithCaller

func (l Logger) WithCaller() Logger

WithCaller return a new logger, based on l, that will include callers info in its output.

func (Logger) WithCtx

func (l Logger) WithCtx(ctx context.Context) Logger

WithCtx return a new logger, based on l, with the given ctx.

func (Logger) WithFields

func (l Logger) WithFields(f F) Logger

WithFields return a new logger, based on l, that will include the given fields in all its output.

func (Logger) WithImmediate

func (l Logger) WithImmediate() Logger

WithImmediate return a new logger, based on l, that will log immediately without buffering.

func (Logger) Write

func (l Logger) Write(p []byte) (n int, err error)

Write implements the io.Writer interface.

This is useful if you want to set this logger as a writer for the standard library log.

example usage:

l := log.New(ctx, os.Stdout, 100, true)
stdLogger := stdLog.New(l, "stdlib", stdLog.LstdFlags)
stdLogger.Println("hello world")

Jump to

Keyboard shortcuts

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