logdflag

package
v0.1.44 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 12 Imported by: 18

Documentation

Overview

Package logdflag provides a standard flag set for configuring the logger package and starting logging. Typical usage would be something like:

	import(
		"bitbucket.org/pcas/logger/logd/logdflag"
     "bitbucket.org/pcastools/flag"
		...
	)

// parseArgs parses the command-line flags and environment variables.

func parseArgs() error {
	// Define the usage message and command-line flags
	flag.SetGlobalHeader(fmt.Sprintf("...header for the usage message...")
	flag.SetName("Options")
	flag.Add(
		flag.String("foo", ...),
     ...
	)
 // Add the flagset that controls the logger
	logSet := logdflag.NewLogSet(nil)
	flag.AddSet(logSet)
	// Parse the flags
	flag.Parse()
 // Do any additional sanity checks
 ...
	// Set the global logger, using the application name as the log name
	if err := logdflag.SetLogger(
		logSet.ToStderr(),
		logSet.ToServer(),
		logSet.ClientConfig(),
		Name,
	); err != nil {
		return err
	}
 return nil
 }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultIdentifier

func DefaultIdentifier() string

DefaultIdentifier returns the identifier set on the logd client. This is typically of the form "<hostname>-<PID>", however if the hostname cannot be determined then a ULID is used.

func SetLogger

func SetLogger(ctx context.Context, toStderr bool, toServer bool, c *logd.ClientConfig, name string) error

SetLogger sets the global logger log.Log() as specified by toStderr, toServer, the client config c, and the destination log name. This will also update the default client config to c. If necessary, a shutdown function will be registered with package "bitbucket.org/pcastools/cleanup".

Types

type LogSet

type LogSet struct {
	*Set
	// contains filtered or unexported fields
}

LogSet represents a set of command-line flags defined by a client config, and provides destination logging information.

func NewLogSet

func NewLogSet(c *logd.ClientConfig) *LogSet

NewLogSet returns a set of command-line flags as described by NewSet. In addition, this set provides destination logging information.

func (*LogSet) Flags

func (s *LogSet) Flags() []flag.Flag

Flags returns the members of the set.

func (*LogSet) ToServer

func (s *LogSet) ToServer() bool

ToServer returns true iff the user selected to log to the logd server. This should only be called after the set has been successfully validated.

func (*LogSet) ToStderr

func (s *LogSet) ToStderr() bool

ToStderr returns true iff the user selected to log to os.Stderr. This should only be called after the set has been successfully validated.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set represents a set of command-line flags defined by a client config.

func NewSet

func NewSet(c *logd.ClientConfig) *Set

NewSet returns a set of command-line flags with defaults given by c. If c is nil then the logd.DefaultConfig() will be used. Note that this does not update the default client config, nor does this update c. To recover the updated client config after parse, call the ClientConfig() method on the returned set.

func (*Set) ClientConfig

func (s *Set) ClientConfig() *logd.ClientConfig

ClientConfig returns the client config described by this set. This should only be called after the set has been successfully validated.

func (*Set) Flags

func (s *Set) Flags() []flag.Flag

Flags returns the members of the set.

func (*Set) Name

func (*Set) Name() string

Name returns the name of this collection of flags.

func (*Set) UsageFooter

func (*Set) UsageFooter() string

UsageFooter returns the footer for the usage message for this flag set.

func (*Set) UsageHeader

func (*Set) UsageHeader() string

UsageHeader returns the header for the usage message for this flag set.

func (*Set) Validate

func (s *Set) Validate() error

Validate validates the flag set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL