Skip to content

Commit

Permalink
feat: Add Color Logging for Error Messages (#406)
Browse files Browse the repository at this point in the history
* fix: fix this bug scripts

* fix: set openim admin chat code
  • Loading branch information
cubxxw committed Feb 4, 2024
1 parent 7886317 commit a030b8a
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/scripts-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
sudo make start
sudo make check
sudo cat logs/openim_$(date '+%Y%m%d').log 2>/dev/null
sudo cat logs/chat_$(date '+%Y%m%d').log 2>/dev/null
echo "pwd = $(pwd)"
cd ..
ls -al && pwd
Expand Down Expand Up @@ -127,7 +127,7 @@ jobs:
run: |
sudo make install
- name: Print openim_$(date '+%Y%m%d').log
- name: Print chat_$(date '+%Y%m%d').log
run: |
ls -al && echo "pwd = $(pwd)"
sudo cat ./logs/* 2>/dev/null
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ copyright-add: tools.verify.addlicense
.PHONY: clean
clean:
@echo "===========> Cleaning all builds TMP_DIR($(TMP_DIR)) AND BIN_DIR($(BIN_DIR))"
@-rm -vrf $(TMP_DIR) $(BIN_DIR)
@-rm -vrf $(TMP_DIR) $(BIN_DIR) _output/logs
@echo "===========> End clean..."

## help: Show this help info.
Expand Down
19 changes: 13 additions & 6 deletions cmd/api/admin-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ package main

import (
"fmt"
"github.com/OpenIMSDK/tools/errs"
"math/rand"
"net"
"os"
"strconv"
"time"

"github.com/OpenIMSDK/tools/errs"

"github.com/OpenIMSDK/chat/pkg/discovery_register"

"github.com/OpenIMSDK/chat/tools/component"
Expand All @@ -44,7 +46,8 @@ func main() {
rand.Seed(time.Now().UnixNano())
configFile, ginPort, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}

// Check if the version flag was set
Expand All @@ -60,11 +63,13 @@ func main() {
}

if err := config.InitConfig(configFile); err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
err = component.ComponentCheck()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
if err := log.InitFromConfig("chat.log", "admin-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil {
panic(fmt.Errorf("InitFromConfig failed:%w", err))
Expand All @@ -77,7 +82,8 @@ func main() {
//zk, err = openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
// openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger()))
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}

if err := zk.CreateRpcRootNodes([]string{config.Config.RpcRegisterName.OpenImAdminName, config.Config.RpcRegisterName.OpenImChatName}); err != nil {
Expand All @@ -90,6 +96,7 @@ func main() {

address := net.JoinHostPort(config.Config.AdminApi.ListenIP, strconv.Itoa(ginPort))
if err := engine.Run(address); err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
}
20 changes: 13 additions & 7 deletions cmd/api/chat-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package main
import (
"flag"
"fmt"
"github.com/OpenIMSDK/tools/errs"
"math/rand"
"net"
"os"
"strconv"
"time"

"github.com/OpenIMSDK/tools/errs"

"github.com/OpenIMSDK/chat/pkg/discovery_register"
"github.com/OpenIMSDK/tools/discoveryregistry"

Expand Down Expand Up @@ -51,7 +53,8 @@ func main() {

configFile, ginPort, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}

// Check if the version flag was set
Expand All @@ -70,12 +73,13 @@ func main() {

err = config.InitConfig(configFile)
if err != nil {
fmt.Println("err ", err.Error())
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
err = component.ComponentCheck()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
if err := log.InitFromConfig("chat.log", "chat-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil {
panic(fmt.Errorf("InitFromConfig failed:%w", err))
Expand All @@ -89,7 +93,8 @@ func main() {
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username,
config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger()))*/
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
if err := zk.CreateRpcRootNodes([]string{config.Config.RpcRegisterName.OpenImAdminName, config.Config.RpcRegisterName.OpenImChatName}); err != nil {
panic(errs.Wrap(err, "CreateRpcRootNodes error"))
Expand All @@ -101,6 +106,7 @@ func main() {

address := net.JoinHostPort(config.Config.ChatApi.ListenIP, strconv.Itoa(ginPort))
if err := engine.Run(address); err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
}
13 changes: 9 additions & 4 deletions cmd/rpc/admin-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"flag"
"fmt"
"math/rand"
"os"
"time"

"github.com/OpenIMSDK/chat/pkg/common/chatrpcstart"
Expand All @@ -34,7 +35,8 @@ func main() {

configFile, rpcPort, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}

// Check if the version flag was set
Expand All @@ -52,11 +54,13 @@ func main() {
flag.Parse()

if err := config.InitConfig(configFile); err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
err = component.ComponentCheck()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
if config.Config.Envs.Discovery == "k8s" {
rpcPort = 80
Expand All @@ -66,6 +70,7 @@ func main() {
}
err = chatrpcstart.Start(rpcPort, config.Config.RpcRegisterName.OpenImAdminName, 0, admin.Start)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
}
13 changes: 9 additions & 4 deletions cmd/rpc/chat-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"fmt"
"math/rand"
"os"
"time"

"github.com/OpenIMSDK/chat/pkg/common/chatrpcstart"
Expand All @@ -32,7 +33,8 @@ func main() {
rand.Seed(time.Now().UnixNano())
configFile, rpcPort, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}

// Check if the version flag was set
Expand All @@ -48,20 +50,23 @@ func main() {
}

if err := config.InitConfig(configFile); err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
if config.Config.Envs.Discovery == "k8s" {
rpcPort = 80
}
err = component.ComponentCheck()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
if err := log.InitFromConfig("chat.log", "chat-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil {
panic(fmt.Errorf("InitFromConfig failed:%w", err))
}
err = chatrpcstart.Start(rpcPort, config.Config.RpcRegisterName.OpenImChatName, 0, chat.Start)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
}
15 changes: 11 additions & 4 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package api

import (
"context"
"fmt"
"os"

"github.com/OpenIMSDK/chat/example/callback"

"github.com/OpenIMSDK/chat/pkg/common/config"
Expand All @@ -26,11 +29,13 @@ import (
func NewChatRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegistry) {
chatConn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImChatName)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
adminConn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImAdminName)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
mw := NewMW(adminConn)
chat := NewChat(chatConn, adminConn)
Expand Down Expand Up @@ -66,11 +71,13 @@ func NewChatRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegis
func NewAdminRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegistry) {
adminConn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImAdminName)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
chatConn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImChatName)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
mw := NewMW(adminConn)
admin := NewAdmin(chatConn, adminConn)
Expand Down
10 changes: 7 additions & 3 deletions pkg/email/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ package email
import (
"context"
"errors"
"github.com/OpenIMSDK/chat/pkg/common/config"
"gopkg.in/yaml.v3"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/OpenIMSDK/chat/pkg/common/config"
"gopkg.in/yaml.v3"
)

func TestEmail(T *testing.T) {
if err := InitConfig(); err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
tests := []struct {
name string
Expand Down
5 changes: 4 additions & 1 deletion pkg/rpclient/chat/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package chat

import (
"context"
"fmt"
"os"

"github.com/OpenIMSDK/tools/discoveryregistry"

Expand All @@ -28,7 +30,8 @@ import (
func NewAdminClient(discov discoveryregistry.SvcDiscoveryRegistry) *AdminClient {
conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImAdminName)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
return &AdminClient{
client: admin.NewAdminClient(conn),
Expand Down
5 changes: 4 additions & 1 deletion pkg/rpclient/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package chat

import (
"context"
"fmt"
"os"

"github.com/OpenIMSDK/tools/discoveryregistry"
"github.com/OpenIMSDK/tools/errs"
Expand All @@ -29,7 +31,8 @@ import (
func NewChatClient(discov discoveryregistry.SvcDiscoveryRegistry) *ChatClient {
conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImChatName)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "\n\nexit -1: \n%+v\n\n", err)
os.Exit(-1)
}
return &ChatClient{
client: chat.NewChatClient(conn),
Expand Down
6 changes: 4 additions & 2 deletions scripts/admin_rpc_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ sleep 1
cd ${push_binary_root}

for ((i = 0; i < ${#rpc_ports[@]}; i++)); do
nohup ./${push_name} -port ${rpc_ports[$i]} -prometheus_port ${prome_ports[$i]} >>../logs/openim_$(date '+%Y%m%d').log 2>&1 &
nohup ./${push_name} -port ${rpc_ports[$i]} -prometheus_port ${prome_ports[$i]} >>../logs/chat_$(date '+%Y%m%d').log 2>&1 &
done

sleep 3
Expand All @@ -61,5 +61,7 @@ if [ $check -ge 1 ]; then
echo -e ${SKY_BLUE_PREFIX}"PID: "${COLOR_SUFFIX}${YELLOW_PREFIX}${newPid}${COLOR_SUFFIX}
echo -e ${SKY_BLUE_PREFIX}"LISTENING_PORT: "${COLOR_SUFFIX}${YELLOW_PREFIX}${allPorts}${COLOR_SUFFIX}
else
echo -e ${YELLOW_PREFIX}${push_name}${COLOR_SUFFIX}${RED_PREFIX}"SERVICE START ERROR, PLEASE CHECK openim_$(date '+%Y%m%d').log"${COLOR_SUFFIX}
echo -e ${YELLOW_PREFIX}${push_name}${COLOR_SUFFIX}${RED_PREFIX}"SERVICE START ERROR, PLEASE CHECK chat_$(date '+%Y%m%d').log"${COLOR_SUFFIX}
fi

echo -e ${SKY_BLUE_PREFIX}"# Start Chat push_rpc.sh "$(date +%H:%M:%S)" For local deployments, use ./push_rpc.sh --print-screen"${COLOR_SUFFIX}
8 changes: 0 additions & 8 deletions scripts/build_all_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@ source $SCRIPTS_ROOT/style_info.sh
source $SCRIPTS_ROOT/path_info.sh
source $SCRIPTS_ROOT/function.sh

echo -e "${YELLOW_PREFIX}=======>SCRIPTS_ROOT=$SCRIPTS_ROOT${COLOR_SUFFIX}"
echo -e "${YELLOW_PREFIX}=======>OPENIM_ROOT=$OPENIM_ROOT${COLOR_SUFFIX}"
echo -e "${YELLOW_PREFIX}=======>pwd=$PWD${COLOR_SUFFIX}"

echo -e ""

echo -e "${BACKGROUND_BLUE}===============> Building all using make build binary files ${COLOR_SUFFIX}"

echo -e ""
echo -e "${BOLD_PREFIX}____________________________________________________________ ${COLOR_SUFFIX}"


bin_dir="$BIN_DIR"
logs_dir="$OPENIM_ROOT/_output/logs"
Expand Down
Loading

0 comments on commit a030b8a

Please sign in to comment.