From c944ccd079ae3371b1bffed3eae4fe2b5112adf7 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Sat, 24 Nov 2018 18:39:00 +0800 Subject: [PATCH] fix mac os stdout print buffer truncate When set log to console, if fmt.Print larger than 1K the output buffer will be truncate. This problem will be trigger when set log to stdout, beego and soar both print their output into stdout, and this may come into confusing status in mac os. For fixing this problem, we only allow set log to file not. SOAR never write SQL suggest into log. Only developer should be concern about log, common user have no need of looking up log. --- common/config.go | 10 +--------- common/config_test.go | 12 ------------ common/logger.go | 15 ++++----------- 3 files changed, 5 insertions(+), 32 deletions(-) diff --git a/common/config.go b/common/config.go index c4f7954..c0b4c79 100644 --- a/common/config.go +++ b/common/config.go @@ -131,14 +131,6 @@ type Configuration struct { MaxPrettySQLLength int `yaml:"max-pretty-sql-length"` // 超出该长度的SQL会转换成指纹输出 } -// getDefaultLogOutput get default log-output by runtime.GOOS -func getDefaultLogOutput() string { - if runtime.GOOS == "windows" { - return "nul" - } - return os.Stderr.Name() -} - // Config 默认设置 var Config = &Configuration{ OnlineDSN: &dsn{ @@ -178,7 +170,7 @@ var Config = &Configuration{ SpaghettiQueryLength: 2048, AllowDropIndex: false, LogLevel: 3, - LogOutput: getDefaultLogOutput(), + LogOutput: "soar.log", ReportType: "markdown", ReportCSS: "", ReportJavascript: "", diff --git a/common/config_test.go b/common/config_test.go index 7f9a569..48f5a7a 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -19,7 +19,6 @@ package common import ( "flag" "os" - "runtime" "testing" "github.com/kr/pretty" @@ -27,17 +26,6 @@ import ( var update = flag.Bool("update", false, "update .golden files") -func TestGetDefaultLogOutput(t *testing.T) { - output := getDefaultLogOutput() - if runtime.GOOS == "windows" && output != "nul" { - t.Error("windows default -log-output not nul") - } - - if runtime.GOOS != "windows" && output != "/dev/stderr" { - t.Error("default -log-output not /dev/stderr") - } -} - func TestParseConfig(t *testing.T) { err := ParseConfig("") if err != nil { diff --git a/common/logger.go b/common/logger.go index 77059cb..6323fca 100644 --- a/common/logger.go +++ b/common/logger.go @@ -39,17 +39,10 @@ func init() { // LoggerInit Log配置初始化 func LoggerInit() { Log.SetLevel(Config.LogLevel) - if Config.LogOutput == logs.AdapterConsole { - err := Log.SetLogger(logs.AdapterConsole) - if err != nil { - fmt.Println(err.Error()) - } - } else { - func() { _ = Log.DelLogger(logs.AdapterFile) }() - err := Log.SetLogger(logs.AdapterFile, fmt.Sprintf(`{"filename":"%s","level":7,"maxlines":0,"maxsize":0,"daily":false,"maxdays":0}`, Config.LogOutput)) - if err != nil { - fmt.Println(err.Error()) - } + func() { _ = Log.DelLogger(logs.AdapterFile) }() + err := Log.SetLogger(logs.AdapterFile, fmt.Sprintf(`{"filename":"%s","level":7,"maxlines":0,"maxsize":0,"daily":false,"maxdays":0}`, Config.LogOutput)) + if err != nil { + fmt.Println(err.Error()) } } -- GitLab