github.com/richardwilkes/toolbox@v1.121.0/log/rotation/options.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package rotation 11 12 import ( 13 "os" 14 "path/filepath" 15 16 "github.com/richardwilkes/toolbox/cmdline" 17 "github.com/richardwilkes/toolbox/errs" 18 "github.com/richardwilkes/toolbox/xio/fs/paths" 19 ) 20 21 // Constants for defaults. 22 const ( 23 DefaultMaxSize = 10 * 1024 * 1024 24 DefaultMaxBackups = 1 25 ) 26 27 // DefaultPath returns the default path that will be used. This will use cmdline.AppIdentifier (if set) to better 28 // isolate the log location. 29 func DefaultPath() string { 30 return filepath.Join(paths.AppLogDir(), cmdline.AppCmdName+".log") 31 } 32 33 // Path specifies the file to write logs to. Backup log files will be retained in the same directory. Defaults to the 34 // value of DefaultPath(). 35 func Path(path string) func(*Rotator) error { 36 return func(r *Rotator) error { 37 if path == "" { 38 return errs.New("Must specify a path") 39 } 40 r.path = path 41 return nil 42 } 43 } 44 45 // MaxSize sets the maximum size of the log file before it gets rotated. Defaults to DefaultMaxSize. 46 func MaxSize(maxSize int64) func(*Rotator) error { 47 return func(r *Rotator) error { 48 r.maxSize = maxSize 49 return nil 50 } 51 } 52 53 // MaxBackups sets the maximum number of old log files to retain. Defaults to DefaultMaxBackups. 54 func MaxBackups(maxBackups int) func(*Rotator) error { 55 return func(r *Rotator) error { 56 r.maxBackups = maxBackups 57 return nil 58 } 59 } 60 61 // WithMask sets the mask when creating files, which have the unmasked mode of 0644, and directories, which have the 62 // unmasked mode of 0755. Defaults to 0777. 63 func WithMask(mask os.FileMode) func(*Rotator) error { 64 return func(r *Rotator) error { 65 r.mask = mask 66 return nil 67 } 68 }