[agent-go] 完成日志框架部分
This commit is contained in:
83
agent-go/config/nacosConfig.go
Normal file
83
agent-go/config/nacosConfig.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"agent-go/g"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type OctopusAgentNacosConfig struct {
|
||||
Spring Spring `json:"spring"`
|
||||
Server Server `json:"server"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Port int64 `json:"port"`
|
||||
}
|
||||
|
||||
type Spring struct {
|
||||
Application Application `json:"application"`
|
||||
Profiles Profiles `json:"profiles"`
|
||||
Cloud Cloud `json:"cloud"`
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Cloud struct {
|
||||
Nacos Nacos `json:"nacos"`
|
||||
}
|
||||
|
||||
type Nacos struct {
|
||||
Config Config `json:"config"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Group string `json:"group"`
|
||||
ConfigRetryTime int64 `json:"config-retry-time"`
|
||||
FileExtension string `json:"file-extension"`
|
||||
MaxRetry int64 `json:"max-retry"`
|
||||
ServerAddr string `json:"server-addr"`
|
||||
Timeout int64 `json:"timeout"`
|
||||
ConfigLongPollTimeout int64 `json:"config-long-poll-timeout"`
|
||||
ExtensionConfigs []ExtensionConfig `json:"extension-configs"`
|
||||
}
|
||||
|
||||
type ExtensionConfig struct {
|
||||
Group string `json:"group"`
|
||||
DataID string `json:"data-id"`
|
||||
}
|
||||
|
||||
type Profiles struct {
|
||||
Active string `json:"active"`
|
||||
}
|
||||
|
||||
var log = g.G.LOG
|
||||
|
||||
func InitNacos(configFileName string) {
|
||||
|
||||
data, err := ioutil.ReadFile(configFileName)
|
||||
if err != nil {
|
||||
log.Fatal("error reading YAML file: ", zap.Error(err))
|
||||
}
|
||||
|
||||
var config OctopusAgentNacosConfig
|
||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||
log.Fatal("error parsing YAML data: ", zap.Error(err))
|
||||
}
|
||||
|
||||
// 将结构体转换为 JSON 字符串
|
||||
jsonData, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 输出 JSON 字符串
|
||||
log.Info(string(jsonData))
|
||||
|
||||
}
|
||||
16
agent-go/g/global.go
Normal file
16
agent-go/g/global.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package g
|
||||
|
||||
type Global struct {
|
||||
LOG *Logger
|
||||
}
|
||||
|
||||
var logger, _ = NewLogger()
|
||||
|
||||
var G = NewGlobal(
|
||||
logger,
|
||||
)
|
||||
|
||||
// NewGlobal NewGlobal构造函数返回一个新的Global实例,其中包含指定的Logger。
|
||||
func NewGlobal(logger *Logger) *Global {
|
||||
return &Global{LOG: logger}
|
||||
}
|
||||
60
agent-go/g/logger.go
Normal file
60
agent-go/g/logger.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package g
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// Logger struct represents a zap-based logger.
|
||||
type Logger struct {
|
||||
*zap.Logger
|
||||
}
|
||||
|
||||
// NewLogger creates a new Logger instance.
|
||||
func NewLogger() (*Logger, error) {
|
||||
config := zap.Config{
|
||||
Encoding: "json",
|
||||
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
|
||||
OutputPaths: []string{"stdout"}, // 输出到控制台
|
||||
ErrorOutputPaths: []string{"stderr"},
|
||||
EncoderConfig: zapcore.EncoderConfig{
|
||||
MessageKey: "message",
|
||||
LevelKey: "level",
|
||||
TimeKey: "time",
|
||||
CallerKey: "caller",
|
||||
EncodeLevel: zapcore.CapitalLevelEncoder,
|
||||
EncodeTime: zapcore.ISO8601TimeEncoder,
|
||||
EncodeCaller: zapcore.ShortCallerEncoder,
|
||||
},
|
||||
}
|
||||
logger, err := config.Build()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Logger{logger}, nil
|
||||
}
|
||||
|
||||
// Debug logs a debug message.
|
||||
func (l *Logger) Debug(msg string, fields ...zap.Field) {
|
||||
l.Logger.Debug(msg, fields...)
|
||||
}
|
||||
|
||||
// Info logs an info message.
|
||||
func (l *Logger) Info(msg string, fields ...zap.Field) {
|
||||
l.Logger.Info(msg, fields...)
|
||||
}
|
||||
|
||||
// Warn logs a warning message.
|
||||
func (l *Logger) Warn(msg string, fields ...zap.Field) {
|
||||
l.Logger.Warn(msg, fields...)
|
||||
}
|
||||
|
||||
// Error logs an error message.
|
||||
func (l *Logger) Error(msg string, fields ...zap.Field) {
|
||||
l.Logger.Error(msg, fields...)
|
||||
}
|
||||
|
||||
// Fatal logs a fatal message and exits the program with a non-zero status code.
|
||||
func (l *Logger) Fatal(msg string, fields ...zap.Field) {
|
||||
l.Logger.Fatal(msg, fields...)
|
||||
}
|
||||
@@ -1,3 +1,13 @@
|
||||
module agent-go
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
go.uber.org/zap v1.24.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
)
|
||||
|
||||
21
agent-go/go.sum
Normal file
21
agent-go/go.sum
Normal file
@@ -0,0 +1,21 @@
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
|
||||
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -1,5 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"agent-go/config"
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// 解析命令行参数
|
||||
var version string
|
||||
flag.StringVar(&version, "version", "", "config file version")
|
||||
flag.Parse()
|
||||
// 读取对应版本的配置文件
|
||||
filename := fmt.Sprintf("octopus-agent-%s.yaml", version)
|
||||
// 初始化Nacos的连接配置
|
||||
config.InitNacos(filename)
|
||||
|
||||
}
|
||||
|
||||
21
agent-go/octopus-agent-dev.yaml
Normal file
21
agent-go/octopus-agent-dev.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
spring:
|
||||
application:
|
||||
name: octopus-agent
|
||||
profiles:
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
group: dev
|
||||
config-retry-time: 3000
|
||||
file-extension: yaml
|
||||
max-retry: 3
|
||||
server-addr: "150.230.198.103:21060"
|
||||
timeout: 5000
|
||||
config-long-poll-timeout: 5000
|
||||
extension-configs:
|
||||
- group: dev
|
||||
data-id: "common-dev.yaml"
|
||||
|
||||
server:
|
||||
port: 8000
|
||||
Reference in New Issue
Block a user