Browse Source

修复功能

runningwater 5 years ago
parent
commit
01401db97e
7 changed files with 108 additions and 40 deletions
  1. 30 29
      conf/conf.go
  2. 7 7
      conf/conf_test.go
  3. 32 0
      excel/excel.go
  4. 1 0
      go.mod
  5. 2 0
      go.sum
  6. 26 0
      log_file.go
  7. 10 4
      main.go

+ 30 - 29
conf/conf.go

@@ -1,47 +1,48 @@
 package conf
 
 import (
-	"fmt"
-	"github.com/spf13/viper"
-	"os"
+    l4g "github.com/alecthomas/log4go"
+    "github.com/spf13/viper"
+    "os"
 )
 
 var cfg = Config{}
 
 type Config struct {
-	Database Database `mapstructure:"database"` // 数据库配置
+    Database Database `mapstructure:"database"` // 数据库配置
 }
 
 type Database struct {
-	Host    string `mapstructure:"host"`
-	User    string `mapstructure:"user"`
-	Dbname  string `mapstructure:"dbname"`
-	Pwd     string `mapstructure:"pwd"`
-	Port    int    `mapstructure:"port"`
-	Charset string `mapstructure:"charset"`
+    Host    string `mapstructure:"host"`
+    User    string `mapstructure:"user"`
+    Dbname  string `mapstructure:"dbname"`
+    Pwd     string `mapstructure:"pwd"`
+    Port    int    `mapstructure:"port"`
+    Charset string `mapstructure:"charset"`
 }
 
-func LoadConfig() {
-	path, err := os.Getwd()
-	if err != nil {
-		panic(err)
-	}
-	viper.AddConfigPath(path + "/conf")
-	viper.SetConfigName("config")
-	viper.SetConfigType("yaml")
-
-	if err := viper.ReadInConfig(); err != nil {
-		fmt.Println("读取配置文件失败, 异常信息: ", err)
-		panic(err)
-	}
-
-	// 将文件内容解析后封装到cfg对象中
-	if err := viper.Unmarshal(&cfg); err != nil {
-		fmt.Println("解析配置文件失败, 异常信息 : ", err)
-	}
+func LoadConfig(logger *l4g.Logger) {
+    path, err := os.Getwd()
+    if err != nil {
+        panic(err)
+    }
+    viper.AddConfigPath(path + "/conf")
+    logger.Debug("配置文件路径为 %s", path+"/conf")
+    viper.SetConfigName("config")
+    viper.SetConfigType("yaml")
+
+    if err := viper.ReadInConfig(); err != nil {
+        logger.Error("读取配置文件失败, 异常信息: ", err)
+        panic(err)
+    }
+
+    // 将文件内容解析后封装到cfg对象中
+    if err := viper.Unmarshal(&cfg); err != nil {
+        logger.Error("解析配置文件失败, 异常信息 : ", err)
+    }
 }
 
 // GetInfo 获取配置文件
 func GetInfo() Config {
-	return cfg
+    return cfg
 }

+ 7 - 7
conf/conf_test.go

@@ -1,14 +1,14 @@
 package conf
 
 import (
-	"fmt"
-	"testing"
+    "fmt"
+    "testing"
 )
 
 func TestGetInfo(t *testing.T) {
-	LoadConfig()
-	fmt.Println("name: ", GetInfo().Database.Dbname)
-	fmt.Println("host: ", GetInfo().Database.Host)
-	fmt.Println("user: ", GetInfo().Database.User)
-	fmt.Println("pwd: ", GetInfo().Database.Pwd)
+    LoadConfig()
+    fmt.Println("name: ", GetInfo().Database.Dbname)
+    fmt.Println("host: ", GetInfo().Database.Host)
+    fmt.Println("user: ", GetInfo().Database.User)
+    fmt.Println("pwd: ", GetInfo().Database.Pwd)
 }

+ 32 - 0
excel/excel.go

@@ -14,6 +14,7 @@ func Writing(userInfos []db.UserInfo) {
 		fmt.Print(err.Error())
 	}
 
+
 	row := sheet.AddRow()
 	row.SetHeightCM(0.5)
 	cell := row.AddCell()
@@ -22,14 +23,25 @@ func Writing(userInfos []db.UserInfo) {
 	cell = row.AddCell()
 	cell.Value = "用户名"
 
+	style := xlsx.NewStyle()
+	style.Border = xlsx.Border{
+		Left:        "thin",
+		Right:       "thin",
+		Top:         "thin",
+		Bottom:      "thin",
+	}
+	style.ApplyBorder = true;
 	for _, info := range userInfos {
 		row := sheet.AddRow()
 		row.SetHeightCM(0.5)
 		cell := row.AddCell()
 		cell.Value = info.UserId
+		cell.SetStyle(style)
 
 		cell = row.AddCell()
 		cell.Value = info.UserName
+		cell.SetStyle(style)
+
 	}
 
 	// 保存
@@ -37,4 +49,24 @@ func Writing(userInfos []db.UserInfo) {
 	if err != nil {
 		fmt.Printf(err.Error())
 	}
+
+}
+
+func customStyle() *xlsx.Style {
+	style := xlsx.NewStyle()
+	font := *xlsx.NewFont(12, "Verdana")
+	font.Bold = true
+	font.Italic = true
+	font.Underline = true
+	style.Font = font
+	//fill := *xlsx.NewFill("solid", "00FF0000", "FF000000")
+	//style.Fill = fill
+	border := *xlsx.NewBorder("thin", "thin", "thin", "thin")
+	style.Border = border
+	style.ApplyBorder = true
+	style.ApplyFill = true
+
+	style.ApplyFont = true
+
+	return style
 }

+ 1 - 0
go.mod

@@ -3,6 +3,7 @@ module monitor
 go 1.14
 
 require (
+	github.com/alecthomas/log4go v0.0.0-20180109082532-d146e6b86faa
 	github.com/go-sql-driver/mysql v1.5.0
 	github.com/jmoiron/sqlx v1.2.0
 	github.com/spf13/viper v1.7.1

+ 2 - 0
go.sum

@@ -15,6 +15,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
 github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
+github.com/alecthomas/log4go v0.0.0-20180109082532-d146e6b86faa h1:0zdYOLyuQ3TWIgWNgEH+LnmZNMmkO1ze3wriQt093Mk=
+github.com/alecthomas/log4go v0.0.0-20180109082532-d146e6b86faa/go.mod h1:iCVmQ9g4TfaRX5m5jq5sXY7RXYWPv9/PynM/GocbG3w=
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
 github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=

+ 26 - 0
log_file.go

@@ -0,0 +1,26 @@
+package main
+
+import (
+    l4g "github.com/alecthomas/log4go"
+    "path"
+    "runtime"
+    "strings"
+)
+
+
+func initLogger() * l4g.Logger {
+    log := l4g.Logger{}
+    filenameOnly := GetCurFilename()
+    logFileName := filenameOnly + ".log"
+    log.AddFilter("stdout",l4g.DEBUG,l4g.NewConsoleLogWriter())
+    log.AddFilter("file",l4g.DEBUG,l4g.NewFileLogWriter(logFileName,false))
+    return &log
+}
+
+func GetCurFilename() string {
+    _, fileName, _, _ := runtime.Caller(0)
+    fileNameWithSuffix := path.Base(fileName)
+    fileSuffix := path.Ext(fileNameWithSuffix)
+
+    return strings.TrimSuffix(fileNameWithSuffix, fileSuffix)
+}

+ 10 - 4
main.go

@@ -7,14 +7,20 @@ import (
 	"monitor/excel"
 )
 
+
 func main() {
+	// 日志初始化
+	logger := initLogger()
+	defer logger.Close()
+
 	cmd := parseCmd()
 	if cmd.versionFlag {
 		fmt.Println("version 0.0.1")
 	} else {
-		fmt.Println("run....")
+		logger.Info("...........................run.........................")
 		// 加载初始化配置文件
-		conf.LoadConfig()
+		logger.Info("加载初始化配置文件")
+		conf.LoadConfig(logger)
 
 		// 生成报表文件
 		c := conf.GetInfo().Database
@@ -26,14 +32,14 @@ func main() {
 			Port:     c.Port,
 			Charset:  c.Charset,
 		}
+
 		Db := db.ConnectMysql(&config)
 		defer Db.Close()
 		info := db.UserInfo{}
 		data, err := info.SelectData(Db)
 		if err != nil {
-			fmt.Printf("error:[%v]", err)
+			logger.Error("error:[%v]", err)
 		}
-
 		excel.Writing(data)
 		// 发送邮件