...

Source file src/oss.terrastruct.com/d2/lib/simplelog/simplelog.go

Documentation: oss.terrastruct.com/d2/lib/simplelog

     1  // Package simplelog contains a very simple interface for logging strings at either Debug, Info, or Error levels
     2  package simplelog
     3  
     4  import (
     5  	"context"
     6  
     7  	"oss.terrastruct.com/d2/lib/log"
     8  	"oss.terrastruct.com/util-go/cmdlog"
     9  )
    10  
    11  type Logger interface {
    12  	Debug(string)
    13  	Info(string)
    14  	Error(string)
    15  }
    16  
    17  type logger struct {
    18  	logDebug *func(string)
    19  	logInfo  *func(string)
    20  	logError *func(string)
    21  }
    22  
    23  func (l logger) Debug(s string) {
    24  	if l.logDebug != nil {
    25  		(*l.logDebug)(s)
    26  	}
    27  }
    28  func (l logger) Info(s string) {
    29  	if l.logInfo != nil {
    30  		(*l.logInfo)(s)
    31  	}
    32  }
    33  func (l logger) Error(s string) {
    34  	if l.logError != nil {
    35  		(*l.logError)(s)
    36  	}
    37  }
    38  
    39  func Make(logDebug, logInfo, logError *func(string)) Logger {
    40  	return logger{
    41  		logDebug: logDebug,
    42  		logInfo:  logInfo,
    43  		logError: logError,
    44  	}
    45  }
    46  
    47  func FromLibLog(ctx context.Context) Logger {
    48  	lDebug := func(s string) {
    49  		log.Debug(ctx, s)
    50  	}
    51  	lInfo := func(s string) {
    52  		log.Info(ctx, s)
    53  	}
    54  	lError := func(s string) {
    55  		log.Error(ctx, s)
    56  	}
    57  	return Make(&lDebug, &lInfo, &lError)
    58  }
    59  
    60  func FromCmdLog(cl *cmdlog.Logger) Logger {
    61  	lDebug := func(s string) {
    62  		cl.Debug.Print(s)
    63  	}
    64  	lInfo := func(s string) {
    65  		cl.Info.Print(s)
    66  	}
    67  	lError := func(s string) {
    68  		cl.Error.Print(s)
    69  	}
    70  	return Make(&lDebug, &lInfo, &lError)
    71  }
    72  

View as plain text