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