Documentation ¶
Index ¶
- Constants
- type Clock
- type FileGzipTransformer
- type FileRenameTransformer
- type FileRotator
- type FileSizeBasedTrigger
- type FileTimeBasedTrigger
- type FileTimestampNameGenerator
- type FileTransformer
- type FileTransformingRotator
- type FileTrigger
- type NameGenerator
- type RollingWriter
- type Rotator
- type Trigger
Examples ¶
Constants ¶
const ( // ErrClosed is returned when the operations are performed on an already // closed item. ErrClosed = barrelError("closed writer") )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileGzipTransformer ¶
type FileGzipTransformer struct { // Describes gzip compression level. GzipLevel int }
FileGzipTransformer converts file to gzip format.
func (FileGzipTransformer) Transform ¶
func (t FileGzipTransformer) Transform(path string) (string, error)
Transform transforms the file at the provided path to a gzip file in the same directory. It add .gz extension to the file. The original file is not retained. The mod time and access time of the new gzip file is same as mod time of the original file.
type FileRenameTransformer ¶
type FileRenameTransformer struct { // Generator to be used for generating new name of the file. NameGenerator NameGenerator // Force physical move of the file. ForceMove bool }
FileRenameTransformer renames a file.
type FileRotator ¶
Rotator changes the file. If there is an error while rotating then a non-nil error is returned.
type FileSizeBasedTrigger ¶
type FileSizeBasedTrigger struct { // Max size of file. Size int64 }
FileSizeBasedTrigger describes a trigger which works on size of the file.
type FileTimeBasedTrigger ¶
type FileTimeBasedTrigger struct { // Describes the rotation schedule. CronExpression string // Clock to wrap stdblib timing functions for testing. Clock Clock // contains filtered or unexported fields }
FileTimeBasedTrigger describes a trigger which works on mod time of the file.
type FileTimestampNameGenerator ¶
type FileTimestampNameGenerator struct { // Format of timestamp to use. Format string }
FileTimestampNameGenerator is a NameGenerator which generates timestamp suffixed name for the file with current name.
func (FileTimestampNameGenerator) Name ¶
func (g FileTimestampNameGenerator) Name(current string) (string, error)
Name generates a new timestamp suffixed name for file at current path.
current = application.log, out = application_2020-01-02.log
If the directory of the file already has file with same name and suffixed timestamp with indexes, then it returns the file with next available index.
dir |- application_2020-01-02.0.log |- application_2020-01-02.1.log |- application_2020-01-02.2.log current = application.log, out = application_2020-01-02.3.log
If the directory of file contains file with same name and and suffixed timestamp without indexes, then it moves the file to correct index, and returns the file with next index.
dir |- application_2020-01-02.log current = application.log, out application_2020-01-02.1.log Afterwards, dir |- application_2020-01-02.0.log
If there are any, errors while reading the directories, then non-nil error is returned.
type FileTransformer ¶
FileTransformer takes a file path, transforms the file and returns the path of resulting file. If any error occurs non-nil error is returned.
type FileTransformingRotator ¶
type FileTransformingRotator struct { // Flag to be used to Open the the new file, os.O_CREATE is added // automatically. OpenFlag int // Transformers to to transform the file. Transformers []FileTransformer }
FileTransformingRotator describes a Rotator, which works by rotating files.
func (FileTransformingRotator) Rotate ¶
Rotate closes the current file, runs all the provided transformers in order, and returns a reference to a new file, which is created with same name and permissions (os.FileMode) as the original file.
It uses OpenFlag while opening the file.
If there is any error in any of the transformers, then no further transformers are applied and non-nil error is returned.
type FileTrigger ¶
FileTrigger tells whether the given file should be rotated or not. If it returns true then rotation is preformed, otherwise not. If there are any errors while determining the trigger then a non-nil error is returned.
type NameGenerator ¶
NameGenerator generates a new name with the provided current name.
type RollingWriter ¶
type RollingWriter struct { // Writer which is wrapped. io.Writer // Trigger used to check whether to rotate or not. Trigger // Rotator used to change the Writer. Rotator // contains filtered or unexported fields }
RollingWriter wraps an io.Writer, providing mechanisms to check and perform rotation on each write.
Example ¶
package main import ( "os" "github.com/hemantjadon/barrel" ) func main() { file, _ := os.OpenFile("/path/to/file", os.O_CREATE|os.O_WRONLY|os.O_CREATE, 0644) writer := barrel.RollingWriter{ Writer: file, Trigger: barrel.AdaptFileTrigger(barrel.FileSizeBasedTrigger{Size: 100 * 1000 * 1000}), Rotator: barrel.AdaptFileRotator(barrel.FileTransformingRotator{ OpenFlag: os.O_CREATE | os.O_WRONLY | os.O_CREATE, Transformers: []barrel.FileTransformer{ barrel.FileRenameTransformer{ NameGenerator: barrel.FileTimestampNameGenerator{}, }, }, }), } _, _ = writer.Write([]byte(`hello world`)) }
Output:
func (*RollingWriter) Write ¶
func (w *RollingWriter) Write(p []byte) (int, error)
Write writes the given bytes to the underlying Writer.
Before writing, it calls Trigger.Trigger, to check whether to rotate or not, if it returns true then, Rotator.Rotate is called and the underlying writer is changed to the new writer. And bytes are written to the new Writer.
If Trigger.Trigger returns false the bytes are immediately written to current Writer.
type Rotator ¶
Rotator changes the writer. If there is an error while rotating then a non-nil error is returned.
func AdaptFileRotator ¶
func AdaptFileRotator(fr FileRotator) Rotator
AdaptFileRotator converts a FileRotator into a generic Rotator, which can be used with RollingWriter.
The Rotator returned will return a non-nil error if the writer provided is not a reference of os.File.
type Trigger ¶
Trigger tells whether rotation should be done or not. If it returns true then rotation is preformed, otherwise not. If there are any errors while determining the trigger then a non-nil error is returned.
func AdaptFileTrigger ¶
func AdaptFileTrigger(ft FileTrigger) Trigger
AdaptFileTrigger converts a FileTrigger into a generic Trigger, which can be used with RollingWriter.
The Trigger returned will return a non-nil error if the writer provided is not a reference of os.File.