Skip to content

Commit

Permalink
Export Logger type so multiple loggers can actually be used (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
adjackura authored Aug 8, 2017
1 parent 5b2da9b commit f5e2be5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
33 changes: 17 additions & 16 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import (
)

var (
defaultLogger *logger
initialized bool
defaultLogger *Logger
logLock sync.Mutex
)

Expand All @@ -36,7 +35,7 @@ const (
)

func initialize() {
defaultLogger = &logger{
defaultLogger = &Logger{
infoLog: log.New(os.Stderr, initText+"INFO: ", flags),
errorLog: log.New(os.Stderr, initText+"ERROR: ", flags),
fatalLog: log.New(os.Stderr, initText+"FATAL: ", flags),
Expand All @@ -53,7 +52,7 @@ func init() {
// The first call to Init populates the default logger and returns the
// generated logger, subsequent calls to Init will only return the generated
// logger.
func Init(name string, verbose, systemLog bool, logFile io.Writer) *logger {
func Init(name string, verbose, systemLog bool, logFile io.Writer) *Logger {
var il, el io.Writer
if systemLog {
var err error
Expand All @@ -75,7 +74,7 @@ func Init(name string, verbose, systemLog bool, logFile io.Writer) *logger {
eLogs = append(eLogs, el)
}

var l logger
var l Logger
l.infoLog = log.New(io.MultiWriter(iLogs...), "INFO: ", flags)
l.errorLog = log.New(io.MultiWriter(eLogs...), "ERROR: ", flags)
l.fatalLog = log.New(io.MultiWriter(eLogs...), "FATAL: ", flags)
Expand All @@ -98,14 +97,16 @@ const (
sFatal
)

type logger struct {
// A Logger represents an active logging object. Multiple loggers can be used
// simultaneously even if they are using the same same writers.
type Logger struct {
infoLog *log.Logger
errorLog *log.Logger
fatalLog *log.Logger
initialized bool
}

func (l *logger) output(s severity, txt string) {
func (l *Logger) output(s severity, txt string) {
logLock.Lock()
defer logLock.Unlock()
switch s {
Expand All @@ -122,57 +123,57 @@ func (l *logger) output(s severity, txt string) {

// Info logs with the INFO severity.
// Arguments are handled in the manner of fmt.Print.
func (l *logger) Info(v ...interface{}) {
func (l *Logger) Info(v ...interface{}) {
l.output(sInfo, fmt.Sprint(v...))
}

// Infoln logs with the INFO severity.
// Arguments are handled in the manner of fmt.Println.
func (l *logger) Infoln(v ...interface{}) {
func (l *Logger) Infoln(v ...interface{}) {
l.output(sInfo, fmt.Sprintln(v...))
}

// Infof logs with the INFO severity.
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Infof(format string, v ...interface{}) {
func (l *Logger) Infof(format string, v ...interface{}) {
l.output(sInfo, fmt.Sprintf(format, v...))
}

// Error logs with the ERROR severity.
// Arguments are handled in the manner of fmt.Print.
func (l *logger) Error(v ...interface{}) {
func (l *Logger) Error(v ...interface{}) {
l.output(sError, fmt.Sprint(v...))
}

// Errorln logs with the ERROR severity.
// Arguments are handled in the manner of fmt.Println.
func (l *logger) Errorln(v ...interface{}) {
func (l *Logger) Errorln(v ...interface{}) {
l.output(sError, fmt.Sprintln(v...))
}

// Errorf logs with the Error severity.
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Errorf(format string, v ...interface{}) {
func (l *Logger) Errorf(format string, v ...interface{}) {
l.output(sError, fmt.Sprintf(format, v...))
}

// Fatal logs with the Fatal severity, and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Print.
func (l *logger) Fatal(v ...interface{}) {
func (l *Logger) Fatal(v ...interface{}) {
l.output(sFatal, fmt.Sprint(v...))
os.Exit(1)
}

// Fatalln logs with the Fatal severity, and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Println.
func (l *logger) Fatalln(v ...interface{}) {
func (l *Logger) Fatalln(v ...interface{}) {
l.output(sFatal, fmt.Sprintln(v...))
os.Exit(1)
}

// Fatalf logs with the Fatal severity, and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Fatalf(format string, v ...interface{}) {
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.output(sFatal, fmt.Sprintf(format, v...))
os.Exit(1)
}
Expand Down
1 change: 0 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestLoggingBeforeInit(t *testing.T) {
os.Stderr = w
// Reset
initialize()
initialized = false

info := "info log"
errL := "error log"
Expand Down

0 comments on commit f5e2be5

Please sign in to comment.