Skip to content

Commit

Permalink
Merge pull request #1 from dkyanakiev/feature/swapping-logger
Browse files Browse the repository at this point in the history
Moving to zlog for logging
  • Loading branch information
dkyanakiev authored Nov 28, 2023
2 parents 1093c58 + 729fa36 commit f217fb5
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 1,002 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ setup-test-data:

.PHONY: install-osx
install-osx:
cp ./bin/vaul7y /usr/local/bin/vaul7y
cp ./bin/vaul7y/vaul7y /usr/local/bin/vaul7y

.PHONY: dev
dev: ## Build for the current development version
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ The tool is in active development and is bug heavy. There are multiple things th
If anyone decides to use this and wants to request a specific feature or even fix a bug - please open an issue :smile:

## Short term TODO list:
1. Change the logger (current one is a mess)
1. Finish adding tests (wip in another branch)
2. Finish implementing Update to existing secrets
* Bonus: Create net new ones.
3. Support for namespace changes.
1. [x] Change the logger (current one is a mess)
2. [ ] Finish adding tests (wip in another branch)
3. [ ] Finish implementing Update to existing secrets
- [ ] Bonus: Create net new ones.
4. [ ] Support for namespace changes.
42 changes: 2 additions & 40 deletions cmd/vaul7y/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"log"
"os"
"time"

"github.com/dkyanakiev/vaulty/component"
"github.com/dkyanakiev/vaulty/config"
"github.com/dkyanakiev/vaulty/state"
"github.com/dkyanakiev/vaulty/vault"
"github.com/dkyanakiev/vaulty/view"
Expand All @@ -15,28 +15,11 @@ import (
)

var refreshIntervalDefault = time.Second * 5
var logger *log.Logger

func main() {

//

LOG_FILE, exists := os.LookupEnv("VAULTY_LOG_FILE")
if !exists {
LOG_FILE = "/tmp/vaulty-errors"
} // open log file
logFile, err := os.OpenFile(LOG_FILE, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Panic(err)
}
logFile, logger := config.SetupLogger()
defer logFile.Close()

logger = log.New(logFile, "Vaulty ", log.LstdFlags)
logger.SetOutput(logFile)

// optional: log date-time, filename, and line number
logger.SetFlags(log.Lshortfile | log.LstdFlags)

tview.Styles.PrimitiveBackgroundColor = tcell.NewRGBColor(40, 44, 48)

vaultClient, err := vault.New(func(v *vault.Vault) error {
Expand Down Expand Up @@ -88,27 +71,6 @@ func initializeState(client *vault.Vault) *state.State {
return state
}

func initLogger() {

// TODO Rework later
LOG_FILE := "/tmp/vaulty-errors"
// open log file
logFile, err := os.OpenFile(LOG_FILE, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Panic(err)
}
defer logFile.Close()

logger = log.New(logFile, "app ", log.LstdFlags)

// Set log out put and enjoy :)
logger.SetOutput(logFile)

// optional: log date-time, filename, and line number
logger.SetFlags(log.Lshortfile | log.LstdFlags)

}

// // LOOK AT LATER
// func main() {
// vaultClient, _ := vault.New(vault.Default)
Expand Down
6 changes: 3 additions & 3 deletions component/mounts_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package component

import (
"fmt"
"log"
"sort"
"time"

Expand All @@ -11,6 +10,7 @@ import (
"github.com/dkyanakiev/vaulty/styles"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/rs/zerolog"
)

const (
Expand All @@ -31,7 +31,7 @@ type SelectMountPathFunc func(mountPath string)
type MountsTable struct {
Table Table
Props *MountsTableProps
Logger *log.Logger
Logger *zerolog.Logger

slot *tview.Flex
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (m *MountsTable) GetIDForSelection() string {
func (m *MountsTable) validate() error {
// TODO: Revisid validation
if m.Props.SelectedMount == "" || m.Props.HandleNoResources == nil {
log.Println("Random error: " + ErrComponentPropsNotSet)
m.Logger.Err(ErrComponentPropsNotSet).Msgf("Random error: %s", ErrComponentPropsNotSet)
}

if m.slot == nil {
Expand Down
14 changes: 7 additions & 7 deletions component/secret_obj_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package component
import (
"encoding/json"
"fmt"
"log"
"sort"

"github.com/dkyanakiev/vaulty/models"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/hashicorp/vault/api"
"github.com/rivo/tview"
"github.com/rs/zerolog"
)

const (
Expand All @@ -36,7 +36,7 @@ type SecretObjTable struct {
Form Form

Props *SecretObjTableProps
Logger *log.Logger
Logger *zerolog.Logger
slot *tview.Flex
ShowJson bool
Editable bool
Expand Down Expand Up @@ -173,7 +173,7 @@ func (s *SecretObjTable) renderJson() {
data := s.Props.Data.Data["data"].(map[string]interface{})
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
s.Logger.Println("error:", err)
s.Logger.Err(err).Msgf("error: %s", err)
}
s.TextView.SetText(string(jsonData))
}
Expand All @@ -182,7 +182,7 @@ func (s *SecretObjTable) renderEditField() {
data := s.Props.UpdatedData
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
s.Logger.Println("error:", err)
s.Logger.Err(err).Msgf("error: %s", err)
}
s.TextView.SetText(string(jsonData))

Expand All @@ -191,12 +191,12 @@ func (s *SecretObjTable) renderEditField() {
func (s *SecretObjTable) SaveData(text string) string {
var data map[string]interface{}

s.Logger.Println("Saving data")
s.Logger.Println(text)
s.Logger.Debug().Msg("Saving data")
s.Logger.Debug().Msg(text)

err := json.Unmarshal([]byte(text), &data)
if err != nil {
s.Logger.Println("Failed to validate json:", err)
s.Logger.Err(err).Msgf("Failed to validate json:: %s", err)
return "Failed to validate json:"
}
s.Props.UpdatedData = data
Expand Down
41 changes: 41 additions & 0 deletions config/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

import (
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func SetupLogger() (*os.File, *zerolog.Logger) {

// UNIX Time is faster and smaller than most timestamps
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

logLevel, debugOn := os.LookupEnv("VAULTY_LOG_LEVEL")

// Default level for this example is info, unless debug flag is present
if debugOn {
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
log.Fatal().Err(err).Msg("Invalid log level")
}
zerolog.SetGlobalLevel(level)
}
var logFile *os.File

// Check if file for logging is set
LOG_FILE, exists := os.LookupEnv("VAULTY_LOG_FILE")
if exists {
logFile, err := os.OpenFile(LOG_FILE, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Panic().Err(err).Msg("Error opening log file")
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: logFile, TimeFormat: zerolog.TimeFieldFormat})
} else {
// If no log file is set, write to stdout
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}

return logFile, &log.Logger
}
8 changes: 7 additions & 1 deletion docs/local-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ $ make setup-test-data
3. Configure the env variables required to auth to vault

4. Make sure to set
`VAULTY_LOG_FILE` env variable and point to a file, to log to a file
`VAULTY_LOG_FILE` env variable and point to a file, to log to a file
`VAULTY_LOG_LEVEL` env variable - define the log level you want to use

```
❯ export VAULTY_LOG_LEVEL=debug
❯ export VAULTY_LOG_FILE=/tmp/my-vault-log.log
```
49 changes: 8 additions & 41 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,24 @@ module github.com/dkyanakiev/vaulty
go 1.20

require (
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/glamour v0.6.0
github.com/gdamore/tcell/v2 v2.6.0
github.com/hashicorp/vault/api v1.10.0
github.com/lab47/vterm v0.0.0-20211107042118-80c3d2849f9c
github.com/rivo/tview v0.0.0-20230907083354-a39fe28ba466
gopkg.in/telebot.v3 v3.1.3
github.com/rs/zerolog v1.31.0
)

require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/charmbracelet/bubbles v0.16.1 // indirect
github.com/charmbracelet/bubbletea v0.24.1 // indirect
github.com/charmbracelet/glamour v0.6.0 // indirect
github.com/charmbracelet/lipgloss v0.9.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/goccy/go-yaml v1.9.5 // indirect
github.com/go-test/deep v1.0.3 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -43,48 +33,25 @@ require (
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.13.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/y0ssar1an/q v1.0.7 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/yuin/goldmark v1.5.2 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
github.com/zclconf/go-cty v1.13.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit f217fb5

Please sign in to comment.