Skip to content

Commit

Permalink
add package-wide logger
Browse files Browse the repository at this point in the history
  • Loading branch information
wmnsk committed Aug 20, 2020
1 parent db9c7e7 commit 13439c8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
80 changes: 80 additions & 0 deletions internal/logger/logger.go
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...)
}
37 changes: 37 additions & 0 deletions logger.go
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()
}

0 comments on commit 13439c8

Please sign in to comment.