loggen

package
v0.81.0-dev-1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "loggen",
	Short: "Log Generator",
	Args:  cobra.NoArgs,
	Run: func(c *cobra.Command, args []string) {
		var i int = 0
		if FlagJson {
			var logger zerolog.Logger
			if FlagLogFile != "" {
				f, err := os.OpenFile(FlagLogFile,
					os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
				if err != nil {
					panic(err)
				}
				defer f.Close()
				logger = zerolog.New(f).With().Timestamp().Logger()
				logger.Info().Str("prefix", FlagLogPrefix).Msg("Logging into file " + FlagLogFile)

			} else {
				logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
				logger.Info().Str("prefix", FlagLogPrefix).Msg("Logging into STDOUT")
			}

			if FlagNoError && FlagNoWarn && FlagNoInfo && FlagNoDebug {
				logger.Error().Str("prefix", FlagLogPrefix).Msg("ERROR No logging output enabled.")
				os.Exit(1)
			}

			for {
				time.Sleep(time.Duration(FlagSleepTime) * time.Millisecond)

				randomNumber := rand.Intn(100)
				if randomNumber > 90 && !FlagNoError {
					logger.Error().Str("prefix", FlagLogPrefix).Int("i", i).Msg("An error is usually an exception that has been caught and not handled.")
					i++
					continue
				}
				if randomNumber > 70 && !FlagNoWarn {
					logger.Warn().Str("prefix", FlagLogPrefix).Int("i", i).Msg("WARN A warning that should be ignored is usually at this level and should be actionable.")
					i++
					continue
				}
				if randomNumber > 30 && !FlagNoInfo {
					logger.Info().Str("prefix", FlagLogPrefix).Int("i", i).Msg("INFO This is less important than debug log and is often used to provide context in the current task.")
					i++
					continue
				}
				if !FlagNoDebug {
					logger.Debug().Str("prefix", FlagLogPrefix).Int("i", i).Msg("DEBUG This is a debug log that shows a log that can be ignored.")
					i++
					continue
				}
			}
		} else {
			var logger *log.Logger
			if FlagLogFile != "" {
				f, err := os.OpenFile(FlagLogFile,
					os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
				if err != nil {
					panic(err)
				}
				defer f.Close()
				logger = log.New(f, FlagLogPrefix+" ", log.LstdFlags)
				logger.Println("Logging into file " + FlagLogFile)

			} else {
				logger = log.New(os.Stdout, FlagLogPrefix+" ", log.LstdFlags)
				logger.Println("Logging into STDOUT")
			}

			if FlagNoError && FlagNoWarn && FlagNoInfo && FlagNoDebug {
				logger.Println("ERROR No logging output enabled.")
				os.Exit(1)
			}

			for {
				time.Sleep(time.Duration(FlagSleepTime) * time.Millisecond)

				randomNumber := rand.Intn(100)
				if randomNumber > 90 && !FlagNoError {
					logger.Printf("ERROR An error is usually an exception that has been caught and not handled. (i=%d)\n", i)
					i++
					continue
				}
				if randomNumber > 70 && !FlagNoWarn {
					logger.Printf("WARN A warning that should be ignored is usually at this level and should be actionable. (i=%d)\n", i)
					i++
					continue
				}
				if randomNumber > 30 && !FlagNoInfo {
					logger.Printf("INFO This is less important than debug log and is often used to provide context in the current task (i=%d)\n", i)
					i++
					continue
				}
				if !FlagNoDebug {
					logger.Printf("DEBUG This is a debug log that shows a log that can be ignored. (i=%d)\n", i)
					i++
					continue
				}
			}
		}
	},
}
View Source
var FlagJson bool
View Source
var FlagLogFile string
View Source
var FlagLogPrefix string
View Source
var FlagNoDebug bool
View Source
var FlagNoError bool
View Source
var FlagNoInfo bool
View Source
var FlagNoWarn bool
View Source
var FlagSleepTime int

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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