-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor the logger module (#646)
* refactor: refactor the logger module * fix: supplement the previous commit * style: change name of pkg/logger/controller * style: supplementary function annotation * style: polish function annotation
- Loading branch information
Showing
6 changed files
with
218 additions
and
81 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,127 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 logger | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// logController governs the logging output or configuration changes throughout the entire project. | ||
type logController struct { | ||
mu sync.RWMutex | ||
|
||
logger *logger | ||
} | ||
|
||
// setLoggerLevel safely changes the log level in a concurrent manner. | ||
func (c *logController) setLoggerLevel(level string) bool { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
lvl := c.parseLevel(level) | ||
if lvl == nil { | ||
return false | ||
} | ||
|
||
c.logger.config.Level = *lvl | ||
l, _ := c.logger.config.Build() | ||
c.logger = &logger{SugaredLogger: l.Sugar(), config: c.logger.config} | ||
return true | ||
} | ||
|
||
// updateLogger safely modifies the log object in a concurrent manner. | ||
func (c *logController) updateLogger(l *logger) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
c.logger = l | ||
} | ||
|
||
func (c *logController) debug(args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Debug(args...) | ||
} | ||
|
||
func (c *logController) info(args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Info(args...) | ||
} | ||
|
||
func (c *logController) warn(args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Warn(args...) | ||
} | ||
|
||
func (c *logController) error(args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Error(args...) | ||
} | ||
|
||
func (c *logController) debugf(fmt string, args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Debugf(fmt, args...) | ||
} | ||
|
||
func (c *logController) infof(fmt string, args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Infof(fmt, args...) | ||
} | ||
|
||
func (c *logController) warnf(fmt string, args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Warnf(fmt, args...) | ||
} | ||
|
||
func (c *logController) errorf(fmt string, args ...interface{}) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
c.logger.Errorf(fmt, args...) | ||
} | ||
|
||
// parseLevel is used to parse the level of the log. | ||
func (c *logController) parseLevel(level string) *zap.AtomicLevel { | ||
var lvl zapcore.Level | ||
switch strings.ToLower(level) { | ||
case "debug": | ||
lvl = zapcore.DebugLevel | ||
case "info": | ||
lvl = zapcore.InfoLevel | ||
case "warn": | ||
lvl = zapcore.WarnLevel | ||
case "error": | ||
lvl = zapcore.ErrorLevel | ||
case "panic": | ||
lvl = zapcore.PanicLevel | ||
case "fatal": | ||
lvl = zapcore.FatalLevel | ||
default: | ||
return nil | ||
} | ||
|
||
al := zap.NewAtomicLevelAt(lvl) | ||
return &al | ||
} |
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
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,59 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 logger | ||
|
||
// TripleLogger does not add user-defined log fields, so there will be no pointer | ||
// reference issues after updates, ensuring that all references are under control. | ||
type TripleLogger struct { | ||
} | ||
|
||
func GetTripleLogger() *TripleLogger { | ||
return &TripleLogger{} | ||
} | ||
|
||
func (l *TripleLogger) Info(args ...interface{}) { | ||
control.info(args...) | ||
} | ||
|
||
func (l *TripleLogger) Warn(args ...interface{}) { | ||
control.warn(args...) | ||
} | ||
|
||
func (l *TripleLogger) Error(args ...interface{}) { | ||
control.error(args...) | ||
} | ||
|
||
func (l *TripleLogger) Debug(args ...interface{}) { | ||
control.debug(args...) | ||
} | ||
|
||
func (l *TripleLogger) Infof(fmt string, args ...interface{}) { | ||
control.infof(fmt, args...) | ||
} | ||
|
||
func (l *TripleLogger) Warnf(fmt string, args ...interface{}) { | ||
control.warnf(fmt, args...) | ||
} | ||
|
||
func (l *TripleLogger) Errorf(fmt string, args ...interface{}) { | ||
control.errorf(fmt, args...) | ||
} | ||
|
||
func (l *TripleLogger) Debugf(fmt string, args ...interface{}) { | ||
control.debugf(fmt, args...) | ||
} |