Home > OS >  How klog logs to stdout as well as files files
How klog logs to stdout as well as files files

Time:08-31

I'm working on a project that uses the klog library for logging, and I want to be able to write the logs to a **terminal** while being able to write to a **file** for viewing on reboot or downtime.

I tested through the following code, but it can only write to the file and not output at the terminal at the same time

package main

import (
    "flag"
    "k8s.io/klog/v2"
)

func init() {
func init() {
    var fs flag.FlagSet
    klog.InitFlags(&fs)
    fs.Set("logtostderr", "false")
    fs.Set("log_file_max_size", "1000")
    fs.Set("log_file", "/home/test/workspace/test/test.log")
}
}

func main() {
    defer klog.Flush()
    klog.Info("test")
}

How can I get it? I really appreciate any help with this.

CodePudding user response:

If you read the Klog documentation says that it supports a flag, -alsologtostderr:

-alsologtostderr=false
Logs are written to standard error as well as to files.

Seems like that would do you.

  • Related