Last active
September 22, 2021 14:27
-
-
Save hazcod/35d705ebf87704b976ceab58ba20ca21 to your computer and use it in GitHub Desktop.
Simple logger that listens for UNIX datagram message and prints them to stdout. We don't like syslog-ng in our postfix container!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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