graylog 测试环境服务
graylog 是一个很不错的日志系统,但是我们实际开发中,可能需要进行测试,但是直接部署一个graylog 可能有点太费事
(依赖es,mongo),有一个golang mock 服务的实现包 github.com/lintianzhi/graylogd
以下是一个demo server的构建以及容器镜像,可以直接使用
代码
- go mod
module github.com/rongfengliang/graylogd
go 1.14
require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/lintianzhi/graylogd v0.0.0-20180503131252-dc68342f04dc
github.com/robertkowalski/graylog-golang v0.0.0-20151121031040-e5295cfa2827
github.com/stretchr/testify v1.6.1
)
- main.go
package main
import (
"fmt"
"net"
"github.com/lintianzhi/graylogd"
)
func main() {
conf := graylogd.Config{
ListenAddr: ":12201",
// handle raw message
HandleRaw: func(b []byte) {
fmt.Println(string(b))
fmt.Println(len(b))
},
HandleGELF: func(gelf *graylogd.GelfLog, addi map[string]interface{}) {
fmt.Println(gelf)
fmt.Println(addi)
},
HandleError: func(addr *net.UDPAddr, err error) {
fmt.Println("err:", err)
},
}
logd, _ := graylogd.NewGraylogd(conf)
err := logd.Run()
if err != nil {
fmt.Println("run failed:", err)
return
}
select {}
}
Dockerfile
FROM golang:1.13-alpine AS build-env
WORKDIR /go/src/app
RUN /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn
COPY go.mod .
COPY go.sum .
COPY main.go .
RUN apk update && apk add git \
&& go build
FROM alpine:latest
WORKDIR /app
RUN /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
COPY --from=build-env /go/src/app/graylogd /app/graylogd
COPY supervisord.conf /etc/supervisord.conf
EXPOSE 12201/udp 9001
CMD ["/app/graylogd"]
说明
已经push 的docker 镜像dalongrong/graylogd
可以直接使用,参考运行
docker run -d -p 12201:12201/udp dalongrong/graylogd