-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
279 additions
and
311 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
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 |
---|---|---|
@@ -1,55 +1,38 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/Azure/azure-container-networking/zaplog" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
) | ||
|
||
type Config struct { | ||
Level zapcore.Level | ||
LogPath string | ||
MaxSizeInMB int | ||
MaxBackups int | ||
Name string | ||
Component string | ||
} | ||
|
||
var Logger *zap.Logger | ||
const ( | ||
maxLogFileSizeInMb = 5 | ||
maxLogFileCount = 8 | ||
) | ||
|
||
// Initializes a Zap logger and returns a cleanup function so logger can be cleaned up from caller | ||
func Initialize(ctx context.Context, cfg *Config) { | ||
Logger = newFileLogger(cfg) | ||
var ( | ||
loggerName string | ||
loggerFile string | ||
) | ||
|
||
go func() { | ||
<-ctx.Done() | ||
err := Logger.Sync() | ||
if err != nil { | ||
fmt.Println("failed to sync logger") | ||
} | ||
}() | ||
var LoggerCfg = &zaplog.Config{ | ||
Level: zapcore.DebugLevel, | ||
LogPath: loggerFile, | ||
MaxSizeInMB: maxLogFileSizeInMb, | ||
MaxBackups: maxLogFileCount, | ||
Name: loggerName, | ||
} | ||
|
||
func newFileLogger(cfg *Config) *zap.Logger { | ||
logFileWriter := zapcore.AddSync(&lumberjack.Logger{ | ||
Filename: cfg.LogPath, | ||
MaxSize: cfg.MaxSizeInMB, | ||
MaxBackups: cfg.MaxBackups, | ||
}) | ||
|
||
encoderConfig := zap.NewProductionEncoderConfig() | ||
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig) | ||
logLevel := cfg.Level | ||
|
||
core := zapcore.NewCore(jsonEncoder, logFileWriter, logLevel) | ||
Logger = zap.New(core) | ||
Logger = Logger.With(zap.Int("pid", os.Getpid())) | ||
Logger = Logger.With(zap.String("component", cfg.Component)) | ||
func InitZapLogCNI(loggerName, loggerFile string) *zap.Logger { | ||
LoggerCfg.Name = loggerName | ||
LoggerCfg.LogPath = LogPath + loggerFile | ||
logger := zaplog.InitZapLog(LoggerCfg) | ||
|
||
return Logger | ||
// only log process id on CNI package | ||
logger = logger.With(zap.Int("pid", os.Getpid())) | ||
logger = logger.With(zap.String("component", "cni")) | ||
return logger | ||
} |
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
package log | ||
|
||
import "go.uber.org/zap" | ||
|
||
func InitializeMock() { | ||
Logger = zap.NewNop() | ||
InitZapLogCNI("azure-vnet", "") | ||
} |
Oops, something went wrong.