Skip to content

Commit

Permalink
Merge pull request #847 from hzxuzhonghu/logger-improve
Browse files Browse the repository at this point in the history
make default logger only print to stderr not file
  • Loading branch information
nlgwcy authored Sep 14, 2024
2 parents 05f60a1 + fb8c42e commit 53caaa8
Show file tree
Hide file tree
Showing 21 changed files with 39 additions and 59 deletions.
2 changes: 1 addition & 1 deletion daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func main() {
var log = logger.NewLoggerField("main")
var log = logger.NewLoggerScope("main")
cmd := manager.NewCommand()
if err := cmd.Execute(); err != nil {
log.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion daemon/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
pkgSubsys = "manager"
)

var log = logger.NewLoggerField(pkgSubsys)
var log = logger.NewLoggerScope(pkgSubsys)

func NewCommand() *cobra.Command {
configs := options.NewBootstrapConfigs()
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
)

var (
log = logger.NewLoggerField("pkg/auth")
log = logger.NewLoggerScope("auth")
)

type Rbac struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/bpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
)

var (
log = logger.NewLoggerField("pkg/bpf")
log = logger.NewLoggerScope("bpf")
hash = fnv.New32a()
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/v2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

var RWListener sync.RWMutex

var log = logger.NewLoggerField("cache/v2")
var log = logger.NewLoggerScope("cache/v2")

type ListenerCache struct {
mutex sync.RWMutex
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/v2/maps/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

var (
log = logger.NewLoggerField("cache/v2/maps")
log = logger.NewLoggerScope("cache/v2/maps")
)

func listenerToGolang(goMsg *listener_v2.Listener, cMsg *C.Listener__Listener) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cni/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"kmesh.net/kmesh/pkg/logger"
)

var log = logger.NewLoggerField("cni installer")
var log = logger.NewLoggerScope("cni installer")

func (i *Installer) addCniConfig() error {
var err error
Expand Down
2 changes: 1 addition & 1 deletion pkg/cni/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
)

var (
log = logger.NewLoggerFieldWithoutStdout("plugin/cniplugin")
log = logger.NewFileLogger("cniplugin")
)

// cniConf is whatever you expect your configuration json to be. This is whatever
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/ads/ads_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

var (
log = logger.NewLoggerField("ads_controller")
log = logger.NewLoggerScope("ads_controller")
)

type Controller struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/bypass/bypass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

var (
log = logger.NewLoggerField("bypass")
log = logger.NewLoggerScope("bypass")
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
)

var (
log = logger.NewLoggerField("controller/config")
log = logger.NewLoggerScope("controller/config")
config *XdsConfig
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

var (
ctx, cancel = context.WithCancel(context.Background())
log = logger.NewLoggerField("controller")
log = logger.NewLoggerScope("controller")
)

type Controller struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/manage/manage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
"kmesh.net/kmesh/pkg/utils"
)

var log = logger.NewLoggerField("manage_controller")
var log = logger.NewLoggerScope("manage_controller")

const (
MaxRetries = 5
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/security/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"kmesh.net/kmesh/pkg/logger"
)

var log = logger.NewLoggerField("security")
var log = logger.NewLoggerScope("security")

type certItem struct {
cert *istiosecurity.SecretItem
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/telemetry/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

var (
log = logger.NewLoggerField("pkg/telemetry")
log = logger.NewLoggerScope("telemetry")
mu sync.Mutex
deleteWorkload = []*workloadapi.Workload{}
deleteService = []string{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/workload/bpfcache/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"kmesh.net/kmesh/pkg/logger"
)

var log = logger.NewLoggerField("workload_bpfcache")
var log = logger.NewLoggerScope("workload_bpfcache")

type Cache struct {
bpfMap bpf2go.KmeshCgroupSockWorkloadMaps
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/workload/workload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
AuthorizationType = "type.googleapis.com/istio.security.Authorization"
)

var log = logger.NewLoggerField("workload_controller")
var log = logger.NewLoggerScope("workload_controller")

type Controller struct {
Stream discoveryv3.AggregatedDiscoveryService_DeltaAggregatedResourcesClient
Expand Down
2 changes: 1 addition & 1 deletion pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

var (
log = logger.NewLoggerField("dns_resolver")
log = logger.NewLoggerScope("dns_resolver")
)

const (
Expand Down
58 changes: 19 additions & 39 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ import (
)

const (
logSubsys = "subsys"
mapName = "kmesh_events"
MAX_MSG_LEN = 255
logSubsys = "subsys"
mapName = "kmesh_events"
)

type LogEvent struct {
Expand All @@ -44,8 +43,8 @@ type LogEvent struct {
}

var (
defaultLogger = InitializeDefaultLogger(false)
fileOnlyLogger = InitializeDefaultLogger(true)
defaultLogger = initDefaultLogger()
fileOnlyLogger = initFileLogger()

defaultLogLevel = logrus.InfoLevel
defaultLogFile = "/var/run/kmesh/daemon.log"
Expand All @@ -61,22 +60,6 @@ var (
}
)

func PrintLogs() error {
logsPath := defaultLogFile
file, err := os.Open(logsPath)
if err != nil {
return err
}
defer file.Close()

// Redirct file contents to STDOUT
_, err = io.Copy(os.Stdout, file)
if err != nil {
return err
}
return nil
}

func SetLoggerLevel(loggerName string, level logrus.Level) error {
logger, exists := loggerMap[loggerName]
if !exists || logger == nil {
Expand All @@ -102,12 +85,17 @@ func GetLoggerNames() []string {
return names
}

// InitializeDefaultLogger return a initialized logger
func InitializeDefaultLogger(onlyFile bool) *logrus.Logger {
// initDefaultLogger return a default logger
func initDefaultLogger() *logrus.Logger {
logger := logrus.New()
logger.SetFormatter(defaultLogFormat)
logger.SetLevel(defaultLogLevel)
return logger
}

// initFileLogger return a file only logger
func initFileLogger() *logrus.Logger {
logger := initDefaultLogger()
path, _ := filepath.Split(defaultLogFile)
err := os.MkdirAll(path, 0o700)
if err != nil {
Expand All @@ -121,29 +109,21 @@ func InitializeDefaultLogger(onlyFile bool) *logrus.Logger {
MaxAge: 28, //days
Compress: false, // disabled by default
}

if onlyFile {
logger.SetOutput(io.Writer(logfile))
} else {
logger.SetOutput(io.MultiWriter(os.Stdout, logfile))
}

logger.SetOutput(io.Writer(logfile))
return logger
}

// NewLoggerField allocates a new log entry and adds a field to it.
func NewLoggerField(pkgSubsys string) *logrus.Entry {
return defaultLogger.WithField(logSubsys, pkgSubsys)
// NewLoggerScope allocates a new log entry for a specific scope.
func NewLoggerScope(scope string) *logrus.Entry {
return defaultLogger.WithField(logSubsys, scope)
}

// NewLoggerFieldFileOnly don't output log to stdout
func NewLoggerFieldWithoutStdout(pkgSubsys string) *logrus.Entry {
// NewFileLogger don't output log to stdout
func NewFileLogger(pkgSubsys string) *logrus.Entry {
return fileOnlyLogger.WithField(logSubsys, pkgSubsys)
}

/*
print bpf log to daemon process.
*/
// print bpf log in daemon process
func StartRingBufReader(ctx context.Context, mode string, bpfFsPath string) error {
var path string

Expand All @@ -166,7 +146,7 @@ func StartRingBufReader(ctx context.Context, mode string, bpfFsPath string) erro
}

func handleLogEvents(ctx context.Context, rbMap *ebpf.Map) {
log := NewLoggerField("ebpf")
log := NewLoggerScope("ebpf")
events, err := ringbuf.NewReader(rbMap)
if err != nil {
log.Errorf("ringbuf new reader from rb map failed:%v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/status/status_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"kmesh.net/kmesh/pkg/logger"
)

var log = logger.NewLoggerField("status")
var log = logger.NewLoggerScope("status")

const (
adminAddr = "localhost:15200"
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"kmesh.net/kmesh/pkg/logger"
)

var log = logger.NewLoggerField("utils")
var log = logger.NewLoggerScope("utils")

func ExecuteWithRedirect(cmd string, args []string, stdout io.Writer) error {
var err error
Expand Down

0 comments on commit 53caaa8

Please sign in to comment.