I thought by returning a pointer to the object the caller should obtain write access? This is (or will be) part of a much larger existing application and returns no building or vetting errors or warnings. What am I missing? The console option works just fine.
I guess the problem is defer errorlog.Close() within New(). The deferred function call runs when the surrounding function exits, so after calling New(false), the file is already closed.
Wow. I hadn’t thought of that. It explains why it worked in the previous, monolithic version of this application and not in the current package-based layout. Thanks, I will drop that defer when I get back to the code tomorrow night and see how it behaves. Easy enough to put in a hook to close the file when the application exits.
Odd that I don’t see a run-time error, though, trying to write to a closed file?
Yes removing the deferred call to close seems to have done the trick. Also, for anyone curious, one library I use needs to be able to write to the same log created in main(). I solved this by making a pointer to a log object part of the library, and setting it up with a New() function:
logger, err := customlog.New(false)
if err != nil {
fmt.Printf("error = %v\n", err)
os.Exit(1) // should panic and notify
}
// ....
request, err := requests.New(logger.Custom)
Seems to work well, and will allow me to pass around the logger pointer for “write” HTTP handlers that can’t gracefully return an error status to main().