github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/supervisor/logconfig/logconfig.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package logconfig
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  	stdlog "log"
    24  	"os"
    25  
    26  	"github.com/mysteriumnetwork/node/logconfig/rollingwriter"
    27  	"github.com/rs/zerolog"
    28  	"github.com/rs/zerolog/log"
    29  )
    30  
    31  const (
    32  	timestampFmt = "2006-01-02T15:04:05.000"
    33  )
    34  
    35  // LogOptions describes logging options.
    36  type LogOptions struct {
    37  	LogLevel string
    38  	Filepath string
    39  }
    40  
    41  // Configure configures supervisor global logger instance.
    42  func Configure(opts LogOptions) error {
    43  	logLevel, err := zerolog.ParseLevel(opts.LogLevel)
    44  	if err != nil {
    45  		return fmt.Errorf("could not parse log level: %w", err)
    46  	}
    47  	log.Logger = log.Logger.Level(logLevel)
    48  
    49  	// Set default file path if not specified.
    50  	if opts.Filepath == "" {
    51  		var err error
    52  		opts.Filepath, err = defaultLogPath()
    53  		if err != nil {
    54  			return fmt.Errorf("could not get default log path: %w", err)
    55  		}
    56  	}
    57  
    58  	logsWriter, err := newLogWriter(opts.Filepath)
    59  	if err != nil {
    60  		return fmt.Errorf("could not create logs writer: %w", err)
    61  	}
    62  
    63  	logger := log.Output(logsWriter).
    64  		Level(logLevel).
    65  		With().
    66  		Timestamp().
    67  		Logger()
    68  
    69  	log.Logger = logger
    70  	stdlog.SetFlags(0)
    71  	stdlog.SetOutput(log.Logger)
    72  
    73  	return nil
    74  }
    75  
    76  // newLogWriter returns multi writer which can log to both file and console.
    77  func newLogWriter(filepath string) (io.Writer, error) {
    78  	rw, err := rollingwriter.NewRollingWriter(filepath)
    79  	if err != nil {
    80  		return nil, fmt.Errorf("could not to create rolling logs writer: %w", err)
    81  	}
    82  
    83  	if err := rw.CleanObsoleteLogs(); err != nil {
    84  		log.Err(err).Msg("Failed to cleanup obsolete logs")
    85  	}
    86  
    87  	fileWriter := zerolog.ConsoleWriter{
    88  		Out:        rw,
    89  		NoColor:    true,
    90  		TimeFormat: timestampFmt,
    91  	}
    92  	stderrWriter := zerolog.ConsoleWriter{
    93  		Out:        os.Stderr,
    94  		NoColor:    true,
    95  		TimeFormat: timestampFmt,
    96  	}
    97  	return io.MultiWriter(fileWriter, stderrWriter), nil
    98  }