-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exportable custom plain logger (#1171)
* Add exportable custom plain logger * fix for ci * add copyright * remove unused function * Change the log directory to current directory --------- Co-authored-by: chenyarong <[email protected]>
- Loading branch information
1 parent
b5f08e1
commit d301c81
Showing
4 changed files
with
176 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2017, MegaEase | ||
* All rights reserved. | ||
* | ||
* 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:https://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 logger | ||
|
||
import ( | ||
"sync" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
var lh *logHub | ||
|
||
func init() { | ||
lh = &logHub{ | ||
loggers: make(map[string]*zap.SugaredLogger), | ||
mu: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
type logHub struct { | ||
loggers map[string]*zap.SugaredLogger | ||
|
||
mu *sync.RWMutex | ||
} | ||
|
||
// register registers a logger with name. | ||
func (lh *logHub) register(name string, logger *zap.SugaredLogger) { | ||
lh.mu.Lock() | ||
defer lh.mu.Unlock() | ||
|
||
lh.loggers[name] = logger | ||
} | ||
|
||
// sync syncs all loggers. | ||
func (lh *logHub) sync() { | ||
lh.mu.RLock() | ||
defer lh.mu.RUnlock() | ||
|
||
for _, logger := range lh.loggers { | ||
logger.Sync() | ||
} | ||
} |
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,79 @@ | ||
/* | ||
* Copyright (c) 2017, MegaEase | ||
* All rights reserved. | ||
* | ||
* 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:https://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 logger | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/megaease/easegress/v2/pkg/option" | ||
) | ||
|
||
func TestMustPlainLogger(t *testing.T) { | ||
opt := &option.Options{AbsLogDir: "."} | ||
defer func() { | ||
if rv := recover(); rv != nil { | ||
t.Errorf("mustPlainLogger() panic: %v", rv) | ||
} | ||
}() | ||
|
||
MustNewPlainLogger(opt, "test.log", 1) | ||
_, err := os.Stat("test.log") | ||
if err == nil { | ||
return | ||
} | ||
|
||
if os.IsNotExist(err) { | ||
t.Errorf("mustPlainLogger() failed: %v", err) | ||
} | ||
} | ||
|
||
func TestMustPlainLoggerPanic(t *testing.T) { | ||
opt := &option.Options{AbsLogDir: "."} | ||
defer func() { | ||
if rv := recover(); rv != nil { | ||
t.Logf("mustPlainLogger() panic: %v", rv) | ||
} | ||
}() | ||
|
||
MustNewPlainLogger(opt, "test.log", 0) | ||
_, err := os.Stat("test.log") | ||
if err == nil { | ||
return | ||
} | ||
|
||
if os.IsNotExist(err) { | ||
t.Errorf("mustPlainLogger() failed: %v", err) | ||
} | ||
} | ||
|
||
func TestMustPlainLoggerWrite(t *testing.T) { | ||
opt := &option.Options{AbsLogDir: "."} | ||
defer func() { | ||
if rv := recover(); rv != nil { | ||
t.Errorf("mustPlainLogger() panic: %v", rv) | ||
} | ||
}() | ||
|
||
l := MustNewPlainLogger(opt, "test.log", 0) | ||
l.Errorf("test error") | ||
l.Info("test info") | ||
l.Debug("test debug") | ||
l.Sync() | ||
t.Logf("mustPlainLogger() success") | ||
} |