Logrus: Not writing to file

Created on 20 Feb 2018  路  2Comments  路  Source: sirupsen/logrus

Hello,

I have encountered with strange behavior I was not able to find explanation for it.
Considering following program:

package main

import (
    "github.com/sirupsen/logrus"
    "os"
    "fmt"
)

var (
    logger = logrus.New()
)

func init() {
    f, err := os.OpenFile("../logs/logrus.log", os.O_WRONLY | os.O_CREATE, 0755)

    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to initialize log file %s", err)
        os.Exit(1)
    }

    // Out to file
    logger.Out = f

    // Level
    logger.Level = logrus.DebugLevel
}

func main() {
    logger.Info("Application start")
}

The issue is that no matter how many times I will run this program it will log Application start only once. After deleting log it creates new file and again writes only single time to it and for the next times when I run this program it will not write to log.

Is it normal behavior?

Most helpful comment

@deividaspetraitis Your application is over-writing the file everytime it starts. You need to add os.O_APPEND flag for it to append to existing file:

f, err := os.OpenFile("logrus.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)

All 2 comments

@deividaspetraitis Your application is over-writing the file everytime it starts. You need to add os.O_APPEND flag for it to append to existing file:

f, err := os.OpenFile("logrus.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)

@shahidhk worked like a charm, thank you!

Was this page helpful?
0 / 5 - 0 ratings