Skip to content

Commit

Permalink
logs in a file (#753)
Browse files Browse the repository at this point in the history
* logs in a file

* taskfile: add install-plugin for windows

* Fix logger crash

---------

Co-authored-by: thomas-senechal <[email protected]>
  • Loading branch information
Thykof and thomas-senechal authored Sep 21, 2023
1 parent 2f7f817 commit 780baef
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ wallet_*.json
wallet-plugin*

coverage.out
internal/handler/wallet/unit-test.log
build/*
!build/appicon.png
node_modules
Expand Down
10 changes: 9 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tasks:
build-windows:
platforms: [windows]
cmds:
- cmd: mkdir build/wallet-plugin
- cmd: mkdir -p build/wallet-plugin
- cmd: go build -tags desktop,production -ldflags "-w -s -H windowsgui" -o build/wallet-plugin/wallet-plugin.exe main.go

build-webapp:
Expand Down Expand Up @@ -104,6 +104,14 @@ tasks:
platforms: [linux, darwin]
- cmd: cp manifest.json /usr/local/share/massastation/plugins/wallet-plugin
platforms: [linux, darwin]
- cmd: mkdir -p "C:/Program Files (x86)/MassaStation/plugins/wallet-plugin"
platforms: [windows]
- cmd: cp build/wallet-plugin/wallet-plugin.exe "C:/Program Files (x86)/MassaStation/plugins/wallet-plugin"
platforms: [windows]
- cmd: cp wallet.svg "C:/Program Files (x86)/MassaStation/plugins/wallet-plugin"
platforms: [windows]
- cmd: cp manifest.json "C:/Program Files (x86)/MassaStation/plugins/wallet-plugin"
platforms: [windows]

run:
cmds:
Expand Down
7 changes: 2 additions & 5 deletions api/server/restapi/configure_massa_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-openapi/runtime"
"github.com/massalabs/station-massa-wallet/api/server/restapi/operations"
"github.com/massalabs/station-massa-wallet/internal/handler/html"
"github.com/massalabs/station/pkg/logger"
"github.com/rs/cors"
)

Expand All @@ -25,11 +26,7 @@ func configureAPI(api *operations.MassaWalletAPI) http.Handler {
// configure the api here
api.ServeError = errors.ServeError

// Set your custom logger if needed. Default one is log.Printf
// Expected interface func(string, ...interface{})
//
// Example:
// api.Logger = log.Printf
api.Logger = logger.Infof

api.UseSwaggerUI()
// To continue using redoc as your UI, uncomment the following line
Expand Down
10 changes: 5 additions & 5 deletions cmd/massa-wallet/massa-wallet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"log"
"os"

"github.com/bluele/gcache"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/massalabs/station-massa-wallet/pkg/assets"
"github.com/massalabs/station-massa-wallet/pkg/network"
"github.com/massalabs/station-massa-wallet/pkg/prompt"
"github.com/massalabs/station/pkg/logger"
)

func StartServer(app *walletApp.WalletApp) {
Expand All @@ -29,7 +29,7 @@ func StartServer(app *walletApp.WalletApp) {

AssetsStore, err := assets.NewAssetsStore()
if err != nil {
log.Fatalf("Failed to create AssetsStore: %v", err)
logger.Fatalf("Failed to create AssetsStore: %v", err)
}

// Initializes API
Expand All @@ -40,7 +40,7 @@ func StartServer(app *walletApp.WalletApp) {
gc,
)
if err != nil {
log.Fatalln(err)
logger.Fatalf("Failed to initialize API: %v", err)
}

// instantiates and configure server
Expand All @@ -53,12 +53,12 @@ func StartServer(app *walletApp.WalletApp) {

listener, err := server.HTTPListener()
if err != nil {
log.Fatalln(err)
logger.Fatalf("Failed to create HTTP listener: %v", err)
}

plugin.RegisterPlugin(listener)

if err := server.Serve(); err != nil {
log.Fatalln(err)
logger.Fatalf("Failed to serve: %v", err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/bluele/gcache v0.0.2
github.com/go-openapi/runtime v0.25.0
github.com/jessevdk/go-flags v1.5.0
github.com/labstack/gommon v0.4.0
github.com/massalabs/station v0.3.5-0.20230918142757-8967f55f2177
github.com/massalabs/station-massa-hello-world v0.0.10
github.com/pkg/errors v0.9.1
Expand All @@ -27,6 +26,7 @@ require (
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect
github.com/klauspost/cpuid/v2 v2.1.0 // indirect
github.com/labstack/echo/v4 v4.10.2 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leaanthony/go-ansi-parser v1.6.0 // indirect
github.com/leaanthony/gosod v1.0.3 // indirect
github.com/leaanthony/slicer v1.6.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions internal/handler/wallet/add_assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wallet
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"testing"
Expand All @@ -11,11 +12,16 @@ import (
"github.com/massalabs/station-massa-wallet/api/server/restapi/operations"
utils "github.com/massalabs/station-massa-wallet/pkg/assets"
"github.com/massalabs/station-massa-wallet/pkg/wallet"
"github.com/massalabs/station/pkg/logger"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

func TestAddAssetHandler(t *testing.T) {
if err := logger.InitializeGlobal("./unit-test.log"); err != nil {
log.Fatalf("while initializing global logger: %s", err.Error())
}

api, _, _, _, err := MockAPI()
assert.NoError(t, err)

Expand Down
26 changes: 26 additions & 0 deletions internal/initialize/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package initialize

import (
"path/filepath"

"github.com/massalabs/station-massa-wallet/pkg/wallet"
"github.com/massalabs/station/pkg/logger"
)

const (
LogFileName = "station-massa-wallet.log"
)

func Logger() error {
logPath, err := wallet.AccountPath()
if err != nil {
return err
}

err = logger.InitializeGlobal(filepath.Join(logPath, LogFileName))
if err != nil {
return err
}

return nil
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"embed"
"fmt"
"log"

walletServer "github.com/massalabs/station-massa-wallet/cmd/massa-wallet"
"github.com/massalabs/station-massa-wallet/internal/initialize"
walletApp "github.com/massalabs/station-massa-wallet/pkg/app"
"github.com/massalabs/station-massa-wallet/pkg/wails"
"github.com/massalabs/station-massa-wallet/pkg/wallet"
"github.com/massalabs/station/pkg/logger"
)

//nolint:typecheck,nolintlint
Expand All @@ -17,6 +20,13 @@ var wailsAssets embed.FS
func main() {
app := walletApp.NewWalletApp()

err := initialize.Logger()
if err != nil {
log.Fatalf("Failed to initialize logger: %v", err)
}

defer logger.Close()

if walletApp.IsTestMode() {
fmt.Println("Wallet is running in test mode")
walletServer.StartServer(app)
Expand Down
4 changes: 2 additions & 2 deletions pkg/network/account.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package network

import (
"github.com/labstack/gommon/log"
"github.com/massalabs/station-massa-wallet/pkg/utils"
"github.com/massalabs/station-massa-wallet/pkg/wallet"
"github.com/massalabs/station/pkg/logger"
"github.com/massalabs/station/pkg/node"
)

Expand All @@ -13,7 +13,7 @@ func NewMassaClient() (*node.Client, error) {
return nil, err
}

log.Infof("Connected to node URL: %s", networkInfo.URL)
logger.Debugf("Connected to node URL: %s", networkInfo.URL)

return node.NewClient(networkInfo.URL), nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"io"
"net/http"

"github.com/labstack/gommon/log"
"github.com/massalabs/station-massa-hello-world/pkg/plugin"
"github.com/massalabs/station/pkg/logger"
)

const (
Expand All @@ -22,7 +22,7 @@ type NetworkInfo struct {
}

func logFallback(action string, err error) {
log.Warnf("Fallback to default network: failed to %s: %v", action, err)
logger.Warnf("Fallback to default network: failed to %s: %v", action, err)
}

// retrieve network info from endpoint
Expand Down
5 changes: 2 additions & 3 deletions pkg/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"strings"

"github.com/btcsuite/btcutil/base58"
"github.com/labstack/gommon/log"
"github.com/massalabs/station-massa-wallet/api/server/models"
"github.com/massalabs/station-massa-wallet/pkg/utils"
"github.com/massalabs/station/pkg/logger"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -286,7 +286,6 @@ func GetWorkDir() (string, error) {
}

// AccountPath returns the path where the account yaml file are stored.
// Note: the wallet directory is the folder where the wallet plugin binary resides.
func AccountPath() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
Expand Down Expand Up @@ -328,7 +327,7 @@ func LoadAll() ([]Wallet, error) {
wallet, loadErr := LoadFile(filePath)
wallets = append(wallets, wallet)
if loadErr != nil {
log.Errorf("while loading wallet '%s': %s", filePath, loadErr.Err)
logger.Errorf("while loading wallet '%s': %s", filePath, loadErr.Err)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion wails-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 780baef

Please sign in to comment.