I have what should be a very simple logger function but while the log file is created, nothing is written to it. I tried several different things but it continues to fail. The function is
func logger(logname string, message string) {
logName := "../planner/log/" + logname + ".log"
var file *os.File
file, err := os.OpenFile(logName, os.O_APPEND|os.O_CREATE, 0755)
if err != nil {
fmt.Println("File", logName, "failed with error:", err)
}
//file.WriteString(message)
fmt.Fprintf(file, message)
fmt.Println("Wrote", message, "to", logName)
file.Sync()
file.Close()
test, err := os.Stat(logName)
size := test.Size()
fmt.Println("File size is", size)
}
You are not getting anything in file because you are not opening it with os.WRONLY
or os.RDWR
. By default. os.OpenFile
returns a buffer with only read permission. You might want to close your file with a defer
in case the code panics with defer file.Close()
.