sirupsen / logrus

Structured, pluggable logging for Go.
MIT License
24.8k stars 2.27k forks source link

Skip debug level log from console output but should present in log file #1386

Closed tarun818 closed 1 year ago

tarun818 commented 1 year ago

Hi ,

In my application I want to show only info log level at console and want to skip all debug log messages from console, but in log file I want both info and debug logs. e.g.

var logrusLogger *logrus.Logger
var f *os.File

func main() {
    f, err := os.OpenFile("mylog.log", os.O_WRONLY|os.O_CREATE, 0644)
    if err != nil {
        fmt.Println("Failed to create logfile mylog.log")
        panic(err)
    }

    logrusLogger = logrus.New()
    logrusLogger.SetFormatter(&logrus.TextFormatter{
        ForceColors:      false, //Enable this for coloured output
        DisableColors:    false,
        TimestampFormat:  "2006-01-02 15:04:05.000",
        DisableTimestamp: false,
        FullTimestamp:    true,
    })
    logLevel, err := logrus.ParseLevel(os.Getenv("MY_LOG_LEVEL"))
    if err != nil {
        logLevel = logrus.DebugLevel
    }
    logrusLogger.SetLevel(logLevel)
    logrusLogger.Out = io.MultiWriter(f, os.Stdout) //print logs in console as well inside tcxctl_<date>_.log file

    logrusLogger.Info("=> This should print on console as well as inside mylog.log file <=")
    logrusLogger.Debug("=>This should not print on console but should be inside mylog.log file <=")
}

I am not sure how I can achieve this using logrus, If anybody have any idea please share.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 14 days since being marked as stale.