-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelloworld.go
More file actions
79 lines (61 loc) · 1.44 KB
/
Copy pathhelloworld.go
File metadata and controls
79 lines (61 loc) · 1.44 KB
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
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"time"
)
type Log struct {
}
func handler(w http.ResponseWriter, r *http.Request) {
l := &Log{}
l.Info("Hello world received a request.")
target := os.Getenv("TARGET")
if target == "" {
target = "World"
}
rand.Seed(time.Now().Unix())
fmt.Fprintf(w, "Hello %s!\n", target)
if rand.Intn(10) < 4 {
l.Error("http handler error")
} else {
l.Info("http handler success")
}
}
func main() {
l := &Log{}
l.Info("Hello world sample started.")
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil {
log.Fatalf("ListenAndServe error:%s ", err.Error())
}
}
func (log *Log) Infof(format string, a ...interface{}) {
log.log("INFO", format, a...)
}
func (log *Log) Info(msg string) {
log.log("INFO", "%s", msg)
}
func (log *Log) Errorf(format string, a ...interface{}) {
log.log("ERROR", format, a...)
}
func (log *Log) Error(msg string) {
log.log("ERROR", "%s", msg)
}
func (log *Log) Fatalf(format string, a ...interface{}) {
log.log("FATAL", format, a...)
}
func (log *Log) Fatal(msg string) {
log.log("FATAL", "%s", msg)
}
func (log *Log) log(level, format string, a ...interface{}) {
var cstSh, _ = time.LoadLocation("Asia/Shanghai")
ft := fmt.Sprintf("%s %s %s\n", time.Now().In(cstSh).Format("2006-01-02 15:04:05"), level, format)
fmt.Printf(ft, a...)
}