forked from wmnsk/go-pfcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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...) | ||
} |
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,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() | ||
} |