Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (66 sloc) 1.76 KB
package nxlog
import (
// Writer represents a writer to NXLog
type Writer struct {
protocol string
endpoint string
settings interface{}
connection net.Conn
// NewWriter returns a reference to a new writer, with protocol, endpoint and
// settings populated. It also tries to connect to it and returns nil on
// success or error otherwise.
func NewWriter(protocol string, endpoint string, settings interface{}) (*Writer, error) {
w := &Writer{
protocol: protocol,
endpoint: endpoint,
settings: settings,
return w, w.Connect()
// Connect with the protocol and endpoint specified in the writer.
func (w *Writer) Connect() error {
if w.protocol == "ssl" {
return w.getTLSWriter()
return w.getNetWriter()
// Reconnect will close the connection and open it again.
func (w *Writer) Reconnect() {
// Close will close the current connection if possible.
func (w *Writer) Close() {
// Write will send a message to the open connection of the writer.
// In case of error, it will try to reconnect once.
func (w *Writer) Write(message []byte, reconnect bool) (int, error) {
n, err := w.connection.Write(message)
if err != nil && reconnect {
return w.Write(message, false)
return n, err
func (w *Writer) getNetWriter() error {
connection, err := net.Dial(w.protocol, w.endpoint)
w.connection = connection
return err
func (w *Writer) getTLSWriter() error {
configuration := w.settings
if configuration == nil || reflect.TypeOf(configuration).String() != "*tls.Config" {
configuration = &tls.Config{
InsecureSkipVerify: true,
connection, err := tls.Dial("tcp", w.endpoint, configuration.(*tls.Config))
w.connection = connection
return err
You can’t perform that action at this time.