github.com/prysmaticlabs/prysm@v1.4.4/shared/logutil/logutil.go (about)

     1  // Package logutil creates a Multi writer instance that
     2  // write all logs that are written to stdout.
     3  package logutil
     4  
     5  import (
     6  	"io"
     7  	"net/url"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/prysmaticlabs/prysm/shared/params"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  func addLogWriter(w io.Writer) {
    16  	mw := io.MultiWriter(logrus.StandardLogger().Out, w)
    17  	logrus.SetOutput(mw)
    18  }
    19  
    20  // ConfigurePersistentLogging adds a log-to-file writer. File content is identical to stdout.
    21  func ConfigurePersistentLogging(logFileName string) error {
    22  	logrus.WithField("logFileName", logFileName).Info("Logs will be made persistent")
    23  	f, err := os.OpenFile(logFileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, params.BeaconIoConfig().ReadWritePermissions)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	addLogWriter(f)
    29  
    30  	logrus.Info("File logging initialized")
    31  	return nil
    32  }
    33  
    34  // MaskCredentialsLogging masks the url credentials before logging for security purpose
    35  // [scheme:][//[userinfo@]host][/]path[?query][#fragment] -->  [scheme:][//[***]host][/***][#***]
    36  // if the format is not matched nothing is done, string is returned as is.
    37  func MaskCredentialsLogging(currUrl string) string {
    38  	// error if the input is not a URL
    39  	MaskedUrl := currUrl
    40  	u, err := url.Parse(currUrl)
    41  	if err != nil {
    42  		return currUrl // Not a URL, nothing to do
    43  	}
    44  	// Mask the userinfo and the URI (path?query or opaque?query ) and fragment, leave the scheme and host(host/port)  untouched
    45  	if u.User != nil {
    46  		MaskedUrl = strings.Replace(MaskedUrl, u.User.String(), "***", 1)
    47  	}
    48  	if len(u.RequestURI()) > 1 { // Ignore the '/'
    49  		MaskedUrl = strings.Replace(MaskedUrl, u.RequestURI(), "/***", 1)
    50  	}
    51  	if len(u.Fragment) > 0 {
    52  		MaskedUrl = strings.Replace(MaskedUrl, u.RawFragment, "***", 1)
    53  	}
    54  	return MaskedUrl
    55  }