-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (108 loc) · 3.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package logger
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os"
"time"
"github.com/joho/godotenv"
amqp "github.com/rabbitmq/amqp091-go"
)
const INFO = "INFO"
const WARNING = "WARNING"
const ERROR = "ERROR"
const TRACE = "TRACE"
const DEBUG = "DEBUG"
var connection *amqp.Connection
var channel *amqp.Channel
var queue amqp.Queue
var project_endpoint_type string
var project_code string
func InitLogger() error {
logger := os.Getenv("APP_LOGGER")
if logger == "true" {
err := godotenv.Load()
if err != nil {
return fmt.Errorf("failed get env for asiasiapac logger, error: %s", err)
}
user := os.Getenv("RABBITMQ_LOGGER_USERNAME")
pass := os.Getenv("RABBITMQ_LOGGER_PASSWORD")
host := os.Getenv("RABBITMQ_LOGGER_HOST")
port := os.Getenv("RABBITMQ_LOGGER_PORT")
project_endpoint_type = os.Getenv("PROJECT_ENDPOINT_TYPE")
if project_endpoint_type == "" {
return fmt.Errorf("project_endpoint_type env is needed")
}
project_code = os.Getenv("PROJECT_CODE")
if project_code == "" {
return fmt.Errorf("project_code env is needed")
}
connection, err = amqp.DialConfig(fmt.Sprintf("amqp://%s:%s@%s:%s/%s", user, pass, host, port, user), amqp.Config{
Heartbeat: 10 * time.Second,
})
if err != nil {
return fmt.Errorf("failed to init connection asiasiapac logger, error: %s", err)
}
channel, err = connection.Channel()
if err != nil {
return fmt.Errorf("failed to init channel asiasiapac logger, error: %s", err)
}
queue, err = channel.QueueDeclare(
"logger-sys",
true,
false,
false,
false,
nil,
)
if err != nil {
return fmt.Errorf("failed to create queue asiasiapac logger, error: %s", err)
}
}
return nil
}
func Save(level, task, remark, note string) error {
logger := os.Getenv("APP_LOGGER")
if logger == "true" {
message := map[string]string{
"remark": remark,
"level": level,
"endpoint_rules": task,
"note": note,
"endpoint_type": project_endpoint_type,
"source": project_code,
"time": time.Now().Format("2006-01-02T15:04:05.000000000Z07:00"),
"uuid": uuidV4(),
"refer_class": "go function",
}
msg, err := json.Marshal(&message)
if err != nil {
return fmt.Errorf("failed to marshal message asiasiapac logger, error: %s", err)
}
err = channel.PublishWithContext(
context.Background(),
"",
queue.Name,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: msg,
},
)
if err != nil {
return fmt.Errorf("failed to publish message asiasiapac logger, error: %s", err)
}
}
return nil
}
func uuidV4() string {
return fmt.Sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
rand.Intn(0xffff), rand.Intn(0xffff), // 32 bits for "time_low"
rand.Intn(0xffff), // 16 bits for "time_mid"
rand.Intn(0x0fff)|0x4000, // 16 bits for "time_hi_and_version", with version number 4
rand.Intn(0x3fff)|0x8000, // 16 bits for "clk_seq_hi_res" and "clk_seq_low"
rand.Intn(0xffff), rand.Intn(0xffff), rand.Intn(0xffff), // 48 bits for "node"
)
}