diff --git a/cmd/loggingctl/cat.go b/cmd/loggingctl/cat.go new file mode 100644 index 0000000..16d318a --- /dev/null +++ b/cmd/loggingctl/cat.go @@ -0,0 +1,26 @@ +/* +Copyright © 2024 John van Zantvoort +*/ +package main + +import ( + "github.com/jvzantvoort/gextend-bash/messages" + "github.com/spf13/cobra" +) + +// CatCmd represents the cat command +var CatCmd = &cobra.Command{ + Use: "cat", + Short: "Log a cat command", + Long: messages.GetLong("logging"), + Run: handleLogCmd, +} + +func init() { + rootCmd.AddCommand(CatCmd) + CatCmd.Flags().StringP("tag", "t", "", "mark every line with this tag") + CatCmd.Flags().StringP("file", "f", "", "log the contents of this file") + CatCmd.Flags().StringP("priority", "p", "", "mark given message with this priority") + CatCmd.Flags().BoolP("skip-empty", "e", false, "do not log empty lines when processing files") + CatCmd.Flags().BoolP("stderr", "s", false, "output message to standard error as well") +} diff --git a/cmd/loggingctl/common.go b/cmd/loggingctl/common.go new file mode 100644 index 0000000..560fa57 --- /dev/null +++ b/cmd/loggingctl/common.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + + "github.com/jvzantvoort/gextend-bash/logging" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func GetString(cmd cobra.Command, name string) string { + retv, _ := cmd.Flags().GetString(name) + if len(retv) != 0 { + log.Infof("Found %s as %s", name, retv) + } + return retv +} + +func handleLogCmd(cmd *cobra.Command, args []string) { + if verbose { + log.SetLevel(log.DebugLevel) + } + log.Debugf("%s: start", cmd.Use) + defer log.Debugf("%s: end", cmd.Use) + filepath := "/home/jvzantvoort/Logs/2024/July/common.log" + + logmsgs := logging.NewLogMessages(filepath) + for _, msg := range logmsgs.Messages { + tag := "" + if len(msg.Tag) != 0 { + tag = fmt.Sprintf("<%s> ", msg.Tag) + } + fmt.Printf("%s %-8s %s%s\n", + msg.Time.Format("2006-01-02 15:04:05"), + msg.Priority, + tag, + msg.Message, + ) + } + /* + for _, msg := range logmsgs.messages { + + fmt.Printf("%#v\n", msg) + + } + */ +} diff --git a/cmd/loggingctl/main.go b/cmd/loggingctl/main.go new file mode 100644 index 0000000..0038e3c --- /dev/null +++ b/cmd/loggingctl/main.go @@ -0,0 +1,8 @@ +/* +goproj creates, maintains, archives and removes projects +*/ +package main + +func main() { + Execute() +} diff --git a/cmd/loggingctl/root.go b/cmd/loggingctl/root.go new file mode 100644 index 0000000..0edd78e --- /dev/null +++ b/cmd/loggingctl/root.go @@ -0,0 +1,48 @@ +/* +Copyright © 2024 John van Zantvoort +*/ +package main + +import ( + "os" + + "github.com/jvzantvoort/gextend-bash/messages" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var verbose bool + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "loggingctl", + Short: "Interface for loggingctl", + Long: messages.GetLong("root"), +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Setup loggingctl + log.SetFormatter(&log.TextFormatter{ + FullTimestamp: true, + DisableLevelTruncation: true, + TimestampFormat: "2006-01-02 15:04:05", + }) + + // Output to stdout instead of the default stderr + // Can be any io.Writer, see below for File example + log.SetOutput(os.Stdout) + + // Only log the warning severity or above. + log.SetLevel(log.InfoLevel) + + rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose logging") +} diff --git a/logging/main.go b/logging/main.go index 9ca4a9c..d8578fa 100644 --- a/logging/main.go +++ b/logging/main.go @@ -27,7 +27,7 @@ type LogMessage struct { } type LogMessages struct { - messages []LogMessage + Messages []LogMessage } func GetString(cmd cobra.Command, name string) string { @@ -179,12 +179,12 @@ func NewLogMessages(inputfile string) *LogMessages { if err := json.Unmarshal([]byte(scanner.Bytes()), &obj); err != nil { log.Errorf("Error: %s", err) } - retv.messages = append(retv.messages, *obj) + retv.Messages = append(retv.Messages, *obj) } // Sort in ascending order - sort.Slice(retv.messages, func(i, j int) bool { - return retv.messages[i].Time.Before(retv.messages[j].Time) + sort.Slice(retv.Messages, func(i, j int) bool { + return retv.Messages[i].Time.Before(retv.Messages[j].Time) }) if err := scanner.Err(); err != nil {