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 ¶
- Variables
- func NewBuffer(size int, interval time.Duration, w io.WriteCloser) io.WriteCloser
- func NewCompressedWriter(w io.WriteCloser, a Algorithm) io.WriteCloser
- func NewTickWriter(w io.WriteCloser, interval time.Duration) io.WriteCloser
- func Open(option OpenOption) (io.WriteCloser, error)
- type Algorithm
- type Buffer
- type CompressedWriter
- type GzipAlgorithm
- type NopAlgorithm
- type OpenOption
- type TearDown
- type TickWriter
- type ZstdAlgorithm
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 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.
type CompressedWriter ¶
type CompressedWriter struct {
// contains filtered or unexported fields
}
CompressedWriter compress data before writing data.
func (*CompressedWriter) Close ¶
func (c *CompressedWriter) Close() 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) 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
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
Click to show internal directories.
Click to hide internal directories.