Skip to content

Commit

Permalink
Merge pull request #49 from tomochain/features/optimize-logs
Browse files Browse the repository at this point in the history
Features/optimize logs
  • Loading branch information
piavgh authored Apr 25, 2019
2 parents 7953dcb + 18f12a2 commit b3cf6aa
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 71 deletions.
3 changes: 3 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type appConfig struct {
CoinmarketcapAPIKey string `mapstructure:"coinmarketcap_api_key"`

SupportedCurrencies string `mapstructure:"supported_currencies"`

Env string `mapstructure:"env"`
}

func (config appConfig) Validate() error {
Expand Down Expand Up @@ -103,5 +105,6 @@ func LoadConfig(configPath string, env string) error {
logger.Infof("RabbitMQ url: %v", Config.RabbitMQURL)
logger.Infof("Exchange contract address: %v", Config.Ethereum["exchange_address"])
logger.Infof("Fee Account: %v", Config.Ethereum["fee_account"])
logger.Infof("Env: %v", Config.Env)
return Config.Validate()
}
3 changes: 2 additions & 1 deletion config/config.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ jwt_signing_key: QfCAH04Cob7b71QCqy738vw5XGSnFZ9d

coinmarketcap_api_url: https://pro-api.coinmarketcap.com/v1
coinmarketcap_api_key: a928d4ca-37cc-41b8-a9ea-5a65ae025aa5
supported_currencies: ETH,TOMO,BTC,USDT
supported_currencies: ETH,TOMO,BTC,USDT
env: dev
3 changes: 2 additions & 1 deletion config/config.local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ jwt_signing_key: QfCAH04Cob7b71QCqy738vw5XGSnFZ9d
coinmarketcap_api_url: https://pro-api.coinmarketcap.com/v1
coinmarketcap_api_key: a928d4ca-37cc-41b8-a9ea-5a65ae025aa5
supported_currencies: ETH,TOMO,BTC,USDT # For getting fiat price from coinmarketcap.com
#supported_currencies: ETH,TOMO # For getting fiat price from coinmarketcap.com
#supported_currencies: ETH,TOMO # For getting fiat price from coinmarketcap.com
env: local
3 changes: 2 additions & 1 deletion config/config.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ jwt_signing_key: QfCAH04Cob7b71QCqy738vw5XGSnFZ9d

coinmarketcap_api_url: https://pro-api.coinmarketcap.com/v1
coinmarketcap_api_key: a928d4ca-37cc-41b8-a9ea-5a65ae025aa5
supported_currencies: ETH,TOMO,BTC,USDT
supported_currencies: ETH,TOMO,BTC,USDT
env: prod
3 changes: 2 additions & 1 deletion config/config.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ tick_duration:

coinmarketcap_api_url: https://pro-api.coinmarketcap.com/v1
coinmarketcap_api_key: a928d4ca-37cc-41b8-a9ea-5a65ae025aa5
supported_currencies: ETH,TOMO,BTC,USDT
supported_currencies: ETH,TOMO,BTC,USDT
env: test
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ deposit:
signer_private_key: 0x7f4c1bacba63f05827f6d8fc0e22cf68c42005775a7f73abff7d819986bae77c
starting_balance: 100
token_asset_code: WETH
env: local
error_file: config/errors.yaml
ethereum:
decimal: 8
Expand Down
2 changes: 1 addition & 1 deletion endpoints/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package endpoints

import "github.com/tomochain/tomodex/utils"

var logger = utils.APILogger
var logger = utils.Logger
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Engine struct {
provider *ethereum.EthereumProvider
}

var logger = utils.EngineLogger
var logger = utils.Logger

// NewEngine initializes the engine singleton instance
func NewEngine(
Expand Down
2 changes: 1 addition & 1 deletion rabbitmq/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var conn *Connection
var channels = make(map[string]*amqp.Channel)
var queues = make(map[string]*amqp.Queue)

var logger = utils.RabbitLogger
var logger = utils.Logger

type Connection struct {
Conn *amqp.Connection
Expand Down
1 change: 0 additions & 1 deletion services/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

var logger = utils.Logger
var engineLogger = utils.EngineLogger

var ErrPairExists = errors.New("Pairs already exists")
var ErrPairNotFound = errors.New("Pair not found")
Expand Down
2 changes: 1 addition & 1 deletion swap/bitcoin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/tyler-smith/go-bip32"
)

var logger = utils.BitcoinListenerLogger
var logger = utils.Logger

var (
eight = big.NewInt(8)
Expand Down
2 changes: 1 addition & 1 deletion swap/ethereum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/tomochain/tomodex/utils"
)

var logger = utils.EthereumListenerLogger
var logger = utils.Logger

var (
ten = big.NewInt(10)
Expand Down
2 changes: 1 addition & 1 deletion swap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// swap is engine
var logger = utils.EngineLogger
var logger = utils.Logger

// JS SDK use to communicate.
const ProtocolVersion int = 2
Expand Down
2 changes: 1 addition & 1 deletion swap/tomochain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Status describes status of account processing
type Status string

var logger = utils.EngineLogger
var logger = utils.Logger

const (
StatusCreatingAccount Status = "creating_account"
Expand Down
76 changes: 25 additions & 51 deletions utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path"
"runtime"

logging "github.com/op/go-logging"
"github.com/op/go-logging"
)

// TODO: add log prefix to params
Expand All @@ -16,40 +16,17 @@ const (
)

var Logger = NewLogger("main", "./logs/main.log")
var EngineLogger = NewLogger("engine", "./logs/engine.log")
var EthereumListenerLogger = NewLogger("ethereum", "./logs/ethereum.log")
var BitcoinListenerLogger = NewLogger("bitcoin", "./logs/bitcoin.log")
var EthereumLogger = NewLogger("engine", "./logs/engine.log")
var APILogger = NewLogger("api", "./logs/api.log")
var RabbitLogger = NewLogger("rabbitmq", "./logs/rabbit.log")

var NoopLogger = NewNoopLogger()
var TerminalLogger = NewColoredLogger()

var StdoutLogger = NewStandardOutputLogger()
var MainLogger = NewMainLogger()
var ErrorLogger = NewErrorLogger()
var WebsocketMessagesLogger = NewFileLogger("websocket", "./logs/websocket.log")
var OperatorMessagesLogger = NewFileLogger("operator", "./logs/operator.log")

func NewNoopLogger() *logging.Logger {
logger, err := logging.GetLogger("noop")
if err != nil {
panic(err)
}
noopBackend := logging.NewLogBackend(ioutil.Discard, "", 0)
formattedBackend := logging.NewBackendFormatter(noopBackend, logging.DefaultFormatter)
leveledBackend := logging.AddModuleLevel(formattedBackend)
logger.SetBackend(leveledBackend)
return logger
}
var TerminalLogger = NewColoredLogger()

func NewStandardOutputLogger() *logging.Logger {
// NewFileLogger creates a logging utility that outputs to the file passed as argument and also output to stdout.
func NewLogger(module string, logFile string) *logging.Logger {
_, fileName, _, _ := runtime.Caller(1)
logDir := path.Join(path.Dir(fileName), "../logs/")
mainLogFile := path.Join(path.Dir(fileName), "../logs/main.log")
logFile = path.Join(path.Dir(fileName), "../", logFile)

logger, err := logging.GetLogger("main")
logger, err := logging.GetLogger(module)
if err != nil {
panic(err)
}
Expand All @@ -62,12 +39,12 @@ func NewStandardOutputLogger() *logging.Logger {
os.Mkdir(logDir, os.ModePerm)
}

mainLog, err := os.OpenFile(mainLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
log, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}

writer := io.MultiWriter(os.Stdout, mainLog)
writer := io.MultiWriter(os.Stdout, log)
backend := logging.NewLogBackend(writer, LogPrefix, 0)

formattedBackend := logging.NewBackendFormatter(backend, format)
Expand All @@ -77,13 +54,12 @@ func NewStandardOutputLogger() *logging.Logger {
return logger
}

func NewLogger(module string, logFile string) *logging.Logger {
// NewFileLogger creates a logging utility that only output to stdout.
func NewStandardOutputLogger() *logging.Logger {
_, fileName, _, _ := runtime.Caller(1)
logDir := path.Join(path.Dir(fileName), "../logs/")
mainLogFile := path.Join(path.Dir(fileName), "../logs/main.log")
logFile = path.Join(path.Dir(fileName), "../", logFile)

logger, err := logging.GetLogger(module)
logger, err := logging.GetLogger("main")
if err != nil {
panic(err)
}
Expand All @@ -96,17 +72,7 @@ func NewLogger(module string, logFile string) *logging.Logger {
os.Mkdir(logDir, os.ModePerm)
}

mainLog, err := os.OpenFile(mainLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}

log, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}

writer := io.MultiWriter(os.Stdout, mainLog, log)
writer := io.MultiWriter(os.Stdout)
backend := logging.NewLogBackend(writer, LogPrefix, 0)

formattedBackend := logging.NewBackendFormatter(backend, format)
Expand Down Expand Up @@ -150,12 +116,8 @@ func NewFileLogger(module string, logFile string) *logging.Logger {
return logger
}

func NewMainLogger() *logging.Logger {
return NewFileLogger("main", "./logs/main.log")
}

func NewErrorLogger() *logging.Logger {
return NewFileLogger("error", "./logs/errors.log")
return NewLogger("error", "./logs/errors.log")
}

func NewColoredLogger() *logging.Logger {
Expand All @@ -177,3 +139,15 @@ func NewColoredLogger() *logging.Logger {
logger.SetBackend(leveledBackend)
return logger
}

func NewNoopLogger() *logging.Logger {
logger, err := logging.GetLogger("noop")
if err != nil {
panic(err)
}
noopBackend := logging.NewLogBackend(ioutil.Discard, "", 0)
formattedBackend := logging.NewBackendFormatter(noopBackend, logging.DefaultFormatter)
leveledBackend := logging.AddModuleLevel(formattedBackend)
logger.SetBackend(leveledBackend)
return logger
}
12 changes: 10 additions & 2 deletions ws/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gorilla/websocket"
"github.com/tomochain/tomodex/app"
"github.com/tomochain/tomodex/types"
)

Expand Down Expand Up @@ -75,7 +76,11 @@ func readHandler(c *Client) {
return
}

logger.LogMessageIn(&msg)
// Only log WS messages in local environment
if app.Config.Env == "local" {
logger.LogMessageIn(&msg)
}

logger.Infof("%v", msg.String())

if socketChannels[msg.Channel] == nil {
Expand Down Expand Up @@ -111,7 +116,10 @@ func writeHandler(c *Client) {
c.WriteMessage(websocket.CloseMessage, []byte{})
}

logger.LogMessageOut(&m)
if app.Config.Env == "local" {
logger.LogMessageOut(&m)
}

logger.Infof("%v", m.String())

err := c.WriteJSON(m)
Expand Down
8 changes: 2 additions & 6 deletions ws/log.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package ws

import (
logging "github.com/op/go-logging"
"github.com/op/go-logging"
"github.com/tomochain/tomodex/types"
"github.com/tomochain/tomodex/utils"
)

type WebsocketLogger struct {
*logging.Logger
mainLogger *logging.Logger
websocketMessageLogger *logging.Logger
}

func NewWebsocketLogger() *WebsocketLogger {
return &WebsocketLogger{
utils.StdoutLogger,
utils.MainLogger,
utils.WebsocketMessagesLogger,
utils.Logger,
}
}

func (l *WebsocketLogger) LogMessageIn(msg *types.WebsocketMessage) {
l.mainLogger.Infof("Receiving %v/%v message", msg.Channel, msg.Event.Type, utils.JSON(msg))
l.websocketMessageLogger.Infof("Receiving %v/%v message", msg.Channel, msg.Event.Type, utils.JSON(msg))
}

func (l *WebsocketLogger) LogMessageOut(msg *types.WebsocketMessage) {
l.mainLogger.Infof("Receiving %v/%v message", msg.Channel, msg.Event.Type, utils.JSON(msg))
l.websocketMessageLogger.Infof("Sending %v/%v message", msg.Channel, msg.Event.Type, utils.JSON(msg))
}

0 comments on commit b3cf6aa

Please sign in to comment.