natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.76k stars 585 forks source link

RFC: Option to not move rotated files #125

Closed ahshah closed 3 years ago

ahshah commented 3 years ago

I was wondering if an option to not move rotated files would be a patch you'd consider. I have external tooling that moves and compresses the file (thereby avoiding #124), and I'd rather not have errors show up in stdout. (lumberjack.go:223)

Looks like pretty straightforward work!

ahshah commented 3 years ago

@natefinch would love to hear back when you get a minute!

natefinch commented 3 years ago

I'm not sure what you mean about not moving rotated files. If you are writing to log.txt, and it reaches maximum capacity... what would you have lumberjack do? Currently lumberjack closes that file, renames it to include a timestamp, and starts writing a new log.txt.

ahshah commented 3 years ago

I've got apex/log writing to a log file via lumberjack. I've set the max file size to max int32, to circumvent lumber jack auto rotation by size. As mentioned I have an external tool thats watching the log file and doing the rotation; When the file reaches a particular size it is rotated and my app is signaled; When receiving the signal i can easily use lumberjack to close and reopen the file in a thread safe manner. I'm really only using it for its thread safety, something not built into apex/log.

Maybe it doesn't make sense to use lumberjack in this way, and I should recreate the write function, pretty much exactly as it's implemented in lumberjack.go:135 (less the auto rotate stuff)

natefinch commented 3 years ago

Yeah, that's pretty much outside the scope of what lumberjack is for.

I would be careful about having an external tool rotating the log that your server is writing to. The external tool could start to rotate the file at the same time your server tries to write to it. The lock in lumberjack prevents that, but that won't work for the external tool, unless you have some lock inside your server than knows when the external tool is rotating the file.

ahshah commented 3 years ago

Thanks for your comments. My understanding is that once the file descriptor is open, the file can be moved anywhere on the FS (dependent, I suppose, on the FS implementation), or even removed, with out much repercussions. I'll close this ticket!

phuslu commented 3 years ago

Hi @ahshah , you're not alone. I also suggest similar behavior on https://github.com/natefinch/lumberjack/issues/115#issuecomment-739835573 Using symlink instead renaming during rotation, it will be more friendly for external tools(but hit the human friendliness), so it on the repo owner.

davidnewhall commented 3 years ago

Pretty sure you can do this with rotatorr. The default is to not rotate anything.

package main

import (
    "log"
    "os"
    "os/signal"
    "syscall"

    "golift.io/rotatorr"
    "golift.io/rotatorr/timerotator"
)

func main() {
    rotator := rotatorr.NewMust(&rotatorr.Config{
        Filepath: "/var/log/service.log",
        Rotatorr: &timerotator.Layout{FileCount: 10},
    })
    log.SetOutput(rotator)

    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, syscall.SIGHUP)

    for range sigChan {
        if _, err := rotator.Rotate(); err != nil {
            println("rotation error:", err)
        }
    }
}

EDIT: Well, no, this clearly does not do what you want. hm. Not sure what would. :(