I've been reading around how golang writes to a file, and this stack overflow question and this reddit question highlights the fact Go doesn't gurantee atomicity when writing to a file system. Although I didn't see any interleaved writes (which I guess could be due to writev(2)), I didn't want to risk it so I built a a simple Go interface to do that.
I'm not super proficient with Go, but I'd like to understand how this code can be improved with best practices and on potential issues that may arise when using it.
package main
import (
"fmt"
"io"
"os"
)
// FileLogger defines the methods to log to file
type FileLogger interface {
Write(s string)
}
type fileLogger struct {
stream chan string
writer io.Writer
}
func (l *fileLogger) run() {
for {
select {
case s := <-l.stream:
_, err := l.writer.Write([]byte(s))
if err != nil {
fmt.Println("Error writing to file: ", err.Error())
}
}
}
}
func (l *fileLogger) Write(s string) {
l.stream <- s
}
// NewFileLogger returns a new FileLogger
func NewFileLogger() FileLogger {
file, err := os.OpenFile(
"logs/logs.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666,
)
if err != nil {
panic(err)
}
f := &fileLogger{make(chan string), file}
go f.run()
return f
}
I could for instance make it conform to the io.Writer interface, but I'm not sure what benefits there are of that?
Would there be an advantage to mutex locks here using the sync package?
1 Answer 1
Go doesn't guarantee atomicity ... so I built a simple Go interface to do that
Okay, but is your implementation atomic? The only atomic operations guaranteed to be atomic in Go are through the sync/atomic
package (or things like func (*Cond) Wait
under sync
).
If you need true atomic write, use the atomic package. However, using log
is usually sufficient.
Your implementation looks like it's trying to be concurrent, not atomic. Rather than rolling your own concurrent writer interface, use the standard API.
You're right, fmt
(& os
write functions, etc.) do not provide concurrency. However, the log
package does.
You can see that they use mutex locks for the Output
function, which is used by almost everything else.
This should perform nearly identical to your use case, because you open the file with O_APPEND
, and log
appends.
So open a file, and pass it to log.New()
.
For example:
package main
import (
"log"
"os"
)
func main() {
f, err := os.OpenFile("testfile", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
logger := log.New(f, "", 0)
logger.Output(2, "wow")
}
-
1\$\begingroup\$ thanks for the response! Is it not atomic if I have a single go routine that handles the writes using the for select? \$\endgroup\$Rambatino– Rambatino2018年12月18日 00:55:33 +00:00Commented Dec 18, 2018 at 0:55
-
2\$\begingroup\$ I see, it's concurrent... \$\endgroup\$Rambatino– Rambatino2018年12月18日 00:55:52 +00:00Commented Dec 18, 2018 at 0:55
-
2\$\begingroup\$ I think I have some more reading to do... \$\endgroup\$Rambatino– Rambatino2018年12月18日 00:56:12 +00:00Commented Dec 18, 2018 at 0:56