Push json formatted log to local file system
1. go get github.com/vonlippmann/logstash
2. import "github.com/vonlippmann/logstash" in your code
3. make a new instance of Logger:
logger := NewLogStash(&Config{
LogPath: "<your specified path>",
LogKeepDays: 0,
FileName: "<your specified file name>",
CleanLog: false,
})
4. then you can use logger in anywhere you need to sink log to the specified path:
logger.Sink(Massage{
"auth": "pep-pig",
"age": "18",
})
- you can use hook to postprocess the massage you sinked, such as add some other fields. You can do this simply by register a hook function:
logger.RegisterHook(func(msg Massage)(err error){
mas["email"]="###@example.com"
return
})
package main
var logger *Logger
func main(){
for{
logger.Sink(Massage{
"auth": "pep-pig",
"age": "18",
})
}
}
func addField(massage Massage) (err error) {
massage["email"] = "###@example.com"
return
}
func init() {
logger = NewLogStash(&Config{
LogPath: "/var/log/logstash",
LogKeepDays: 0,
FileName: "elk",
CleanLog: false,
})
logger.RegisterHook(addField)
}