-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
862c7ee
commit 30e050a
Showing
5 changed files
with
114 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package cmds | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/docker/docker/pkg/reexec" | ||
"github.com/natefinch/lumberjack" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
type Log struct { | ||
VLevel int | ||
VModule string | ||
LogFile string | ||
AlsoLogToStderr bool | ||
} | ||
|
||
var ( | ||
LogConfig Log | ||
|
||
VLevel = cli.IntFlag{ | ||
Name: "v", | ||
Usage: "Number for the log level verbosity", | ||
Destination: &LogConfig.VLevel, | ||
} | ||
VModule = cli.StringFlag{ | ||
Name: "vmodule", | ||
Usage: "Comma-separated list of pattern=N settings for file-filtered logging", | ||
Destination: &LogConfig.VModule, | ||
} | ||
LogFile = cli.StringFlag{ | ||
Name: "log,l", | ||
Usage: "Log to file", | ||
Destination: &LogConfig.LogFile, | ||
} | ||
AlsoLogToStderr = cli.BoolFlag{ | ||
Name: "alsologtostderr", | ||
Usage: "Log to standard error as well as file (if set)", | ||
Destination: &LogConfig.AlsoLogToStderr, | ||
} | ||
) | ||
|
||
func InitLogging() error { | ||
if LogConfig.LogFile != "" && os.Getenv("_K3S_LOG_REEXEC_") == "" { | ||
return runWithLogging() | ||
} | ||
|
||
if err := checkUnixTimestamp(); err != nil { | ||
return err | ||
} | ||
|
||
setupLogging() | ||
return nil | ||
} | ||
|
||
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 runWithLogging() error { | ||
var ( | ||
l io.Writer | ||
) | ||
l = &lumberjack.Logger{ | ||
Filename: LogConfig.LogFile, | ||
MaxSize: 50, | ||
MaxBackups: 3, | ||
MaxAge: 28, | ||
Compress: true, | ||
} | ||
if LogConfig.AlsoLogToStderr { | ||
l = io.MultiWriter(l, os.Stderr) | ||
} | ||
|
||
args := append([]string{"k3s"}, os.Args[1:]...) | ||
cmd := reexec.Command(args...) | ||
cmd.Env = os.Environ() | ||
cmd.Env = append(cmd.Env, "_K3S_LOG_REEXEC_=true") | ||
cmd.Stderr = l | ||
cmd.Stdout = l | ||
cmd.Stdin = os.Stdin | ||
return cmd.Run() | ||
} | ||
|
||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters