From 4eb8f1a0ee280720bef0de1fd69c793b8a269472 Mon Sep 17 00:00:00 2001 From: Somefive Date: Fri, 30 Jul 2021 18:39:34 +0800 Subject: [PATCH] suppress dirty logs during import libraries (#1991) Co-authored-by: Yin Da --- go.mod | 1 + references/a/preimport/preimport.go | 42 +++++++++++++++++++++++++++++ references/cmd/cli/main.go | 2 ++ 3 files changed, 45 insertions(+) create mode 100644 references/a/preimport/preimport.go diff --git a/go.mod b/go.mod index 04d5546b8..11557ba5e 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 github.com/briandowns/spinner v1.11.1 + github.com/containerd/containerd v1.3.2 github.com/coreos/prometheus-operator v0.41.1 github.com/crossplane/crossplane-runtime v0.10.0 github.com/davecgh/go-spew v1.1.1 diff --git a/references/a/preimport/preimport.go b/references/a/preimport/preimport.go new file mode 100644 index 000000000..db4718b8e --- /dev/null +++ b/references/a/preimport/preimport.go @@ -0,0 +1,42 @@ +/* + Copyright 2021. The KubeVela Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package preimport + +import ( + "flag" + + "github.com/containerd/containerd/log" + "k8s.io/klog" +) + +var _flagSet *flag.FlagSet + +// disable logging during import +func init() { + lvl, _ := log.ParseLevel("panic") + log.L.Logger.SetLevel(lvl) + _flagSet = flag.CommandLine + klog.InitFlags(_flagSet) + _ = _flagSet.Set("v", "-1") +} + +// Start recover logging +func Start() { + lvl, _ := log.ParseLevel("info") + log.L.Logger.SetLevel(lvl) + _ = _flagSet.Set("v", "0") +} diff --git a/references/cmd/cli/main.go b/references/cmd/cli/main.go index 5dd4ab21a..1e5557762 100644 --- a/references/cmd/cli/main.go +++ b/references/cmd/cli/main.go @@ -21,10 +21,12 @@ import ( "os" "time" + "github.com/oam-dev/kubevela/references/a/preimport" "github.com/oam-dev/kubevela/references/cli" ) func main() { + preimport.Start() rand.Seed(time.Now().UnixNano()) command := cli.NewCommand()