github.heygears.com/openimsdk/tools@v0.0.49/log/file-rotatelogs/options.go (about)

     1  package rotatelogs
     2  
     3  import (
     4  	"github.com/openimsdk/tools/log/file-rotatelogs/internal/option"
     5  	"time"
     6  )
     7  
     8  const (
     9  	optkeyClock         = "clock"
    10  	optkeyHandler       = "handler"
    11  	optkeyLinkName      = "link-name"
    12  	optkeyMaxAge        = "max-age"
    13  	optkeyRotationTime  = "rotation-time"
    14  	optkeyRotationSize  = "rotation-size"
    15  	optkeyRotationCount = "rotation-count"
    16  	optkeyForceNewFile  = "force-new-file"
    17  )
    18  
    19  // WithClock creates a new Option that sets a clock
    20  // that the RotateLogs object will use to determine
    21  // the current time.
    22  //
    23  // By default rotatelogs.Local, which returns the
    24  // current time in the local time zone, is used. If you
    25  // would rather use UTC, use rotatelogs.UTC as the argument
    26  // to this option, and pass it to the constructor.
    27  func WithClock(c Clock) Option {
    28  	return option.New(optkeyClock, c)
    29  }
    30  
    31  // WithLocation creates a new Option that sets up a
    32  // "Clock" interface that the RotateLogs object will use
    33  // to determine the current time.
    34  //
    35  // This optin works by always returning the in the given
    36  // location.
    37  func WithLocation(loc *time.Location) Option {
    38  	return option.New(optkeyClock, clockFn(func() time.Time {
    39  		return time.Now().In(loc)
    40  	}))
    41  }
    42  
    43  // WithLinkName creates a new Option that sets the
    44  // symbolic link name that gets linked to the current
    45  // file name being used.
    46  func WithLinkName(s string) Option {
    47  	return option.New(optkeyLinkName, s)
    48  }
    49  
    50  // WithMaxAge creates a new Option that sets the
    51  // max age of a log file before it gets purged from
    52  // the file system.
    53  func WithMaxAge(d time.Duration) Option {
    54  	return option.New(optkeyMaxAge, d)
    55  }
    56  
    57  // WithRotationTime creates a new Option that sets the
    58  // time between rotation.
    59  func WithRotationTime(d time.Duration) Option {
    60  	return option.New(optkeyRotationTime, d)
    61  }
    62  
    63  // WithRotationSize creates a new Option that sets the
    64  // log file size between rotation.
    65  func WithRotationSize(s int64) Option {
    66  	return option.New(optkeyRotationSize, s)
    67  }
    68  
    69  // WithRotationCount creates a new Option that sets the
    70  // number of files should be kept before it gets
    71  // purged from the file system.
    72  func WithRotationCount(n uint) Option {
    73  	return option.New(optkeyRotationCount, n)
    74  }
    75  
    76  // WithHandler creates a new Option that specifies the
    77  // Handler object that gets invoked when an event occurs.
    78  // Currently `FileRotated` event is supported
    79  func WithHandler(h Handler) Option {
    80  	return option.New(optkeyHandler, h)
    81  }
    82  
    83  // ForceNewFile ensures a new file is created every time New()
    84  // is called. If the base file name already exists, an implicit
    85  // rotation is performed
    86  func ForceNewFile() Option {
    87  	return option.New(optkeyForceNewFile, true)
    88  }