logcmd

package
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cobra.Command{
	Use:   "Log",
	Short: "log a message to the configured logger",
	Example: `
	clog Log -T  "trace message"
	clog Log -D  "debug message"
	clog Log -W  "warning message"
	clog Log -I  "info message"
	clog Log -S  "success message"
	clog Log -E  "error message"
	clog Log -F  "fatal message"
	clog Log -X  "emergency message"
	clog Log -UI "up one line (overprint) an info message"
	clog Log -B "$errCount" "$isProduction" "Base-Message"
	`,
	Run: func(cmd *cobra.Command, args []string) {
		logMsg := strings.Join(args, " ")

		logFlag := "none"

		if up {

			fmt.Print("\x1b[A\x1b[G\x1b[K")
		}

		switch {
		case emergency:
			slog.Emergency(logMsg)
			logFlag = "X"
		case fatal:
			slog.Fatal(logMsg)
			logFlag = "F"
		case error:
			slog.Error(logMsg)
			logFlag = "E"
		case warn:
			slog.Warn(logMsg)
			logFlag = "W"
		case success:
			slog.Success(logMsg)
			logFlag = "S"
		case info:
			slog.Info(logMsg)
			logFlag = "I"
		case trace:
			slog.Trace(logMsg)
			logFlag = "T"
		case debug:
			slog.Debug(logMsg)
			logFlag = "D"
		case build:
			// a special case.
			// -  err + dev  builds: Warn and carry on (i.e. exit code 0)
			// -  err + prod builds: Fail and exit (i.e. exit code 1)
			// -  ok  + either     : print out success
			var exitCode int
			_, err := fmt.Sscanf(args[0], "%d", &exitCode)
			if len(args) < 4 || err != nil {
				slog.Error("clog Log -B requires 4 arguments")
				slog.Error("   arg[0]           \"$?\" exit code of command to log for")
				slog.Error("   arg[1]      \"$doPROD\" empty string for tolerant dev mode otherwise any string for fragile PROD mode")
				slog.Error("   arg[2]   \"OK message\" string to be logged for $?=0")
				slog.Error("   arg[3]  \"Err Message\" string to be logged for $?=0")
				os.Exit(1)
			}
			errInfo := fmt.Sprintf(" {err:%d, prod:\"%s\"}", exitCode, args[1])
			if exitCode == 0 {
				slog.Success(args[2])
			} else if len(args[1]) > 0 {

				slog.Error(args[3] + errInfo)
				os.Exit(1)
			} else {

				slog.Warn(args[3] + errInfo)
			}
			logFlag = "B"
		}

		slog.Debug("Log (-%s) (%s)", logFlag, logMsg)
	},
}

Command define the cobra settings for this command

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