Skip to content

Commit 48c5c47

Browse files
author
auxten
committed
use NilFormatter to discard log, because assigning entry.Logger.Out = ioutil.Discard will cause all subsequent log missing
1 parent b33de07 commit 48c5c47

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

utils/log/logwrapper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package log
1919
import (
2020
"fmt"
2121
"io"
22-
"io/ioutil"
2322
"path/filepath"
2423
"runtime"
2524
"strings"
@@ -68,7 +67,9 @@ func (hook *CallerHook) Fire(entry *logrus.Entry) error {
6867
if len(fields) > 0 {
6968
level, ok := PkgDebugLogFilter[fields[0]]
7069
if ok && entry.Level > level {
71-
entry.Logger.Out = ioutil.Discard
70+
nilLogger := logrus.New()
71+
nilLogger.Formatter = &NilFormatter{}
72+
entry.Logger = nilLogger
7273
return nil
7374
}
7475
}

utils/log/nillogger.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2018 The ThunderDB Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package log
18+
19+
import (
20+
"github.com/sirupsen/logrus"
21+
)
22+
23+
// NilFormatter just discards the log entry
24+
type NilFormatter struct{}
25+
26+
// Format just return nil, nil for discarding log entry
27+
func (f *NilFormatter) Format(entry *logrus.Entry) ([]byte, error) {
28+
return nil, nil
29+
}

0 commit comments

Comments
 (0)