Compare commits
No commits in common. "e3093c1b73532501f522f8ea4a2c1ea78a3a2e65" and "cfa92618a86f3b8e53351ec356152398cee92719" have entirely different histories.
e3093c1b73
...
cfa92618a8
|
|
@ -41,12 +41,6 @@ var StartCmd = &cobra.Command{
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
procs, err := process.ScanAllProcesses(cfg)
|
procs, err := process.ScanAllProcesses(cfg)
|
||||||
|
|
||||||
logger.Log.Info("进程扫描完成",
|
|
||||||
zap.Int("进程数量", len(procs)),
|
|
||||||
zap.Duration("扫描耗时", time.Since(startTime)),
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Error("扫描进程失败", zap.Error(err))
|
logger.Log.Error("扫描进程失败", zap.Error(err))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
@ -56,6 +50,22 @@ var StartCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Log.Info("进程列表:")
|
||||||
|
for i, p := range procs {
|
||||||
|
if i >= 10 {
|
||||||
|
logger.Log.Info("... (仅显示前10个进程)")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
logger.Log.Info(
|
||||||
|
"进程信息",
|
||||||
|
zap.Int32("pid", p.PID),
|
||||||
|
zap.String("name", p.Name),
|
||||||
|
zap.String("path", p.Path),
|
||||||
|
zap.String("cmdline", p.Cmdline),
|
||||||
|
zap.Stringer("data", p),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// ====== 文件扫描和存储 ======
|
// ====== 文件扫描和存储 ======
|
||||||
logger.Log.Info("正在扫描文件系统...")
|
logger.Log.Info("正在扫描文件系统...")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
package version
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sysmonitord/internal/version"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
)
|
|
||||||
|
|
||||||
var VersionCmd = &cobra.Command{
|
|
||||||
Use: "version",
|
|
||||||
Short: "显示 sysmonitord 的版本信息",
|
|
||||||
Long: "sysmonitord version 命令用于显示当前 sysmonitord 的版本、Git 提交信息和构建时间。",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
fmt.Println(version.Info())
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -2,10 +2,6 @@ server:
|
||||||
host: "127.0.0.1"
|
host: "127.0.0.1"
|
||||||
port: 8080
|
port: 8080
|
||||||
|
|
||||||
log:
|
|
||||||
level: "info"
|
|
||||||
# level: "debug"
|
|
||||||
|
|
||||||
audit:
|
audit:
|
||||||
enabled: true
|
enabled: true
|
||||||
server: "192.168.1.100"
|
server: "192.168.1.100"
|
||||||
|
|
|
||||||
6
go.mod
6
go.mod
|
|
@ -7,10 +7,7 @@ require (
|
||||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
|
||||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
|
||||||
github.com/schollz/progressbar/v3 v3.19.0 // indirect
|
|
||||||
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
|
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
|
||||||
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
||||||
github.com/spf13/cobra v1.10.2 // indirect
|
github.com/spf13/cobra v1.10.2 // indirect
|
||||||
|
|
@ -20,7 +17,6 @@ require (
|
||||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
go.uber.org/zap v1.27.1 // indirect
|
go.uber.org/zap v1.27.1 // indirect
|
||||||
golang.org/x/sys v0.29.0 // indirect
|
golang.org/x/sys v0.20.0 // indirect
|
||||||
golang.org/x/term v0.28.0 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
10
go.sum
10
go.sum
|
|
@ -8,15 +8,9 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
|
||||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
|
||||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
|
||||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
|
|
||||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
|
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
|
||||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
|
||||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/schollz/progressbar/v3 v3.19.0 h1:Ea18xuIRQXLAUidVDox3AbwfUhD0/1IvohyTutOIFoc=
|
|
||||||
github.com/schollz/progressbar/v3 v3.19.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
|
|
||||||
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
|
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
|
||||||
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
|
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
|
||||||
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
|
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
|
||||||
|
|
@ -43,10 +37,6 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
|
||||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
||||||
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
|
|
||||||
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,11 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Log LogConfig `yaml:"log"`
|
|
||||||
Audit AuditConfig `yaml:"audit"`
|
Audit AuditConfig `yaml:"audit"`
|
||||||
Scanner ScannerConfig `yaml:"scanner"`
|
Scanner ScannerConfig `yaml:"scanner"`
|
||||||
Storage StorageConfig `yaml:"storage"`
|
Storage StorageConfig `yaml:"storage"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LogConfig struct {
|
|
||||||
Level string `yaml:"level"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AuditConfig struct {
|
type AuditConfig struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"sysmonitord/internal/scanner/hash"
|
"sysmonitord/internal/scanner/hash"
|
||||||
"sysmonitord/pkg/logger"
|
"sysmonitord/pkg/logger"
|
||||||
|
|
||||||
"github.com/schollz/progressbar/v3"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -37,61 +36,21 @@ func (s *Scanner) Scan() ([]FileInfo, error) {
|
||||||
targetPaths = []string{"/"}
|
targetPaths = []string{"/"}
|
||||||
}
|
}
|
||||||
|
|
||||||
var allPaths []string
|
var allFiles []FileInfo
|
||||||
|
|
||||||
for _, root := range targetPaths {
|
for _, root := range targetPaths {
|
||||||
if _, err := os.Stat(root); os.IsNotExist(err) {
|
if _, err := os.Stat(root); os.IsNotExist(err) {
|
||||||
logger.Log.Debug("扫描路径不存在,已跳过", zap.String("path", root))
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log.Info("[scan]正在扫描文件系统", zap.String("root", root))
|
logger.Log.Info("[scan]正在扫描文件系统", zap.String("root", root))
|
||||||
|
|
||||||
err := filepath.WalkDir(root, s.collectPathsFunc(&allPaths))
|
err := filepath.WalkDir(root, s.WalkFunc(&allFiles))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Error("[scan]扫描文件系统时发生错误", zap.String("root", root), zap.Error(err))
|
logger.Log.Error("[scan]扫描文件系统时发生错误", zap.String("root", root), zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log.Info("[scan]开始计算文件哈希", zap.Int("文件数量", len(allPaths)))
|
|
||||||
|
|
||||||
var allFiles []FileInfo
|
|
||||||
hashCfg, _ := s.cfg.GetHashConfig()
|
|
||||||
|
|
||||||
bar := progressbar.NewOptions(len(allPaths),
|
|
||||||
progressbar.OptionSetDescription("[scan]计算文件哈希"),
|
|
||||||
progressbar.OptionSetWriter(os.Stderr),
|
|
||||||
progressbar.OptionShowCount(),
|
|
||||||
progressbar.OptionShowIts(),
|
|
||||||
progressbar.OptionSetItsString("files"),
|
|
||||||
progressbar.OptionOnCompletion(func() {
|
|
||||||
fmt.Fprintln(os.Stderr, "\n[scan]文件哈希计算完成")
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, path := range allPaths {
|
|
||||||
bar.Add(1)
|
|
||||||
|
|
||||||
info, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
logger.Log.Debug("[scan]无法获取文件信息", zap.String("path", path), zap.Error(err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if info.Size() > 0 {
|
|
||||||
hash, err := hash.Calculate(path, info.Size(), hashCfg)
|
|
||||||
if err != nil {
|
|
||||||
logger.Log.Debug("[scan]无法计算文件哈希", zap.String("path", path), zap.Error(err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
allFiles = append(allFiles, FileInfo{
|
|
||||||
Path: path,
|
|
||||||
Hash: hash,
|
|
||||||
ModTime: info.ModTime().Unix(),
|
|
||||||
Size: info.Size(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return allFiles, nil
|
return allFiles, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -144,29 +103,6 @@ func (s *Scanner) WalkFunc(result *[]FileInfo) fs.WalkDirFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) collectPathsFunc(result *[]string) fs.WalkDirFunc {
|
|
||||||
return func(path string, d fs.DirEntry, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
logger.Log.Debug("[scan]跳过路径", zap.String("path", path), zap.Error(err))
|
|
||||||
return fs.SkipDir
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exclude := range s.cfg.Scanner.File.ExcludePaths {
|
|
||||||
if strings.HasPrefix(path, exclude) {
|
|
||||||
logger.Log.Debug("[scan]跳过路径", zap.String("path", path), zap.String("reason", "匹配排除路径"))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*result = append(*result, path)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FileInfo) String() string {
|
func (f FileInfo) String() string {
|
||||||
return fmt.Sprintf("%s:%s", f.Path, f.Hash)
|
return fmt.Sprintf("%s:%s", f.Path, f.Hash)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
package version
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
Version = "dev"
|
|
||||||
GitCommit = "none"
|
|
||||||
BuildTime = "unknown"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Info() string {
|
|
||||||
return fmt.Sprintf("Sysmonitord %s\nGit Commit: %s\nBuild Time: %s\nGo Version: %s",
|
|
||||||
Version, GitCommit, BuildTime, runtime.Version())
|
|
||||||
}
|
|
||||||
11
main.go
11
main.go
|
|
@ -3,8 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"sysmonitord/cmd/start"
|
"sysmonitord/cmd/start"
|
||||||
"sysmonitord/cmd/version"
|
|
||||||
"sysmonitord/internal/config"
|
|
||||||
"sysmonitord/pkg/logger"
|
"sysmonitord/pkg/logger"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -15,21 +13,12 @@ func main() {
|
||||||
logger.InitLogger()
|
logger.InitLogger()
|
||||||
defer logger.Sync()
|
defer logger.Sync()
|
||||||
|
|
||||||
cfg, err := config.LoadConfig("./config.yaml")
|
|
||||||
if err != nil {
|
|
||||||
logger.Log.Error("加载配置文件失败", zap.Error(err))
|
|
||||||
os.Exit(1)
|
|
||||||
} else {
|
|
||||||
logger.SetLogLevel(cfg.Log.Level)
|
|
||||||
}
|
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "sysmonitord",
|
Use: "sysmonitord",
|
||||||
Short: "Sysmonitord 是一个 Linux 系统安全监控工具",
|
Short: "Sysmonitord 是一个 Linux 系统安全监控工具",
|
||||||
}
|
}
|
||||||
|
|
||||||
rootCmd.AddCommand(start.StartCmd)
|
rootCmd.AddCommand(start.StartCmd)
|
||||||
rootCmd.AddCommand(version.VersionCmd)
|
|
||||||
|
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
logger.Log.Error("命令执行失败", zap.Error(err))
|
logger.Log.Error("命令执行失败", zap.Error(err))
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,9 @@ import (
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var Log *zap.Logger
|
||||||
Log *zap.Logger
|
|
||||||
atom zap.AtomicLevel
|
|
||||||
)
|
|
||||||
|
|
||||||
func InitLogger() {
|
func InitLogger() {
|
||||||
atom = zap.NewAtomicLevelAt(zapcore.InfoLevel)
|
|
||||||
|
|
||||||
encoderConfig := zapcore.EncoderConfig{
|
encoderConfig := zapcore.EncoderConfig{
|
||||||
TimeKey: "time",
|
TimeKey: "time",
|
||||||
LevelKey: "level",
|
LevelKey: "level",
|
||||||
|
|
@ -23,35 +18,22 @@ func InitLogger() {
|
||||||
MessageKey: "msg",
|
MessageKey: "msg",
|
||||||
StacktraceKey: "stacktrace",
|
StacktraceKey: "stacktrace",
|
||||||
LineEnding: zapcore.DefaultLineEnding,
|
LineEnding: zapcore.DefaultLineEnding,
|
||||||
EncodeLevel: zapcore.LowercaseColorLevelEncoder,
|
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
||||||
EncodeTime: zapcore.ISO8601TimeEncoder,
|
EncodeTime: zapcore.ISO8601TimeEncoder,
|
||||||
EncodeDuration: zapcore.StringDurationEncoder,
|
EncodeDuration: zapcore.StringDurationEncoder,
|
||||||
EncodeCaller: zapcore.ShortCallerEncoder,
|
EncodeCaller: zapcore.ShortCallerEncoder,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debug
|
||||||
consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
|
consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
|
||||||
|
|
||||||
core := zapcore.NewTee(
|
core := zapcore.NewTee(
|
||||||
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), atom),
|
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zap.DebugLevel),
|
||||||
)
|
)
|
||||||
|
|
||||||
Log = zap.New(core, zap.AddCaller())
|
Log = zap.New(core, zap.AddCaller())
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetLogLevel(levelStr string) {
|
|
||||||
if atom == (zap.AtomicLevel{}) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var level zapcore.Level
|
|
||||||
if err := level.UnmarshalText([]byte(levelStr)); err != nil {
|
|
||||||
Log.Warn("无效的日志级别,使用默认级别 info", zap.String("输入级别", levelStr))
|
|
||||||
atom.SetLevel(zapcore.InfoLevel)
|
|
||||||
} else {
|
|
||||||
atom.SetLevel(level)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func Sync() {
|
func Sync() {
|
||||||
if Log != nil {
|
if Log != nil {
|
||||||
_ = Log.Sync()
|
_ = Log.Sync()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user