|
| 1 | +# Rotate package |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +rotate package provides a very simple tool to truncate a given file to 0 after it copies the last *configurable* part of this file to a new file with suffix .tail |
| 6 | + |
| 7 | +The idea is that services need to have their log files (or redirection) be open in append mode. So truncation of the log file should be enough. |
| 8 | + |
| 9 | +There is no guarantee that some logs will be lost between the copying of the file tail and the truncation of the file. |
| 10 | + |
| 11 | +## Structure |
| 12 | + |
| 13 | +### Configuration and Initialization |
| 14 | + |
| 15 | +This section implements the options interface, which allow flexible configuration of the Rotator without a complex parameter list. |
| 16 | + |
| 17 | +```go |
| 18 | +type Option interface { |
| 19 | + apply(cfg *Rotator) |
| 20 | +} |
| 21 | + |
| 22 | +type optFn func(cfg *Rotator) |
| 23 | + |
| 24 | +func (fn optFn) apply(cfg *Rotator) { |
| 25 | + fn(cfg) |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +The package provides a default Rotator and a constructor function that accepts a variable number of Option values to override |
| 30 | +the required settings only |
| 31 | + |
| 32 | +```go |
| 33 | + // Create a rotator with custom limits |
| 34 | + r := NewRotator( |
| 35 | + MaxSize(50 * Megabytes), // rotate after 50MB |
| 36 | + TailSize(25 * Megabytes), // keep last 25MB |
| 37 | + Suffix(".anything"), // name rotated files "*.anything" |
| 38 | + ) |
| 39 | +``` |
| 40 | + |
| 41 | +The key components are: |
| 42 | + - `Option` interface: defines a configuration option that can be changes in a Rotator. |
| 43 | + - `optFn` type: a function adapter that implements the Option interface. |
| 44 | + - `MaxSize`, `TailSize`, `Suffix`: helpers that return Option values to adjust specific fields. |
| 45 | + - `NewRotator`: overrides passed options only over the default configuration and returns a Rotator. |
| 46 | + |
| 47 | + |
| 48 | +### Rotator Interface |
| 49 | +```go |
| 50 | + |
| 51 | +type Rotator struct { |
| 52 | + maxsize Size // maxsize is the max file size. If file size exceeds maxsize rotation is applied |
| 53 | + // otherwise file is not touched |
| 54 | + // default : 20 MB |
| 55 | + |
| 56 | + tailsize Size // tailsize is size of the chunk to keep with Suffix before truncation of the file. |
| 57 | + // If value is bigger than MaxSize, it will be set to MaxSize. |
| 58 | + // default : 10 MB |
| 59 | + |
| 60 | + suffix string // suffix of the tail chunk, default to .0 |
| 61 | +} |
| 62 | + |
| 63 | +// Rotate takes a file and compares it to maxsize, it only rotates if file size > max size |
| 64 | +// It copies the tailsize to tail file and truncates the old file to 0 |
| 65 | +func (r *Rotator) Rotate(file string) error |
| 66 | + |
| 67 | +// RotateAll will rotate all files in the directory |
| 68 | +// if a set of names is given only named files will be rotated, other unknown files will be deleted |
| 69 | +func (r *Rotator) RotateAll(dir string, names ...string) error |
| 70 | +``` |
| 71 | + |
| 72 | +### Constructor |
| 73 | +```go |
| 74 | +func NewRotator(opt ...Option) Rotator |
| 75 | +``` |
0 commit comments