Code Monkey home page Code Monkey logo

logrusmqtt's Introduction

MQTT Publish Hook for Logrus

http://i.imgur.com/hTeVwmJ.png

This is an Logrus Hook which can send log message to MQTT server with operator friendly topic manner.

Usage

package main

import (
     "github.com/Sirupsen/logrus"

     "github.com/shirou/logrusmqtt"
)

func main() {
     log := logrus.New()

     p := logrusmqtt.MQTTHookParams{
             Hostname: "test.mosquitto.org",
             Topic:    "logrusmqtt/test/error",
     }

     hook, err := logrusmqtt.NewMQTTHook(p, logrus.DebugLevel)
     if err != nil {
             panic(err)
     }
     log.Hooks.Add(hook)

     log.Info("Info message")

     log.WithFields(logrus.Fields{
             "name": "joe",
             "age":  42,
     }).Error("Error Message with fields")
}

And the output from MQTT server is,

{"time":"2014-12-25T02:29:54.140874274Z","level":"info","msg":"Info message","data":{}}
{"time":"2014-12-25T02:29:54.141082554Z","level":"error","msg":"Error Message with fields","data":{"age":42,"name":"joe"}}

Topic

messages are published to topic + / + level topic.

If you set a topic as logrusmqtt/log and pass to NewMQTTHook,

  • info level log be sent to logrusmqtt/log/info
  • error level log will be sent to logrusmqtt/log/error

You can subscribe any specific log level. Or if you want to get any of log message, just subscrib logrusmqtt/log/#.

Parameters

To create MQTTHook, use MQTTHookParams{} with any MQTT paramerers.

type MQTTHookParams struct {
     Hostname   string
     Port       int
     Username   string
     Password   string
     QoS        int
     Topic      string
     Retain     bool
     ClientId   string
     CAFilepath string
     Insecure   bool
}

Only Topic and Hostname are required. Others are optional.

logrusmqtt's People

Contributors

shirou avatar telemac avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

logrusmqtt's Issues

Module Sirupsen has been renamed to sirupsen

Seeing weird case-sensitive problems? It's in the past been possible to import Logrus as both upper- and lower-case. Due to the Go package environment, this caused issues in the community and we needed a standard. Some environments experienced problems with the upper-case variant, so the lower-case was decided. Everything using logrus will need to use the lower-case: github.com/sirupsen/logrus. Any package that isn't, should be changed.

sirupsen/logrus

NewMQTTHook and MQTT connection errors

Was looking into a stack overflow question and noticed this code

if token := hook.client.Connect(); token.Error() != nil {
		return nil, err
	}

Please note that Connect is asynchronous (the connection takes place in a go routine). If you want to check for errors you need to call token.Wait() to wait for the operation to complete before calling token.Error() (there are very few situations in which an error would be visible immediately).

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.