MCPcopy
hub / github.com/natefinch/lumberjack / openExistingOrNew

Method openExistingOrNew

lumberjack.go:264–289  ·  view source on GitHub ↗

openExistingOrNew opens the logfile if it exists and if the current write would not put it over MaxSize. If there is no such file or the write would put it over the MaxSize, a new file is created.

(writeLen int)

Source from the content-addressed store, hash-verified

262// would not put it over MaxSize. If there is no such file or the write would
263// put it over the MaxSize, a new file is created.
264func (l *Logger) openExistingOrNew(writeLen int) error {
265 l.mill()
266
267 filename := l.filename()
268 info, err := osStat(filename)
269 if os.IsNotExist(err) {
270 return l.openNew()
271 }
272 if err != nil {
273 return fmt.Errorf("error getting log file info: %s", err)
274 }
275
276 if info.Size()+int64(writeLen) >= l.max() {
277 return l.rotate()
278 }
279
280 file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
281 if err != nil {
282 // if we fail to open the old log file for some reason, just ignore
283 // it and open a new log file.
284 return l.openNew()
285 }
286 l.file = file
287 l.size = info.Size()
288 return nil
289}
290
291// filename generates the name of the logfile from the current time.
292func (l *Logger) filename() string {

Callers 1

WriteMethod · 0.95

Calls 5

millMethod · 0.95
filenameMethod · 0.95
openNewMethod · 0.95
maxMethod · 0.95
rotateMethod · 0.95

Tested by

no test coverage detected