Skip to content

Commit

Permalink
add lumberjack logs can be divided by size and time
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin0325 committed Jan 24, 2022
1 parent 05958d8 commit 7481925
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 143 deletions.
4 changes: 2 additions & 2 deletions clients/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func setConfig(param vo.NacosClientParam) (iClient nacos_client.INacosClient, er
client := &nacos_client.NacosClient{}
if param.ClientConfig == nil {
// default clientConfig
client.SetClientConfig(constant.ClientConfig{})
_ = client.SetClientConfig(constant.ClientConfig{})
} else {
client.SetClientConfig(*param.ClientConfig)
_ = client.SetClientConfig(*param.ClientConfig)
}

if len(param.ServerConfigs) == 0 {
Expand Down
2 changes: 0 additions & 2 deletions clients/client_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func TestSetConfigClient(t *testing.T) {
constant.WithNotLoadCacheAtStart(true),
constant.WithLogDir("/tmp/nacos/log"),
constant.WithCacheDir("/tmp/nacos/cache"),
constant.WithRotateTime("1h"),
constant.WithMaxAge(3),
constant.WithLogLevel("debug"),
)

Expand Down
16 changes: 2 additions & 14 deletions clients/config_client/config_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,8 @@ func NewConfigClient(nc nacos_client.INacosClient) (*ConfigClient, error) {
if err != nil {
return config, err
}
loggerConfig := logger.Config{
Level: clientConfig.LogLevel,
OutputPath: clientConfig.LogDir,
RotationTime: clientConfig.RotateTime,
MaxAge: clientConfig.MaxAge,
}
if clientConfig.LogSampling != nil {
loggerConfig.Sampling = &logger.SamplingConfig{
Initial: clientConfig.LogSampling.Initial,
Thereafter: clientConfig.LogSampling.Thereafter,
Tick: clientConfig.LogSampling.Tick,
}
}
err = logger.InitLogger(loggerConfig)

err = logger.InitLogger(logger.BuildLoggerConfig(clientConfig))
if err != nil {
return config, err
}
Expand Down
8 changes: 0 additions & 8 deletions clients/nacos_client/nacos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,10 @@ func (client *NacosClient) SetClientConfig(config constant.ClientConfig) (err er
config.UpdateThreadNum = 20
}

if len(config.RotateTime) == 0 {
config.RotateTime = "24h"
}

if len(config.LogLevel) == 0 {
config.LogLevel = "info"
}

if config.MaxAge <= 0 {
config.MaxAge = 3
}

if config.CacheDir == "" {
config.CacheDir = file.GetCurrentPath() + string(os.PathSeparator) + "cache"
}
Expand Down
16 changes: 2 additions & 14 deletions clients/naming_client/naming_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,8 @@ func NewNamingClient(nc nacos_client.INacosClient) (NamingClient, error) {
if err != nil {
return naming, err
}
loggerConfig := logger.Config{
Level: clientConfig.LogLevel,
OutputPath: clientConfig.LogDir,
RotationTime: clientConfig.RotateTime,
MaxAge: clientConfig.MaxAge,
}
if clientConfig.LogSampling != nil {
loggerConfig.Sampling = &logger.SamplingConfig{
Initial: clientConfig.LogSampling.Initial,
Thereafter: clientConfig.LogSampling.Thereafter,
Tick: clientConfig.LogSampling.Tick,
}
}
err = logger.InitLogger(loggerConfig)

err = logger.InitLogger(logger.BuildLoggerConfig(clientConfig))
if err != nil {
return naming, err
}
Expand Down
23 changes: 7 additions & 16 deletions common/constant/client_config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func NewClientConfig(opts ...ClientOption) *ClientConfig {
NotLoadCacheAtStart: false,
UpdateCacheWhenEmpty: false,
LogDir: file.GetCurrentPath() + string(os.PathSeparator) + "log",
RotateTime: "24h",
MaxAge: 3,
LogLevel: "info",
}

Expand Down Expand Up @@ -153,20 +151,6 @@ func WithLogDir(logDir string) ClientOption {
}
}

// WithRotateTime ...
func WithRotateTime(rotateTime string) ClientOption {
return func(config *ClientConfig) {
config.RotateTime = rotateTime
}
}

// WithMaxAge ...
func WithMaxAge(maxAge int64) ClientOption {
return func(config *ClientConfig) {
config.MaxAge = maxAge
}
}

// WithLogLevel ...
func WithLogLevel(logLevel string) ClientOption {
return func(config *ClientConfig) {
Expand All @@ -180,3 +164,10 @@ func WithLogSampling(tick time.Duration, initial int, thereafter int) ClientOpti
config.LogSampling = &ClientLogSamplingConfig{initial, thereafter, tick}
}
}

// WithLogRollingConfig ...
func WithLogRollingConfig(rollingConfig *ClientLogRollingConfig) ClientOption {
return func(config *ClientConfig) {
config.LogRollingConfig = rollingConfig
}
}
6 changes: 0 additions & 6 deletions common/constant/client_config_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ func TestNewClientConfig(t *testing.T) {
assert.Equal(t, config.LogLevel, "info")
assert.Equal(t, config.BeatInterval, int64(5000))
assert.Equal(t, config.UpdateThreadNum, 20)
assert.Equal(t, config.RotateTime, "24h")

assert.Equal(t, config.LogDir, file.GetCurrentPath()+string(os.PathSeparator)+"log")
assert.Equal(t, config.CacheDir, file.GetCurrentPath()+string(os.PathSeparator)+"cache")

assert.Equal(t, config.MaxAge, int64(3))
assert.Equal(t, config.NotLoadCacheAtStart, false)
assert.Equal(t, config.UpdateCacheWhenEmpty, false)

Expand All @@ -61,12 +59,10 @@ func TestNewClientConfigWithOptions(t *testing.T) {
WithLogLevel("error"),
WithBeatInterval(int64(2000)),
WithUpdateThreadNum(30),
WithRotateTime("16h"),

WithLogDir("/tmp/nacos/log"),
WithCacheDir("/tmp/nacos/cache"),

WithMaxAge(int64(6)),
WithNotLoadCacheAtStart(true),
WithUpdateCacheWhenEmpty(true),

Expand All @@ -86,12 +82,10 @@ func TestNewClientConfigWithOptions(t *testing.T) {
assert.Equal(t, config.LogLevel, "error")
assert.Equal(t, config.BeatInterval, int64(2000))
assert.Equal(t, config.UpdateThreadNum, 30)
assert.Equal(t, config.RotateTime, "16h")

assert.Equal(t, config.LogDir, "/tmp/nacos/log")
assert.Equal(t, config.CacheDir, "/tmp/nacos/cache")

assert.Equal(t, config.MaxAge, int64(6))
assert.Equal(t, config.NotLoadCacheAtStart, true)
assert.Equal(t, config.UpdateCacheWhenEmpty, true)

Expand Down
74 changes: 51 additions & 23 deletions common/constant/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package constant

import "time"
import (
"time"
)

type ServerConfig struct {
Scheme string //the nacos server scheme
Expand All @@ -26,32 +28,58 @@ type ServerConfig struct {
}

type ClientConfig struct {
TimeoutMs uint64 //timeout for requesting Nacos server, default value is 10000ms
ListenInterval uint64 //Deprecated
BeatInterval int64 //the time interval for sending beat to server,default value is 5000ms
NamespaceId string //the namespaceId of Nacos.When namespace is public, fill in the blank string here.
AppName string //the appName
Endpoint string //the endpoint for get Nacos server addresses
RegionId string //the regionId for kms
AccessKey string //the AccessKey for kms
SecretKey string //the SecretKey for kms
OpenKMS bool //it's to open kms,default is false. https://help.aliyun.com/product/28933.html
CacheDir string //the directory for persist nacos service info,default value is current path
UpdateThreadNum int //the number of gorutine for update nacos service info,default value is 20
NotLoadCacheAtStart bool //not to load persistent nacos service info in CacheDir at start time
UpdateCacheWhenEmpty bool //update cache when get empty service instance from server
Username string //the username for nacos auth
Password string //the password for nacos auth
LogDir string //the directory for log, default is current path
RotateTime string //the rotate time for log, eg: 30m, 1h, 24h, default is 24h
MaxAge int64 //the max age of a log file, default value is 3
LogLevel string //the level of log, it's must be debug,info,warn,error, default value is info
LogSampling *ClientLogSamplingConfig //the sampling config of log
ContextPath string //the nacos server contextpath
TimeoutMs uint64 // timeout for requesting Nacos server, default value is 10000ms
ListenInterval uint64 // Deprecated
BeatInterval int64 // the time interval for sending beat to server,default value is 5000ms
NamespaceId string // the namespaceId of Nacos.When namespace is public, fill in the blank string here.
AppName string // the appName
Endpoint string // the endpoint for get Nacos server addresses
RegionId string // the regionId for kms
AccessKey string // the AccessKey for kms
SecretKey string // the SecretKey for kms
OpenKMS bool // it's to open kms,default is false. https://help.aliyun.com/product/28933.html
CacheDir string // the directory for persist nacos service info,default value is current path
UpdateThreadNum int // the number of gorutine for update nacos service info,default value is 20
NotLoadCacheAtStart bool // not to load persistent nacos service info in CacheDir at start time
UpdateCacheWhenEmpty bool // update cache when get empty service instance from server
Username string // the username for nacos auth
Password string // the password for nacos auth
LogDir string // the directory for log, default is current path
LogLevel string // the level of log, it's must be debug,info,warn,error, default value is info
LogSampling *ClientLogSamplingConfig // the sampling config of log
ContextPath string // the nacos server contextpath
LogRollingConfig *ClientLogRollingConfig // log rolling config
}

type ClientLogSamplingConfig struct {
Initial int //the sampling initial of log
Thereafter int //the sampling thereafter of log
Tick time.Duration //the sampling tick of log
}

type ClientLogRollingConfig struct {
// MaxSize is the maximum size in megabytes of the log file before it gets
// rotated. It defaults to 100 megabytes.
MaxSize int

// MaxAge is the maximum number of days to retain old log files based on the
// timestamp encoded in their filename. Note that a day is defined as 24
// hours and may not exactly correspond to calendar days due to daylight
// savings, leap seconds, etc. The default is not to remove old log files
// based on age.
MaxAge int

// MaxBackups is the maximum number of old log files to retain. The default
// is to retain all old log files (though MaxAge may still cause them to get
// deleted.)
MaxBackups int

// LocalTime determines if the time used for formatting the timestamps in
// backup files is the computer's local time. The default is to use UTC
// time.
LocalTime bool

// Compress determines if the rotated log files should be compressed
// using gzip. The default is not to perform compression.
Compress bool
}
1 change: 1 addition & 0 deletions common/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ const (
DEFAULT_SERVER_SCHEME = "http"
WINDOWS_LEGAL_NAME_SPLITER = "&&"
OS_WINDOWS = "windows"
LOG_FILE_NAME = "nacos-sdk.log"
)
71 changes: 44 additions & 27 deletions common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package logger

import (
"io"
"os"
"path/filepath"
"sync"
"time"

rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/nacos-group/nacos-sdk-go/common/file"
"github.com/nacos-group/nacos-sdk-go/common/constant"

"gopkg.in/natefinch/lumberjack.v2"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -42,11 +42,12 @@ var levelMap = map[string]zapcore.Level{
}

type Config struct {
Level string
OutputPath string
RotationTime string
MaxAge int64
Sampling *SamplingConfig
Level string
OutputPath string
RotationTime string
MaxAge int64
Sampling *SamplingConfig
LogRollingConfig *lumberjack.Logger
}

type SamplingConfig struct {
Expand Down Expand Up @@ -91,6 +92,31 @@ func init() {
SetLogger(&NacosLogger{zapLogger.Sugar()})
}

func BuildLoggerConfig(clientConfig constant.ClientConfig) Config {
loggerConfig := Config{
Level: clientConfig.LogLevel,
}
if clientConfig.LogSampling != nil {
loggerConfig.Sampling = &SamplingConfig{
Initial: clientConfig.LogSampling.Initial,
Thereafter: clientConfig.LogSampling.Thereafter,
Tick: clientConfig.LogSampling.Tick,
}
}
loggerConfig.LogRollingConfig = &lumberjack.Logger{
Filename: clientConfig.LogDir + string(os.PathSeparator) + constant.LOG_FILE_NAME,
}
logRollingConfig := clientConfig.LogRollingConfig
if logRollingConfig != nil {
loggerConfig.LogRollingConfig.MaxSize = logRollingConfig.MaxSize
loggerConfig.LogRollingConfig.MaxAge = logRollingConfig.MaxAge
loggerConfig.LogRollingConfig.MaxBackups = logRollingConfig.MaxBackups
loggerConfig.LogRollingConfig.LocalTime = logRollingConfig.LocalTime
loggerConfig.LogRollingConfig.Compress = logRollingConfig.Compress
}
return loggerConfig
}

// InitLogger is init global logger for nacos
func InitLogger(config Config) (err error) {
logLock.Lock()
Expand All @@ -103,14 +129,13 @@ func InitLogger(config Config) (err error) {
func InitNacosLogger(config Config) (Logger, error) {
logLevel := getLogLevel(config.Level)
encoder := getEncoder()
writer, err := getWriter(config.OutputPath, config.RotationTime, config.MaxAge)
if err != nil {
return nil, err
}
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), zapcore.AddSync(writer), logLevel)
writer := config.getLogWriter()
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), writer, logLevel)

if config.Sampling != nil {
core = zapcore.NewSamplerWithOptions(core, config.Sampling.Tick, config.Sampling.Initial, config.Sampling.Thereafter)
}

zaplogger := zap.New(core, zap.AddCallerSkip(1))
return &NacosLogger{zaplogger.Sugar()}, nil
}
Expand All @@ -137,19 +162,6 @@ func getEncoder() zapcore.EncoderConfig {
}
}

func getWriter(outputPath string, rotateTime string, maxAge int64) (writer io.Writer, err error) {
err = file.MkdirIfNecessary(outputPath)
if err != nil {
return
}
outputPath = outputPath + string(os.PathSeparator)
rotateDuration, err := time.ParseDuration(rotateTime)
writer, err = rotatelogs.New(filepath.Join(outputPath, "nacos-sdk.log-%Y%m%d%H%M"),
rotatelogs.WithRotationTime(rotateDuration), rotatelogs.WithMaxAge(time.Duration(maxAge)*rotateDuration),
rotatelogs.WithLinkName(filepath.Join(outputPath, "nacos-sdk.log")))
return
}

//SetLogger sets logger for sdk
func SetLogger(log Logger) {
logLock.Lock()
Expand All @@ -162,3 +174,8 @@ func GetLogger() Logger {
defer logLock.RUnlock()
return logger
}

// getLogWriter get Lumberjack writer by LumberjackConfig
func (c *Config) getLogWriter() zapcore.WriteSyncer {
return zapcore.AddSync(c.LogRollingConfig)
}
4 changes: 0 additions & 4 deletions example/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func main() {
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
RotateTime: "1h",
MaxAge: 3,
LogLevel: "debug",
}
//or a more graceful way to create ClientConfig
Expand All @@ -58,8 +56,6 @@ func main() {
constant.WithNotLoadCacheAtStart(true),
constant.WithLogDir("/tmp/nacos/log"),
constant.WithCacheDir("/tmp/nacos/cache"),
constant.WithRotateTime("1h"),
constant.WithMaxAge(3),
constant.WithLogLevel("debug"),
)

Expand Down
Loading

0 comments on commit 7481925

Please sign in to comment.