[internal/logging] add logging lib

This commit is contained in:
2023-11-03 14:32:27 +01:00
parent 7256763c20
commit 2a530681df
21 changed files with 148 additions and 61 deletions

View File

@@ -36,7 +36,7 @@ func (a StagedActions) executeInternal(f func(Action)) {
continue
}
fmt.Printf("Execute Stage %v\n", idx+1)
internal.Log.Infof("Execute Stage %v", idx+1)
for actionidx, _ := range stage.Actions {
go f(stage.Actions[actionidx])
}
@@ -45,7 +45,7 @@ func (a StagedActions) executeInternal(f func(Action)) {
err := <-a.ActionChan
if err != nil {
fmt.Printf("Error occured on Stage %d: %s\n", idx+1, err)
internal.Log.Errorf("Error occured on Stage %d: %s", idx+1, err)
}
}
}

View File

@@ -2,7 +2,6 @@ package actions
import (
"encoding/json"
"fmt"
"unknown.com/gokill/internal"
)
@@ -13,12 +12,12 @@ type Printer struct {
}
func (p Printer) Execute() {
fmt.Printf("Print action fires. Message: %s\n", p.Message)
internal.LogDoc(p).Infof("Print action fires. Message: %s", p.Message)
p.ActionChan <- nil
}
func (p Printer) DryExecute() {
fmt.Printf("Print action fire test. Message: %s\n", p.Message)
internal.LogDoc(p).Infof("Print action fire test. Message: %s", p.Message)
p.ActionChan <- nil
}

View File

@@ -53,7 +53,7 @@ func (s SendMatrix) sendMessage(message string) error {
client.Crypto = cryptoHelper
fmt.Println("Matrix Client Now running")
internal.LogDoc(s).Info("Matrix Client Now running")
syncCtx, cancelSync := context.WithCancel(context.Background())
var syncStopWait sync.WaitGroup
syncStopWait.Add(1)
@@ -72,8 +72,8 @@ func (s SendMatrix) sendMessage(message string) error {
if err != nil {
return fmt.Errorf("Failed to send event")
} else {
fmt.Println("Matrix Client: Message sent")
fmt.Printf("Matrix Client: event_id: %s", resp.EventID.String())
internal.LogDoc(s).Info("Matrix Client: Message sent")
internal.LogDoc(s).Infof("Matrix Client: event_id: %s", resp.EventID.String())
}
cancelSync()
@@ -87,22 +87,22 @@ func (s SendMatrix) sendMessage(message string) error {
}
func (s SendMatrix) DryExecute() {
fmt.Println("SendMatrix: Trying to send test message")
internal.LogDoc(s).Info("SendMatrix: Trying to send test message")
err := s.sendMessage(s.TestMessage)
if err != nil {
fmt.Println("SendMatrix: failed to send test message")
internal.LogDoc(s).Info("SendMatrix: failed to send test message")
}
s.ActionChan <- err
}
func (s SendMatrix) Execute() {
fmt.Println("SendMatrix: Trying to send message")
internal.LogDoc(s).Info("SendMatrix: Trying to send message")
err := s.sendMessage(s.Message)
if err != nil {
fmt.Println("SendMatrix: failed to send message")
internal.LogDoc(s).Info("SendMatrix: failed to send message")
}
s.ActionChan <- err

View File

@@ -24,9 +24,6 @@ func (s SendTelegram) sendMessage(message string) error {
}
bot.Debug = true
fmt.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
@@ -37,22 +34,22 @@ func (s SendTelegram) sendMessage(message string) error {
}
func (s SendTelegram) DryExecute() {
fmt.Println("SendTelegram: Trying to send test message")
err := s.sendMessage(s.TestMessage)
internal.LogDoc(s).Info("SendTelegram: Trying to send test message")
err := s.sendMessage(s.TestMessage)
if err != nil {
fmt.Println("SendTelegram: failed to send test message")
internal.LogDoc(s).Info("SendTelegram: failed to send test message")
}
s.ActionChan <- err
}
func (s SendTelegram) Execute() {
fmt.Println("SendTelegram: Trying to send message")
internal.LogDoc(s).Info("SendTelegram: Trying to send message")
err := s.sendMessage(s.Message)
if err != nil {
fmt.Println("SendTelegram: failed to send message")
internal.LogDoc(s).Info("SendTelegram: failed to send message")
}
s.ActionChan <- err

View File

@@ -18,7 +18,6 @@ func isExecutableFile(path string) bool {
fi, err := os.Lstat(path)
if err != nil {
fmt.Println("Test executing Shellscript Failed.")
return false
}
@@ -33,18 +32,18 @@ func isExecutableFile(path string) bool {
}
func (c ShellScript) DryExecute() {
fmt.Printf("Test Executing ShellScript:\n%s\n", c.Path)
internal.LogDoc(c).Infof("Test Executing ShellScript:\n%s", c.Path)
_, err := os.Open(c.Path)
if err != nil {
fmt.Println("Test executing Shellscript Failed.")
internal.LogDoc(c).Warning("Test executing Shellscript Failed.")
c.ActionChan <- err
return
}
if !isExecutableFile(c.Path) {
fmt.Println("Test executing Shellscript Failed.")
internal.LogDoc(c).Warning("Test executing Shellscript Failed.")
c.ActionChan <- fmt.Errorf("File is not executable: %s", c.Path)
return
}
@@ -54,7 +53,7 @@ func (c ShellScript) DryExecute() {
func (c ShellScript) Execute() {
if !isExecutableFile(c.Path) {
fmt.Println("Test executing Shellscript Failed.")
internal.LogDoc(c).Warning("Executing Shellscript Failed.")
c.ActionChan <- fmt.Errorf("File is not executable: %s", c.Path)
return
}
@@ -64,11 +63,11 @@ func (c ShellScript) Execute() {
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
c.ActionChan <- err
internal.LogDoc(c).Warning("Failed to execute Shellscript")
c.ActionChan <- fmt.Errorf("Error during ShellScript execute: %s", err)
}
fmt.Println(string(stdout[:]))
internal.LogDoc(c).Infof("Shellscript output:\n%s", string(stdout[:]))
c.ActionChan <- nil
}

View File

@@ -1,7 +1,6 @@
package actions
import (
"fmt"
"os/exec"
"encoding/json"
@@ -14,17 +13,17 @@ type Shutdown struct {
}
func (s Shutdown) DryExecute() {
fmt.Printf("shutdown -h %s\n", s.Timeout)
fmt.Println("Test Shutdown executed...")
internal.LogDoc(s).Infof("shutdown -h %s", s.Timeout)
internal.LogDoc(s).Info("Test Shutdown executed...")
s.ActionChan <- nil
}
func (s Shutdown) Execute() {
if err := exec.Command("shutdown", "-h", s.Timeout).Run(); err != nil {
fmt.Println("Failed to initiate shutdown:", err)
internal.LogDoc(s).Errorf("Failed to initiate shutdown: %s", err)
}
fmt.Println("Shutdown executed...")
internal.LogDoc(s).Notice("Shutdown executed...")
s.ActionChan <- nil
}
@@ -37,7 +36,7 @@ func (s Shutdown) Create(config internal.ActionConfig, c ActionResultChan) (Acti
err := json.Unmarshal(config.Options, &result)
if err != nil {
fmt.Println("Parsing Shutdown options failed.")
internal.LogDoc(s).Warning("Parsing Shutdown options failed.")
return Shutdown{}, err
}

View File

@@ -2,7 +2,6 @@ package actions
import (
"encoding/json"
"fmt"
"time"
"unknown.com/gokill/internal"
@@ -18,7 +17,7 @@ func (t TimeOut) DryExecute() {
}
func (t TimeOut) Execute() {
fmt.Printf("Waiting %d seconds\n", t.Duration)
internal.LogDoc(t).Infof("Waiting %d seconds", t.Duration)
time.Sleep(time.Duration(t.Duration) * time.Second)
t.ActionChan <- nil
}

View File

@@ -24,19 +24,19 @@ func isCommandAvailable(name string) bool {
}
func (c Command) DryExecute() {
fmt.Printf("Test Executing Command:\n%s\n", c.Command)
internal.LogDoc(c).Infof("Test Executing Command: %s", c.Command)
command, _, err := c.splitCommandString()
if err != nil {
fmt.Printf("Error during argument parsing of command '%s'\n", c.Command)
fmt.Println(err)
internal.LogDoc(c).Errorf("Error during argument parsing of command '%s'", c.Command)
c.ActionChan <- fmt.Errorf("Error on Command action: %s", err)
return
}
isAvailable := isCommandAvailable(command)
if !isAvailable {
fmt.Printf("Command %s not found\n", command)
internal.LogDoc(c).Warningf("Command %s not found", command)
c.ActionChan <- fmt.Errorf("Command %s not found!", command)
return
}
@@ -60,7 +60,7 @@ func (c Command) splitCommandString() (string, []string, error) {
func (c Command) Execute() {
command, args, err := c.splitCommandString()
fmt.Println("Executing command: ", c.Command)
internal.LogDoc(c).Infof("Executing command: ", c.Command)
if err != nil {
c.ActionChan <- err
@@ -72,11 +72,11 @@ func (c Command) Execute() {
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
c.ActionChan <- err
internal.LogDoc(c).Errorf("%s", err.Error())
c.ActionChan <- fmt.Errorf("Executing Command '%s' failed: %s", c.Command, err)
}
fmt.Println(string(stdout[:]))
internal.LogDoc(c).Infof("Command Output:\n%s", string(stdout[:]))
c.ActionChan <- nil
}