clog

package module
v0.0.0-...-8921782 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2020 License: MIT Imports: 4 Imported by: 0

README

Clog is a concurrent log. It stores log entries in a buffer and writes (flushes) them out when the log buffer gets full. Log entries that are in memory (not flushed) can be searched and counted. Multiple goroutines can log concurrently without blocking, even if there is an ongoing flush or search.

Example usage:

package main

import (
	"bitbucket.org/tentontrain/clog"
	"fmt"
	"os"
	"time"
)

var log clog.Log

func init() {
	file, _ := os.OpenFile("log.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	log.Initialize(1024, file, 32)
}

func main() {
	// log stuff concurrently
	for i := 0; i < 10; i++ {
		go func(a int) {
			log.Log(a)
		}(i)
	}

	// search and print contents of log (does not block writters - their writes are buffered)
	// note: buffered entries are not searched
	go fmt.Println(
		log.Search(func(a fmt.Stringer) bool {
			return true // get everything
		}))

	time.Sleep(time.Millisecond * 1)

	// stop log and write everything (log contents and buffered entries) to file
	log.Stop()
}

GoDoc

Documentation

Overview

Package clog is a concurrent log. It stores log entries in a buffer and writes (flushes) them out when the log buffer is full. Log entries that are in memory (not flushed) can be searched and counted. Multiple goroutines can log concurrently without blocking, even if there is an ongoing flush or search.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry string

Generic log event

func (Entry) String

func (e Entry) String() string

type Error

type Error string

Error

func (Error) String

func (e Error) String() string

type Fatal

type Fatal string

Critical error

func (Fatal) String

func (c Fatal) String() string

type Info

type Info string

Info string

func (Info) String

func (l Info) String() string

type Log

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

Log is the main log type. Must be initialized before use.

func (*Log) Count

func (l *Log) Count(match func(fmt.Stringer) bool) int

Count events that satisfy the match function.

func (*Log) Error

func (l *Log) Error(a ...interface{})

Error logs an error. It will be prefixed with 'error:'.

func (*Log) Fatal

func (l *Log) Fatal(a ...interface{})

Fatal logs the error then exits the program.

func (*Log) Flush

func (l *Log) Flush()

Flush log contents. Will not flush contents of temporary buffer (use Stop for that).

func (*Log) Info

func (l *Log) Info(a ...interface{})

Info prefixes log entry with 'info:'.

func (*Log) Initialize

func (l *Log) Initialize(size int, out io.Writer, bufsize int)

Initialize a new logger. Parameter size determines the number of events to hold before flushing to out. Parameter out is the writer used to store log entries on a flush (can be nil). Parameter bufsize is the size of the temporary buffer used to avoid stalls. If set to zero log call block.

func (*Log) Log

func (l *Log) Log(s fmt.Stringer)

Log adds any fmt.Stringer to the log.

func (*Log) LogPrint

func (l *Log) LogPrint(a ...interface{})

func (*Log) Search

func (l *Log) Search(match func(fmt.Stringer) bool) []fmt.Stringer

Search returns all events that satisfy match.

func (*Log) Stop

func (l *Log) Stop()

Stop the log. Both the log contents and the tempory buffer are flushed. To log again after Stop, call Initialize. Count and Search can be used after Stop.

Jump to

Keyboard shortcuts

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