Skip to content

Commit

Permalink
Merge pull request #28 from lixiaojun629/develop
Browse files Browse the repository at this point in the history
add rotate log
  • Loading branch information
lixiaojun629 authored May 7, 2019
2 parents 03f0385 + 3d62aac commit ab51416
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 131 deletions.
47 changes: 42 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
## Change Log
v0.1.16
* Support log rotation. Log file path $HOME/.ucloud/cli.log.
* Bugfix for display nothing when uhost create failed

v0.1.15
* Update documents
* Add test for uhost

v0.1.14
* Create uhost concurrently

v0.1.13
* Update version of ucloud-sdk-go to fix bug

v0.1.12
* Preliminary support umem

v0.1.11
* Use go modules to manage dependencies
* Fix bug for uhost clone

v0.1.10
* Support udb mysql

v0.1.9
* Better flag value completion with local cache and multiple resource ID completion
* Command structure adjustment
- ucloud bw-pkg => ucloud bw pkg
- ucloud shared-bw => ucloud bw shared
- ucloud ulb-vserver => ucloud ulb vserver
- ucloud ulb-ssl-certificate => ucloud ulb ssl
- ucloud ulb-vserver add-node/update-node/delete-node/list-node => ucloud ulb vserver backend add/update/delete/list
- ucloud ulb-vserver add-policy/list-policy/update-policy/delete-policy => ucloud ulb vserver policy add/list/update/delete

v0.1.8
* Support ulb

v0.1.7
* Add udpn, firewall, shared bandwidth and bandwidth package; Refactor vpc, subnet and eip

Expand All @@ -9,16 +46,16 @@ v0.1.5
* support batch operation.

v0.1.4
* add udisk.
* polling udisk and uhost long time operation
* async complete resource-id
* Support udisk.
* Polling udisk and uhost long time operation
* Async complete resource-id

v0.1.3
* integrate auto completion.
* Integrate auto completion.
* Support uhost create, stop, delete and so on.

v0.1.2
* simplify config and completion.
* Simplify config and completion.

v0.1.1
* UHost list; EIP list,delete and allocate; GlobalSSH list,delete,modify and create.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export VERSION=0.1.15
export VERSION=0.1.16

.PHONY : build
build:
Expand Down
2 changes: 1 addition & 1 deletion base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
const DefaultProfile = "default"

//Version 版本号
const Version = "0.1.15"
const Version = "0.1.16"

//ConfigIns 配置实例, 程序加载时生成
var ConfigIns = &AggConfig{}
Expand Down
120 changes: 120 additions & 0 deletions base/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

package base

import (
"os"
"sync"
"fmt"

log "github.com/sirupsen/logrus"
"github.com/ucloud/ucloud-sdk-go/ucloud/request"
)

//Logger 日志
var logger *log.Logger
var mu sync.Mutex

func init() {
initLog()
}

func initLog(){
file, err := os.OpenFile(GetLogFilePath(), os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println("open log file failed: ", err)
return
}
logger = log.New()
logger.SetNoLock()
logger.AddHook(NewLogRotateHook(file))
logger.SetOutput(file)
}

//GetLogFilePath 获取日志文件路径
func GetLogFilePath() string {
return GetHomePath() + fmt.Sprintf("/%s/cli.log", ConfigPath)
}

//Log 记录日志
func Log(logs []string) {
mu.Lock()
defer mu.Unlock()
logger.Info("=============================================================")
for _, line := range logs {
logger.Info(line)
}
}

//LogRotateHook rotate log file
type LogRotateHook struct {
MaxSize int64
Cut float32
LogFile *os.File
mux sync.Mutex
}

//Levels fires hook
func (hook *LogRotateHook) Levels() []log.Level {
return log.AllLevels
}

//Fire do someting when hook is triggered
func (hook *LogRotateHook) Fire(entry *log.Entry) error {
hook.mux.Lock()
defer hook.mux.Unlock()
info, err := hook.LogFile.Stat()
if err != nil {
return err
}

if info.Size() <= hook.MaxSize {
return nil
}
hook.LogFile.Sync()
offset := int64(float32(hook.MaxSize) * hook.Cut)
buf := make([]byte, info.Size()-offset)
_, err = hook.LogFile.ReadAt(buf, offset)
if err != nil {
return err
}

nfile, err := os.Create(GetLogFilePath() + ".tmp")
if err != nil {
return err
}
nfile.Write(buf)
nfile.Close()

err = os.Rename(GetLogFilePath()+".tmp", GetLogFilePath())
if err != nil {
return err
}

mfile, err := os.OpenFile(GetLogFilePath(), os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println("open log file failed: ", err)
return err
}
entry.Logger.SetOutput(mfile)
initLog()
return nil
}

//NewLogRotateHook create a LogRotateHook
func NewLogRotateHook(file *os.File) *LogRotateHook {
return &LogRotateHook{
MaxSize: 1024*1024, //1MB
Cut: 0.2,
LogFile: file,
}
}

//ToQueryMap tranform request to map
func ToQueryMap(req request.Common) map[string]string {
reqMap, err := request.ToQueryMap(req)
if err != nil {
return nil
}
delete(reqMap, "Password")
return reqMap
}
39 changes: 0 additions & 39 deletions base/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"
"unicode"

sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
uerr "github.com/ucloud/ucloud-sdk-go/ucloud/error"
"github.com/ucloud/ucloud-sdk-go/ucloud/helpers/waiter"
"github.com/ucloud/ucloud-sdk-go/ucloud/log"
"github.com/ucloud/ucloud-sdk-go/ucloud/request"
"github.com/ucloud/ucloud-sdk-go/ucloud/response"

"github.com/ucloud/ucloud-cli/model"
Expand All @@ -41,43 +39,6 @@ var SdkClient *sdk.Client
//BizClient 用于调用业务接口
var BizClient *Client

//Logger 日志
var Logger = log.New()
var mu sync.Mutex

func init() {
file, err := os.Create(GetLogFilePath())
if err != nil {
return
}
Logger.SetOutput(file)
}

//GetLogFilePath 获取日志文件路径
func GetLogFilePath() string {
return GetHomePath() + fmt.Sprintf("/%s/cli.log", ConfigPath)
}

//Log 记录日志
func Log(logs []string) {
mu.Lock()
defer mu.Unlock()
Logger.Info("=============================================================")
for _, line := range logs {
Logger.Info(line)
}
}

//ToQueryMap tranform request to map
func ToQueryMap(req request.Common) map[string]string {
reqMap, err := request.ToQueryMap(req)
if err != nil {
return nil
}
// delete(reqMap, "Password")
return reqMap
}

//GetHomePath 获取家目录
func GetHomePath() string {
if runtime.GOOS == "windows" {
Expand Down
6 changes: 3 additions & 3 deletions cmd/uhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"strings"
"sync"
"time"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -291,6 +292,8 @@ func createUhostWrapper(req *uhost.CreateUHostInstanceRequest, eipReq *unet.Allo
tokens <- struct{}{}
defer func() {
<-tokens
//设置延时,使报错能渲染出来
time.Sleep(time.Second / 5)
wg.Done()
}()

Expand All @@ -309,14 +312,12 @@ func createUhost(req *uhost.CreateUHostInstanceRequest, eipReq *unet.AllocateEIP
if err != nil {
logs = append(logs, fmt.Sprintf("err:%v", err))
block.Append(base.ParseError(err))
block.AppendDone()
return false, logs
}

logs = append(logs, fmt.Sprintf("resp:%#v", resp))
if len(resp.UHostIds) != 1 {
block.Append(fmt.Sprintf("expect uhost count 1 , accept %d", len(resp.UHostIds)))
block.AppendDone()
return false, logs
}

Expand Down Expand Up @@ -367,7 +368,6 @@ func createUhost(req *uhost.CreateUHostInstanceRequest, eipReq *unet.AllocateEIP
}
}
}
block.AppendDone()
return true, logs
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/kr/pretty v0.1.0 // indirect
github.com/sirupsen/logrus v1.3.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/ucloud/ucloud-sdk-go v0.8.2
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package main

import "github.com/ucloud/ucloud-cli/cmd"
import (
"github.com/ucloud/ucloud-cli/cmd"
)

func main() {
cmd.Execute()
Expand Down
Loading

0 comments on commit ab51416

Please sign in to comment.