?Go???,??????????????????:
-
??????:??,???????????,?????????????????????JSON?????????
-
???????:??Go???
log
????????(?zap
?logrus
?)???????? -
??????:????????????,????????Elasticsearch????????????
os.OpenFile
?????????,???????????????????? -
??????:??????????,?ELK(Elasticsearch?Logstash?Kibana)???Graylog?Fluentd????????????????????????????
???????Go????????????:
package main import ( "log" "os" ) func main() { // ?????? logFile, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { log.Fatalf("????????: %v", err) } defer logFile.Close() // ??????? logger := log.New(logFile, "APP: ", log.LstdFlags) // ???? logger.Println("????????") }
???????????????,????net/http
???HTTP??,????????(?go-logging
)????????????
???????go-logging
???????????????:
??,??go-logging
?:
go get github.com/op/go-logging
??,????:
package main import ( "github.com/op/go-logging" ) var logger = logging.MustGetLogger("myapp") func main() { // ?????? format := logging.MustStringFormatter(`{"time":" s","level":"s","msg":"%s"}`) // ???????????? backend := logging.NewRemoteBackend("localhost:8080", "/path/to/logstash/input") backend.Formatter = format logger.SetBackend(backend) // ???? logger.Info("????????") }
??????,????????????????Logstash?????????????????????????