github.com/richardwilkes/toolbox@v1.121.0/log/rotation/cmdline.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package rotation
    11  
    12  import (
    13  	"io"
    14  	"log"
    15  	"os"
    16  
    17  	"github.com/richardwilkes/toolbox/cmdline"
    18  	"github.com/richardwilkes/toolbox/errs"
    19  	"github.com/richardwilkes/toolbox/i18n"
    20  	"github.com/richardwilkes/toolbox/xio"
    21  )
    22  
    23  // PathToLog holds the path to the log file that was configured on the command line when using ParseAndSetup().
    24  var PathToLog string
    25  
    26  // ParseAndSetupLogging adds command-line options for controlling logging, parses the command line, then instantiates a
    27  // rotator and attaches it to slog. Returns the remaining arguments that weren't used for option content. If
    28  // consoleOnByDefault is true, then logs will also go to the console by default, but an option to turn them off will be
    29  // added to the command line flags. Conversely, if it is false, an option to turn them on will be added to the command
    30  // line flags.
    31  func ParseAndSetupLogging(cl *cmdline.CmdLine, consoleOnByDefault bool) []string {
    32  	logFile := DefaultPath()
    33  	var maxSize int64 = DefaultMaxSize
    34  	maxBackups := DefaultMaxBackups
    35  	consoleOption := false
    36  	cl.NewGeneralOption(&logFile).SetSingle('l').SetName("log-file").SetUsage(i18n.Text("The file to write logs to"))
    37  	cl.NewGeneralOption(&maxSize).SetName("log-file-size").SetUsage(i18n.Text("The maximum number of bytes to write to a log file before rotating it"))
    38  	cl.NewGeneralOption(&maxBackups).SetName("log-file-backups").SetUsage(i18n.Text("The maximum number of old logs files to retain"))
    39  	opt := cl.NewGeneralOption(&consoleOption)
    40  	if consoleOnByDefault {
    41  		opt.SetSingle('q').SetName("quiet").SetUsage(i18n.Text("Suppress the log output to the console"))
    42  	} else {
    43  		opt.SetSingle('C').SetName("log-to-console").SetUsage(i18n.Text("Copy the log output to the console"))
    44  	}
    45  	remainingArgs := cl.Parse(os.Args[1:])
    46  	if rotator, err := New(Path(logFile), MaxSize(maxSize), MaxBackups(maxBackups)); err == nil {
    47  		if consoleOnByDefault == consoleOption {
    48  			log.SetOutput(rotator)
    49  		} else {
    50  			log.SetOutput(&xio.TeeWriter{Writers: []io.Writer{rotator, os.Stdout}})
    51  		}
    52  		PathToLog = rotator.PathToLog()
    53  	} else {
    54  		errs.Log(err)
    55  	}
    56  	return remainingArgs
    57  }