-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 99d652d
Showing
10 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Karan Jagtiani | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# go-logstash | ||
Logstash Logger written in Go that pushes logs directly to [Logstash](https://www.elastic.co/products/logstash). It supports writing outputs to Logstash in JSON format as well as String format. | ||
|
||
### **Features** | ||
|
||
- Supports TCP and UDP protocols for sending logs to Logstash. | ||
- Supports writing logs in both JSON and string formats. | ||
- Provides customisable options for configuring the Logstash connection. | ||
|
||
### **Getting Started** | ||
|
||
#### 1. Install the package using: | ||
|
||
``` | ||
go get github.com/KaranJagtiani/go-logstash | ||
``` | ||
|
||
#### 2. Import the package in your Go application: | ||
|
||
```go | ||
import "github.com/KaranJagtiani/go-logstash" | ||
``` | ||
#### 3. Initialize the **`logstash_logger`** instance with your Logstash server details and connection type: | ||
|
||
```go | ||
logger := logstash_logger.Init("<host>", <port>, "protocol", <timeout>) | ||
``` | ||
|
||
The `logstash_logger.Init()` has the following configuration options: | ||
|
||
- Host: Logstash server hostname. | ||
- Port: Logstash server port number. | ||
- Protocol: Logstash connection protocol, either "tcp" or "udp". | ||
- Timeout: Connection timeout in seconds. | ||
|
||
Example: | ||
```go | ||
logger := logstash_logger.Init("logstash", 5228, "udp", 5) | ||
``` | ||
|
||
#### 4. Use one of the different methods provided for sending logs to Logstash: | ||
|
||
```go | ||
payload := map[string]interface{}{ | ||
"message": "test message", | ||
"error": false, | ||
} | ||
|
||
// Generic - For logging the payload as it is | ||
logger.Log(payload) | ||
|
||
// Adds a attribute called "severity": "INFO" to the payload | ||
logger.Info(payload) | ||
|
||
// Adds a attribute called "severity": "DEBUG" to the payload | ||
logger.Debug(payload) | ||
|
||
// Adds a attribute called "severity": "WARN" to the payload | ||
logger.Warn(payload) | ||
|
||
// Adds a attribute called "severity": "ERROR" to the payload | ||
logger.Error(payload) | ||
|
||
// For sending a string message to Logstash | ||
logger.LogString("String Message") | ||
``` | ||
|
||
### **Contribution** | ||
Contributions to go-logstash are welcome! If you find a bug or want to add a new feature, please create an issue or submit a pull request here on GitHub. | ||
|
||
#### Submit a Pull Request | ||
1. Fork this repository. | ||
2. Create your feature branch (git checkout -b my-new-feature). | ||
3. Commit your changes (git commit -am 'Add some feature'). | ||
4. Push to the branch (git push origin my-new-feature). | ||
5. Create a new Pull Request. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package logstash_logger | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"time" | ||
) | ||
|
||
func (l *logstash) initLoggerConnection() { | ||
logstashAddr := fmt.Sprintf("%s:%d", l.host, l.port) | ||
if l.connectionType == "udp" { | ||
netConnTemp, err := net.Dial("udp", logstashAddr) | ||
if err != nil { | ||
log.Printf("[Logstash Logger] Failed to connect to Logstash: %s\n", err) | ||
} | ||
l.connection = netConnTemp | ||
} else if l.connectionType == "tcp" { | ||
netConnTemp, err := net.Dial("tcp", logstashAddr) | ||
if err != nil { | ||
log.Printf("[Logstash Logger] Failed to connect to Logstash: %s\n", err) | ||
} | ||
l.connection = netConnTemp | ||
} else { | ||
log.Println("[Logstash Logger] Invalid 'ConnectionType' provided. Please provide one of the following: tcp, udp") | ||
return | ||
} | ||
|
||
l.setTimeout() | ||
} | ||
|
||
func (l *logstash) setTimeout() { | ||
deadline := time.Now().Add(time.Duration(l.timeout) * time.Second) | ||
l.connection.SetDeadline(deadline) | ||
l.connection.SetWriteDeadline(deadline) | ||
l.connection.SetReadDeadline(deadline) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package logstash_logger | ||
|
||
func (l *logstash) Log(payload map[string]interface{}) { | ||
l.pushJsonMessage(payload) | ||
} | ||
|
||
func (l *logstash) Info(payload map[string]interface{}) { | ||
payload["severity"] = "INFO" | ||
l.pushJsonMessage(payload) | ||
delete(payload, "severity") | ||
} | ||
|
||
func (l *logstash) Debug(payload map[string]interface{}) { | ||
payload["severity"] = "DEBUG" | ||
l.pushJsonMessage(payload) | ||
delete(payload, "severity") | ||
} | ||
|
||
func (l *logstash) Warn(payload map[string]interface{}) { | ||
payload["severity"] = "WARN" | ||
l.pushJsonMessage(payload) | ||
delete(payload, "severity") | ||
} | ||
|
||
func (l *logstash) Error(payload map[string]interface{}) { | ||
payload["severity"] = "ERROR" | ||
l.pushJsonMessage(payload) | ||
delete(payload, "severity") | ||
} | ||
|
||
func (l *logstash) LogString(stringMsg string) { | ||
l.pushStringMessage(stringMsg) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/KaranJagtiani/go-logstash | ||
|
||
go 1.18 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package logstash_logger | ||
|
||
func Init(hostname string, port int, connection_type string, timeout int) *logstash { | ||
l := logstash{} | ||
l.host = hostname | ||
l.port = port | ||
l.connectionType = connection_type | ||
l.timeout = timeout | ||
return &l | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package logstash_logger | ||
|
||
import "log" | ||
|
||
func init() { | ||
log.Printf("[Logstash Logger] initializing...") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package logstash_logger | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
) | ||
|
||
func (l *logstash) pushJsonMessage(jsonMsg map[string]interface{}) { | ||
l.initLoggerConnection() | ||
|
||
if l.connection == nil { | ||
log.Println("[Logstash Logger] Logger was not initialized properly, could not push message.") | ||
return | ||
} | ||
|
||
jsonData, err := json.Marshal(jsonMsg) | ||
|
||
if err != nil { | ||
log.Printf("[Logstash Logger] Failed to parse JSON object: %s\n", err) | ||
} | ||
|
||
_, connWriteError := l.connection.Write(jsonData) | ||
if connWriteError != nil { | ||
log.Printf("[Logstash Logger] Failed to send log JSON message: %s\n", connWriteError) | ||
} | ||
|
||
l.connection.Close() | ||
} | ||
|
||
func (l *logstash) pushStringMessage(stringMsg string) { | ||
l.initLoggerConnection() | ||
|
||
if l.connection == nil { | ||
log.Println("[Logstash Logger] Logger was not initialized properly, could not push message.") | ||
return | ||
} | ||
|
||
_, connWriteError := l.connection.Write([]byte(stringMsg)) | ||
if connWriteError != nil { | ||
log.Printf("[Logstash Logger] Failed to send log String message: %s\n", connWriteError) | ||
} | ||
|
||
l.connection.Close() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package logstash_logger | ||
|
||
import "net" | ||
|
||
type logstash struct { | ||
host string | ||
port int | ||
connectionType string | ||
timeout int | ||
connection net.Conn | ||
} |