Simple logger that listens for UNIX datagram message and prints them to stdout. We don't like syslog-ng in our postfix container!
| package main | |
| import ( | |
| "bufio" | |
| "net" | |
| "os" | |
| "syscall" | |
| ) | |
| func println(msg string) { | |
| os.Stdout.WriteString(msg + "\n") | |
| } | |
| func main() { | |
| socketPath := "/dev/log" | |
| // unlink it before doing anything | |
| syscall.Unlink(socketPath) | |
| // resolve unix address | |
| laddr, err := net.ResolveUnixAddr("unixgram", socketPath) | |
| if err != nil { | |
| println("Could not resolve unix socket: " + err.Error()) | |
| os.Exit(1) | |
| } | |
| // listen on the socket | |
| conn, err := net.ListenUnixgram("unixgram", laddr) | |
| if err != nil { | |
| println("Could not listen on unix socket datagram: " + err.Error()) | |
| os.Exit(1) | |
| } | |
| // close socket when we finish | |
| defer conn.Close() | |
| // scan text | |
| scanner := bufio.NewScanner(conn) | |
| for { | |
| if ok := scanner.Scan(); !ok { | |
| continue | |
| } | |
| println(string(scanner.Bytes())) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment