Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to close files opened when a new FileHandler is created. #156

Open
jamesjmurtagh opened this issue Jul 25, 2019 · 0 comments
Open

Comments

@jamesjmurtagh
Copy link

A project that I'm working on spawns a Logger with a new FileHandler each time a particular action is performed. When attempting to os.Remove() a file that was generated by a log15.FileHandler on Windows 10, an error returned due to the fact that the file was still in use by a process. This error occurs because, as far as I can tell, there are no ways to close a log15.FileHandler, so my own code never releases/closes the file.

Here's how I resolved this issue, although you might have different opinions about how best to implement a way to close FileHandlers.

I exported the closingHandler struct (rename to ClosingHandler). In my own code, I get the handler of the logger I no longer wish to use, with (*log15.Logger).GetHandler(), and perform a type assertion:

func CloseLogger(l log15.Logger) error {
        x, ok := l.GetHandler().(log15.ClosingHandler)
        if ok {
                err := x.Close()
                if err != nil {
                        return err
                }
        }
        return nil
}

Note that this will not work if the FileHandler has been wrapped in another handler, such as MultiHandler. Ideally, a MultiHandler would loop through each handler that it contains and attempt to perform a type assertion as detailed in the code fragment above, closing where possible.

I don't know the full ramifications of performing a close like this. In my situation, I never use a logger after performing this step, so I don't imagine I'll notice anything that occurs as a result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant