You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k3s/pkg/cli/cmds/log.go

80 lines
1.6 KiB

5 years ago
package cmds
import (
"flag"
"fmt"
"strconv"
"sync"
5 years ago
"time"
"github.com/urfave/cli"
5 years ago
)
type Log struct {
VLevel int
VModule string
LogFile string
AlsoLogToStderr bool
}
var (
LogConfig Log
VLevel = cli.IntFlag{
Name: "v",
Usage: "(logging) Number for the log level verbosity",
5 years ago
Destination: &LogConfig.VLevel,
}
VModule = cli.StringFlag{
Name: "vmodule",
Usage: "(logging) Comma-separated list of pattern=N settings for file-filtered logging",
5 years ago
Destination: &LogConfig.VModule,
}
LogFile = cli.StringFlag{
Name: "log,l",
Usage: "(logging) Log to file",
5 years ago
Destination: &LogConfig.LogFile,
}
AlsoLogToStderr = cli.BoolFlag{
Name: "alsologtostderr",
Usage: "(logging) Log to standard error as well as file (if set)",
5 years ago
Destination: &LogConfig.AlsoLogToStderr,
}
logSetupOnce sync.Once
5 years ago
)
func InitLogging() error {
var rErr error
logSetupOnce.Do(func() {
if err := forkIfLoggingOrReaping(); err != nil {
rErr = err
return
}
if err := checkUnixTimestamp(); err != nil {
rErr = err
return
}
setupLogging()
})
return rErr
5 years ago
}
func checkUnixTimestamp() error {
timeNow := time.Now()
// check if time before 01/01/1980
if timeNow.Before(time.Unix(315532800, 0)) {
return fmt.Errorf("server time isn't set properly: %v", timeNow)
}
return nil
}
func setupLogging() {
flag.Set("v", strconv.Itoa(LogConfig.VLevel))
flag.Set("vmodule", LogConfig.VModule)
flag.Set("alsologtostderr", strconv.FormatBool(Debug))
flag.Set("logtostderr", strconv.FormatBool(!Debug))
5 years ago
}