Git Product home page Git Product logo

Comments (2)

tagomoris avatar tagomoris commented on July 28, 2024

What really Post method is doing here????
You can read the code anytime. https://github.com/fluent/fluent-logger-golang/blob/master/fluent/fluent.go

We are always welcome to bug reports, but please report bugs with the exact code (not pseudo code) and the steps how to reproduce it.

from fluent-logger-golang.

Manikandan-Raj avatar Manikandan-Raj commented on July 28, 2024

package main

import (
"fmt"
"encoding/json"
syslog_fmt "gopkg.in/mcuadros/go-syslog.v2/format"
"github.com/fluent/fluent-logger-golang/fluent"
"io/ioutil"
"io"
"os"
syslog "gopkg.in/mcuadros/go-syslog.v2"
"flag"
)

var (
fileObject *os.File
bufferfileSize = flag.Int64("bytesize", 0, "Track the file size")
)

func forwardToAggregator(logParts syslog_fmt.LogParts){
tag := "datacollector"
data := make(map[string]interface{})
data["content"] = logParts["content"].(string)
data["hostname"] = logParts["hostname"].(string)
data["severity"] = logParts["severity"].(int)
data["facility"] = logParts["facility"].(int)
data["priority"] = logParts["priority"].(int)

fileObject, _ = os.OpenFile("test.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
logger, err := fluent.New(fluent.Config{FluentPort: X.X.X.X, FluentHost: XXXX})
if err != nil {
# Machine is not reachable/ Network failure, so saving logs to file
file, _ := json.Marshal(data)
route_messages_tofile(string(file))
} else {
sending_to_aggregator(logger, data, tag)
fi,_ := fileObject.Stat()
*bufferfileSize = fi.Size()
if *bufferfileSize != 0 {
send_logs_fromfile(logger, tag)
}
}

defer logger.Close()
}

func route_messages_tofile(content string){
n, err := io.WriteString(fileObject, string(content)+",")
if err != nil {
fmt.Println(n, err)
}
}

func sending_to_aggregator(logger *fluent.Fluent, data map[string]interface{}, tag string){
fmt.Println("Posting logs to aggregator")
error := logger.Post(tag, data)
fmt.Println("Error", error)
if error != nil {
file, _ := json.Marshal(data)
route_messages_tofile(string(file))
}
}

func send_logs_fromfile(logger fluent.Fluent, tag string){
fmt.Println("Aggregator came back to normal state")
fmt.Println("Going to sent the logs from File content")
fmt.Println("
***********************************")
content, _ := ioutil.ReadFile("/home/mani/logmonitor/test.json")
err := os.Truncate("/home/mani/logmonitor/test.json", 0)
if err != nil {
panic(err)
}

file_content := string(content)
processed_content := file_content[:len(file_content)-1]
processed_content = "["+string(processed_content)+"]"

var result []map[string]interface{}
fmt.Println("Content of the file", processed_content)
fmt.Println("*************************************")
json.Unmarshal([]byte(processed_content), &result)

for value := range result{
sending_to_aggregator(logger, result[value], tag)
fmt.Println("Value", result[value])
}
}

func main() {
channel := make(syslog.LogPartsChannel)
handler := syslog.NewChannelHandler(channel)
server := syslog.NewServer()
server.SetFormat(syslog.Automatic)
server.SetHandler(handler)

server.ListenTCP("0.0.0.0:514")
server.Boot()

go func(channel syslog.LogPartsChannel) {
for logParts := range channel {
forwardToAggregator(logParts)
}

}(channel)
server.Wait()
}

Scenario1 : if syslog message is received , it will got forwardToAggregator(logParts) -->formating messages and assume that Aggregator Machine is up, so it will sent the message to sending_to_aggregator(), here logger.Post() directly sending that syslog msg, and I checked in aggregator side.

Scenario2: Aggregator is up and scenario1 steps will get executed, but one point
aggregator becomes down, so the follow will be like
forwardToAggregator(logParts) --> route_messages_tofile(data),
After 5 minutes, again the machine is up, now message is coming and scenario1 steps will get executed,
after that it will check the buffer file and it is not zero, so now data is there and
send_logs_fromfile(logger, tag) method starts execution, it leads to hit the
sending_to_aggregator() function, here logger.Post(tag, data) should return ERROR,
but it is not.

Any idea???

from fluent-logger-golang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.