gitee.com/lh-her-team/common@v1.5.1/log/file-rotatelogs/README.md (about) 1 file-rotatelogs 2 ================== 3 4 Provide an `io.Writer` that periodically rotates log files from within the application. Port of [File::RotateLogs](https://metacpan.org/release/File-RotateLogs) from Perl to Go. 5 6 [](https://travis-ci.org/lestrrat-go/file-rotatelogs) 7 8 [](https://godoc.org/github.com/lestrrat-go/file-rotatelogs) 9 10 11 # SYNOPSIS 12 13 ```go 14 import ( 15 "log" 16 "net/http" 17 18 apachelog "github.com/lestrrat-go/apache-logformat" 19 rotatelogs "github.com/lestrrat-go/file-rotatelogs" 20 ) 21 22 func main() { 23 mux := http.NewServeMux() 24 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ... }) 25 26 logf, err := rotatelogs.New( 27 "/path/to/access_log.%Y%m%d%H%M", 28 rotatelogs.WithLinkName("/path/to/access_log"), 29 rotatelogs.WithMaxAge(24 * time.Hour), 30 rotatelogs.WithRotationTime(time.Hour), 31 ) 32 if err != nil { 33 log.Printf("failed to create rotatelogs: %s", err) 34 return 35 } 36 37 // Now you must write to logf. apache-logformat library can create 38 // a http.Handler that only writes the approriate logs for the request 39 // to the given handle 40 http.ListenAndServe(":8080", apachelog.CombinedLog.Wrap(mux, logf)) 41 } 42 ``` 43 44 # DESCRIPTION 45 46 When you integrate this to into your app, it automatically write to logs that 47 are rotated from within the app: No more disk-full alerts because you forgot 48 to setup logrotate! 49 50 To install, simply issue a `go get`: 51 52 ``` 53 go get github.com/lestrrat-go/file-rotatelogs 54 ``` 55 56 It's normally expected that this library is used with some other 57 logging service, such as the built-in `log` library, or loggers 58 such as `github.com/lestrrat-go/apache-logformat`. 59 60 ```go 61 import( 62 "log" 63 "github.com/lestrrat-go/file-rotatelogs" 64 ) 65 66 func main() { 67 rl, _ := rotatelogs.New("/path/to/access_log.%Y%m%d%H%M") 68 69 log.SetOutput(rl) 70 71 /* elsewhere ... */ 72 log.Printf("Hello, World!") 73 } 74 ``` 75 76 OPTIONS 77 ==== 78 79 ## Pattern (Required) 80 81 The pattern used to generate actual log file names. You should use patterns 82 using the strftime (3) format. For example: 83 84 ```go 85 rotatelogs.New("/var/log/myapp/log.%Y%m%d") 86 ``` 87 88 ## Clock (default: rotatelogs.Local) 89 90 You may specify an object that implements the roatatelogs.Clock interface. 91 When this option is supplied, it's used to determine the current time to 92 base all of the calculations on. For example, if you want to base your 93 calculations in UTC, you may specify rotatelogs.UTC 94 95 ```go 96 rotatelogs.New( 97 "/var/log/myapp/log.%Y%m%d", 98 rotatelogs.WithClock(rotatelogs.UTC), 99 ) 100 ``` 101 102 ## Location 103 104 This is an alternative to the `WithClock` option. Instead of providing an 105 explicit clock, you can provide a location for you times. We will create 106 a Clock object that produces times in your specified location, and configure 107 the rotatelog to respect it. 108 109 ## LinkName (default: "") 110 111 Path where a symlink for the actual log file is placed. This allows you to 112 always check at the same location for log files even if the logs were rotated 113 114 ```go 115 rotatelogs.New( 116 "/var/log/myapp/log.%Y%m%d", 117 rotatelogs.WithLinkName("/var/log/myapp/current"), 118 ) 119 ``` 120 121 ``` 122 // Else where 123 $ tail -f /var/log/myapp/current 124 ``` 125 126 Links that share the same parent directory with the main log path will get a 127 special treatment: namely, linked paths will be *RELATIVE* to the main log file. 128 129 | Main log file name | Link name | Linked path | 130 |---------------------|---------------------|-----------------------| 131 | /path/to/log.%Y%m%d | /path/to/log | log.YYYYMMDD | 132 | /path/to/log.%Y%m%d | /path/to/nested/log | ../log.YYYYMMDD | 133 | /path/to/log.%Y%m%d | /foo/bar/baz/log | /path/to/log.YYYYMMDD | 134 135 If not provided, no link will be written. 136 137 ## RotationTime (default: 86400 sec) 138 139 Interval between file rotation. By default logs are rotated every 86400 seconds. 140 Note: Remember to use time.Duration values. 141 142 ```go 143 // Rotate every hour 144 rotatelogs.New( 145 "/var/log/myapp/log.%Y%m%d", 146 rotatelogs.WithRotationTime(time.Hour), 147 ) 148 ``` 149 150 ## MaxAge (default: 7 days) 151 152 Time to wait until old logs are purged. By default no logs are purged, which 153 certainly isn't what you want. 154 Note: Remember to use time.Duration values. 155 156 ```go 157 // Purge logs older than 1 hour 158 rotatelogs.New( 159 "/var/log/myapp/log.%Y%m%d", 160 rotatelogs.WithMaxAge(time.Hour), 161 ) 162 ``` 163 164 ## RotationCount (default: -1) 165 166 The number of files should be kept. By default, this option is disabled. 167 168 Note: MaxAge should be disabled by specifing `WithMaxAge(-1)` explicitly. 169 170 ```go 171 // Purge logs except latest 7 files 172 rotatelogs.New( 173 "/var/log/myapp/log.%Y%m%d", 174 rotatelogs.WithMaxAge(-1), 175 rotatelogs.WithRotationCount(7), 176 ) 177 ``` 178 179 ## Handler (default: nil) 180 181 Sets the event handler to receive event notifications from the RotateLogs 182 object. Currently only supported event type is FiledRotated 183 184 ```go 185 rotatelogs.New( 186 "/var/log/myapp/log.%Y%m%d", 187 rotatelogs.Handler(rotatelogs.HandlerFunc(func(e Event) { 188 if e.Type() != rotatelogs.FileRotatedEventType { 189 return 190 } 191 192 // Do what you want with the data. This is just an idea: 193 storeLogFileToRemoteStorage(e.(*FileRotatedEvent).PreviousFile()) 194 })), 195 ) 196 ``` 197 198 ## ForceNewFile 199 200 Ensure a new file is created every time New() is called. If the base file name 201 already exists, an implicit rotation is performed. 202 203 ```go 204 rotatelogs.New( 205 "/var/log/myapp/log.%Y%m%d", 206 rotatelogs.ForceNewFile(), 207 ) 208 ``` 209 210 # Rotating files forcefully 211 212 If you want to rotate files forcefully before the actual rotation time has reached, 213 you may use the `Rotate()` method. This method forcefully rotates the logs, but 214 if the generated file name clashes, then a numeric suffix is added so that 215 the new file will forcefully appear on disk. 216 217 For example, suppose you had a pattern of '%Y.log' with a rotation time of 218 `86400` so that it only gets rotated every year, but for whatever reason you 219 wanted to rotate the logs now, you could install a signal handler to 220 trigger this rotation: 221 222 ```go 223 rl := rotatelogs.New(...) 224 225 signal.Notify(ch, syscall.SIGHUP) 226 227 go func(ch chan os.Signal) { 228 <-ch 229 rl.Rotate() 230 }() 231 ``` 232 233 And you will get a log file name in like `2018.log.1`, `2018.log.2`, etc.