From 13439c8d79a0b625e244dd3d730c257e44103ee5 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Kurauchi Date: Thu, 20 Aug 2020 22:15:59 +0900 Subject: [PATCH] add package-wide logger --- internal/logger/logger.go | 80 +++++++++++++++++++++++++++++++++++++++ logger.go | 37 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 internal/logger/logger.go create mode 100644 logger.go diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 00000000..a3de1913 --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,80 @@ +// Copyright 2019-2020 go-pfcp authors. All rights reserved. +// Use of this source code is governed by a MIT-style license that can be +// found in the LICENSE file. + +// Package logger provides a logging functionalities for go-pfcp. +// +// This is hidden here to be able to be imported from each package of go-pfcp. +package logger + +import ( + "io/ioutil" + "log" + "os" + "sync" +) + +var ( + logger = log.New(os.Stderr, "", log.LstdFlags) + logMu sync.Mutex +) + +// SetLogger replaces the standard logger with arbitrary *log.Logger. +// +// DON'T CALL THIS. Use the func in pfcp package instead. +// +// This package prints just informational logs from goroutines working background +// that might help developers test the program but can be ignored safely. More +// important ones that needs any action by caller would be returned as errors. +func SetLogger(l *log.Logger) { + if l == nil { + log.Println("Don't pass nil to SetLogger: use DisableLogging instead.") + } + + setLogger(l) +} + +// EnableLogging enables the logging from the package. +// +// DON'T CALL THIS. Use the func in pfcp package instead. +// +// If l is nil, it uses default logger provided by the package. +// Logging is enabled by default. +// +// See also: SetLogger. +func EnableLogging(l *log.Logger) { + logMu.Lock() + defer logMu.Unlock() + + setLogger(l) +} + +// DisableLogging disables the logging from the package. +// +// DON'T CALL THIS. Use the func in pfcp package instead. +// +// Logging is enabled by default. +func DisableLogging() { + logMu.Lock() + defer logMu.Unlock() + + logger.SetOutput(ioutil.Discard) +} + +func setLogger(l *log.Logger) { + if l == nil { + l = log.New(os.Stderr, "", log.LstdFlags) + } + + logMu.Lock() + defer logMu.Unlock() + + logger = l +} + +func Logf(format string, v ...interface{}) { + logMu.Lock() + defer logMu.Unlock() + + logger.Printf(format, v...) +} diff --git a/logger.go b/logger.go new file mode 100644 index 00000000..d0c1441b --- /dev/null +++ b/logger.go @@ -0,0 +1,37 @@ +// Copyright 2019-2020 go-pfcp authors. All rights reserved. +// Use of this source code is governed by a MIT-style license that can be +// found in the LICENSE file. + +package pfcp + +import ( + "log" + + "github.com/wmnsk/go-pfcp/internal/logger" +) + +// SetLogger replaces the standard logger with arbitrary *log.Logger. +// +// This package prints just informational logs from goroutines working background +// that might help developers test the program but can be ignored safely. More +// important ones that need any action by the caller would be returned as errors. +func SetLogger(l *log.Logger) { + logger.SetLogger(l) +} + +// EnableLogging enables the logging from the package. +// +// If l is nil, it uses default logger provided by the package. +// Logging is enabled by default. +// +// See also: SetLogger. +func EnableLogging(l *log.Logger) { + logger.EnableLogging(l) +} + +// DisableLogging disables the logging from the package. +// +// Logging is enabled by default. +func DisableLogging() { + logger.DisableLogging() +}