Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/loggingctl/cat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright © 2024 John van Zantvoort <[email protected]>
*/
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")
}
47 changes: 47 additions & 0 deletions cmd/loggingctl/common.go
Original file line number Diff line number Diff line change
@@ -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)

}
*/
}
8 changes: 8 additions & 0 deletions cmd/loggingctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
goproj creates, maintains, archives and removes projects
*/
package main

func main() {
Execute()
}
48 changes: 48 additions & 0 deletions cmd/loggingctl/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright © 2024 John van Zantvoort <[email protected]>
*/
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")
}
8 changes: 4 additions & 4 deletions logging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type LogMessage struct {
}

type LogMessages struct {
messages []LogMessage
Messages []LogMessage
}

func GetString(cmd cobra.Command, name string) string {
Expand Down Expand Up @@ -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 {
Expand Down
Loading