2
\$\begingroup\$

This works, but there must be a better, more memory-efficient way to do it.

log.Init checks to make sure log file exists, and creates it if it doesn't. It also sets a maximum file size for the log file.

package log

import (
    "errors"
    "fmt"
    "io/ioutil"
    "os"
    "time"
)

var logFile string
var fileLimit int

func Init(dir, name string, l int) error {
    fileLimit = l
    p := dir + "/" + name
    info, err := os.Stat(p)
    if err != nil {
        if os.IsNotExist(err) {
            err := os.MkdirAll(dir, os.ModePerm)
            if err != nil {
                return errors.New("Unable to create directory: " + err.Error())
            }

            _, err = os.Create(p)
            if err != nil {
                return errors.New("Unable to create log file: " + err.Error())
            }

            logFile = p
            return nil
        }

        return errors.New("Unable to stat log file: " + err.Error())
    }

    if info.IsDir() {
        return errors.New("File name is directory")
    }

    logFile = p
    return nil
}

func Append(message string) {
    file, err := os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY, 0600)
    if err != nil {
        fmt.Printf("Unable to open log file: %s\n%s\n", err, message)
        return
    }
    defer file.Close()

    if _, err = file.WriteString("[" + time.Now().Format(time.ANSIC) + "] " + message + "\n"); err != nil {
        fmt.Printf("Unable to write to log file: %s\n%s\n", err, message)
        return
    }

    fi, err := os.Stat(logFile)
    if err != nil {
        fmt.Println("Unable to stat log file: " + err.Error())
        return
    }

    fileSize := int(fi.Size())
    if fileSize > fileLimit {
        b, err := ioutil.ReadFile(logFile)
        if err != nil {
            fmt.Println("Unable to read log file" + err.Error())
            return
        }

        err = file.Truncate(0)
        if err != nil {
            fmt.Println("Unable to truncate log file" + err.Error())
            return
        }

        new := b[fileSize-fileLimit:]
        _, err = file.Write(new)
        if err != nil {
            fmt.Println("Unable to write to log file" + err.Error())
            return
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.