Skip to content

Commit

Permalink
added option to specify listen IP in socket mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter committed Jan 31, 2024
1 parent ff7b3f6 commit 9ea1126
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,28 @@ where you can filter and browse well formatted application output.

var listenStdCmd = &cobra.Command{
Use: "stdin [command]",
Short: "Listens to STDOUT/STDERR of a provided command. Example ./logdy listen_stdin \"npm run dev\"",
Short: "Listens to STDOUT/STDERR of a provided command. Example ./logdy stdin \"npm run dev\"",
Long: ``,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
logger.WithFields(logrus.Fields{
"cmd": args[0],
}).Info("Command")

arg := strings.Split(args[0], " ")
startCmd(ch, arg[0], arg[1:])
},
}

var listenSocketCmd = &cobra.Command{
Use: "socket [port]",
Short: "Sets up a port to listen on for incoming log messages. Example ./logdy listen_socket 8233",
Short: "Sets up a port to listen on for incoming log messages. Example ./logdy socket 8233",
Long: ``,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
go startSocketServer(ch, args[0])
ip, _ := cmd.Flags().GetString("ip")

go startSocketServer(ch, ip, args[0])
},
}

Expand All @@ -106,6 +109,7 @@ func init() {
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Verbose logs")
rootCmd.PersistentFlags().BoolP("no-analytics", "n", false, "Opt-out from sending anonymous analytical data that help improve this product")
demoSocketCmd.PersistentFlags().BoolP("sample-text", "", true, "By default demo data will produce JSON, use this flag to produce raw text")
listenSocketCmd.PersistentFlags().StringP("ip", "", "", "IP address to listen to, leave empty to listen on all IP addresses")

initLogger()

Expand Down
1 change: 1 addition & 0 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func produce(ch chan Message, line string, mt LogType) {
}

func trunc(str string, limit int) string {

if len(str) <= limit {
return str
}
Expand Down
7 changes: 4 additions & 3 deletions socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ func handleConnection(conn net.Conn, ch chan Message) {
}
}

func startSocketServer(ch chan Message, port string) {
func startSocketServer(ch chan Message, ip string, port string) {

addr := ip + ":" + port
// Start the TCP server
server, err := net.Listen("tcp", ":"+port)
server, err := net.Listen("tcp", addr)
if err != nil {
logger.Error("Error starting server:", err)
os.Exit(1)
}
defer server.Close()

logger.WithFields(logrus.Fields{
"port": port,
"address": addr,
}).Info("TCP Server is listening")

// Accept incoming connections and handle them in a separate goroutine
Expand Down

0 comments on commit 9ea1126

Please sign in to comment.