logwriter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: MIT Imports: 13 Imported by: 0

README

logwriter

The logwriter is compressed log writer package for golang.

Usage

See example_*_test.go files.

Documentation

Overview

Package logwriter implements specialized WriteCloser for writing compressed logs.

Example (Log)

An example integrates log and logwriter.

log.Println("This message is written to default destination.")
defer log.Println("This message is written to default destination.")

opt := logwriter.DefaultOpenOption
opt.FileOrDir = os.TempDir()     // Please change path to correct directory where you want log files to be placed.
opt.Prefix = "logwriter-example" // Alternatively, you can use auto-detected name.
tearDownLogger, err := logwriter.Setup(opt)
if err != nil {
	panic(err)
}
defer tearDownLogger()

log.Print("This message is written to /tmp/logwriter-example.*.log.zst within a second.")
Output:

Example (Open)

An example using Open() with application log without standard logging system.

opt := logwriter.DefaultOpenOption
opt.FileOrDir = os.TempDir()     // Please change path to correct directory where you want log files to be placed.
opt.Prefix = "logwriter-example" // Alternatively, you can use auto-detected name.
w, err := logwriter.Open(opt)
if err != nil {
	panic(err)
}
defer w.Close()

// w implemented io.WriteCloser interface.
// Note that w is buffered. If the application exits without calling w.Close(), some data will be lost.

// third_party_logging_framework.SetOutput(w)
//   or
// use w directly like this:
w.Write([]byte("This message is written to /tmp/logwriter-example.*.log.zst within a second.\n"))
fmt.Fprintf(w, "pid is %d.\n", os.Getpid())
io.WriteString(w, "Application going to shutdown.\n")
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOpenOption = OpenOption{
	FileOrDir: "-",
	Prefix:    filepath.Base(os.Args[0]),
	Suffix:    ".zst",
	Flag:      os.O_WRONLY | os.O_APPEND | os.O_CREATE,
	Mode:      0666,
}
View Source
var Discard io.WriteCloser = &discard{}

Functions

func NewBuffer

func NewBuffer(size int, interval time.Duration, w io.WriteCloser) io.WriteCloser

func NewCompressedWriter

func NewCompressedWriter(w io.WriteCloser, a Algorithm) io.WriteCloser

func NewTickWriter

func NewTickWriter(w io.WriteCloser, interval time.Duration) io.WriteCloser

func Open

func Open(option OpenOption) (io.WriteCloser, error)

Types

type Algorithm

type Algorithm interface {
	Compress(in []byte, out *bytes.Buffer) error
	Decompress(in []byte, out *bytes.Buffer) error
}

type Buffer

type Buffer struct {
	Size     int
	Interval time.Duration
	// Now() is a function returns current time like time.Time().
	// This function used to inject time from outside.
	Now func() time.Time
	// contains filtered or unexported fields
}

Buffer implements buffered io.Writer like bufio.Writer, but specializing in log writer.

func (*Buffer) Close

func (b *Buffer) Close() (err error)

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

type CompressedWriter

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

CompressedWriter compress data before writing data.

func (*CompressedWriter) Close

func (c *CompressedWriter) Close() error

func (*CompressedWriter) Write

func (c *CompressedWriter) Write(p []byte) (n int, err error)

type GzipAlgorithm

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

func (*GzipAlgorithm) Compress

func (g *GzipAlgorithm) Compress(in []byte, out *bytes.Buffer) error

func (*GzipAlgorithm) Decompress

func (g *GzipAlgorithm) Decompress(in []byte, out *bytes.Buffer) error

type NopAlgorithm

type NopAlgorithm struct{}

func (*NopAlgorithm) Compress

func (n *NopAlgorithm) Compress(in []byte, out *bytes.Buffer) error

func (*NopAlgorithm) Decompress

func (n *NopAlgorithm) Decompress(in []byte, out *bytes.Buffer) error

type OpenOption

type OpenOption struct {
	// Path to file or directory.
	// If empty string specified, discard all writes operations.
	// If "-" is specified, send to stderr.
	// If existing directory path specified, file name is generated automatically.
	FileOrDir string
	// Prefix for file name.
	// This option only affect if FileOrDir points to a directory.
	Prefix string
	// Additional file extensions.
	// This option only affect if FileOrDir points to a directory.
	//
	// Supported extensions list:
	//	".zst"
	//	".gz"
	//	"" (without compression)
	Suffix string
	// Flag for open a file.
	Flag int
	// Default file mode.
	Mode os.FileMode
}

type TearDown

type TearDown func() error

func Setup

func Setup(option OpenOption) (TearDown, error)

type TickWriter

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

TickWriter calls w.Write(nil) every specified interval. Main use case is flushing write buffer in the Buffer.

var file io.WriteCloser
w = NewBuffer(0, time.Second, file)
w = TickWriter(w, time.Second)
defer w.Close()

func (*TickWriter) Close

func (t *TickWriter) Close() error

func (*TickWriter) Write

func (t *TickWriter) Write(p []byte) (int, error)

type ZstdAlgorithm

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

func (*ZstdAlgorithm) Compress

func (z *ZstdAlgorithm) Compress(in []byte, out *bytes.Buffer) error

func (*ZstdAlgorithm) Decompress

func (z *ZstdAlgorithm) Decompress(in []byte, out *bytes.Buffer) error

Jump to

Keyboard shortcuts

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