Saturday, April 20, 2024
HomeGolanglog.Println in a excessive efficiency software? - Getting Assist

log.Println in a excessive efficiency software? – Getting Assist


I’m writing a excessive efficiency software which is able to get hit onerous through the community and must implement logging (to a textual content file) as effectively.

Is it a good suggestion to have a buffer with a separate goroutine slowly draining the buffer to a file, or is log.Println (to a file) already optimised sufficient to keep away from this being crucial?

Wanting on the implementation of log.Println, I see it simply delegates to fmt.Sprintln and finally calls (*log.Logger).Output which simply calls Write on the logger’s configured output right here. There doesn’t appear to be any form of optimization. If the underlying io.Author implementation doesn’t buffer its writes, this could possibly be a bottleneck should you’re doing a lot of logging. Be aware additionally that *log.Logger has a sync.Mutex to verify writes are atomic, so just one log message might be written at a time.

You might strive utilizing (*log.Logger).SetOutput to set the logging output to a *bufio.Author which ought to let most logging operations full extra rapidly, nonetheless it’s important to make sure that to name (*bufio.Author).Flush so that each one the log messages really get flushed to the underlying author. Perhaps it has to go in a defer assertion in case of a panic.

How do you propose to do that? You might presumably use (*log.Logger).SetOutput to set the output to, e.g. a *bytes.Buffer, however then how would you talk to a different goroutine to flush the buffer? Perhaps you can use a wrapper round (*bytes.Buffer).Write that checks the buffer capability and makes use of, e.g., a channel to sign to the author goroutine to flush the buffer, however then you definitely additionally must await the flush to finish. I don’t suppose that will get you a lot over the *bufio.Author different apart from permitting the final goroutine that crammed the buffer to proceed whereas the author goroutine really flushes the buffer. Nonetheless, the subsequent name from any goroutine to any of the logging capabilities will block till that author goroutine completes the flush.

You probably have loads of requests with loads of log occasions per request, maybe you can use separate *log.Logger cases with separate (maybe buffered) outputs. It could end in separate information which can or is probably not a deal-breaker, however the efficiency of writing out to the information wouldn’t be bottlenecked by one *log.Logger‘s mutex or by the underlying author.

This subject was mechanically closed 90 days after the final reply. New replies are not allowed.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments