Giter VIP home page Giter VIP logo

vdlog's Introduction

vdlog

Build Status GoDoc Go Report Card

Structured logger for Go.

Info

Package assumes that there are this event levels:

  • Error is a level for errors that can wake you on 4 hours past midnight

  • Warn is a level for an unexpected technical or business event happened, customers may be affected, but probably no immediate human intervention is required. On call people won't be called immediately, but support personnel will want to review these issues asap to understand what the impact is. Basically any issue that needs to be tracked but may not require immediate intervention. For example, CPU load is higher than usual, but bot critical.

  • Info are for things we want to see at high volume in case we need to forensically analyze an issue. System lifecycle events (system start, stop) go here. "Session" lifecycle events (login, logout, etc.) go here. Significant boundary events should be considered as well (e.g. database calls, remote API calls). Typical business exceptions can go here (e.g. login failed due to bad credentials). Any other event you think you'll need to see in production at high volume goes here.

  • Verbose just about everything that doesn't make the "info" cut... any message that is helpful in tracking the flow through the system and isolating issues, especially during the development and QA phases. We use "debug" level logs for entry/exit of most non-trivial methods and marking interesting events and decision points inside methods.

  • Debug is for extremely detailed and potentially high volume logs that you don't typically want enabled even during normal development. Examples include dumping a full object hierarchy, logging some state during every iteration of a large loop, etc.

  • Silly is putting every fart to log.

After events emitted, they are all send to buffered channel called spine. Than, each event is processed via separate goroutines using Sink functions applied to this event. You can define sink function easily, see the source of console.go and file.go files. If Sink function returns error, it is processed by BrokenSinkReporter function.

Log storage support

From the box - STDERR, local file, Journald, Loggly, Telegram channel or group via bot.

With little effort - anything. You just need to implement simple function that will process events, like this AddSink one

	package main

	import (
	  	"fmt"
		"gopkg.in/vodolaz095/vdlog.v3"
	)

	func main(){
		// it is custom sink for events of type `feedback`
		vdlog.AddSink("feedback", func(e vdlog.Event) error {
			fmt.Println("Feedback", e.String())
			return nil
		})

		vdlog.EmitError("test", fmt.Errorf("test %s", "error"), vdlog.H{"error": "error"})
		vdlog.EmitWarn("test", vdlog.H{"warn": "warn"})
		
		//this even will be processed by our defined sink `feedback`
		vdlog.EmitInfo("feedback", vdlog.H{"info": "info"})
		
		vdlog.EmitVerbose("test", vdlog.H{"verbose": "verbose"})
		vdlog.EmitDebug("test", vdlog.H{"debug": "debug"})
		vdlog.EmitSilly("test", vdlog.H{"silly": "silly"})

		//wait until all events are processed
		vdlog.FlushLogs()
	}

How to send messages to Telegram channel or group

  1. create telegram bot - see https://core.telegram.org/bots.
  2. invite this bot as admin of channel or as member of group you want to recieve log entries too.
  3. set up log sink using your bot token (something like this - 286759464:AAFRalklssMW9hsZ592O8CxZo63QU7KM7d0)
  4. Get channel/group id using console telegram client and channel_info or chat_info commands
  5. Find proper id for channel. There is a crazy solution. For public/private channel you need to prepend -100 to its id for notifications to be delivered.
  6. Enable sink for delivering events
	vdlog.LogToTelegram("286759464:AAFRalklssMW9hsZ592O8CxZo63QU7KM7d0", "-1001055587116", vdlog.LevelInfo)

How does logged events looks like (in JSON format)?

      {
        //unique UUID of event, the same reported to all sinks
        "uuid": "990d28fd-4461-480a-be7e-08abacc9bdeb",
        "timestamp": "2017-02-26T19:45:13.398512249+03:00",
  
        //type and level
        "level": 2,
        "levelString": "INFO",
        "type": "vdlogUnitTest",
  
        //event payload, metadata
        "metadata": {
          "someText":"text",
          "someNumber":10,
          "someArray":[0,"1","b"]
        },
  
        //Server info
        "hostname":"server.local",
        "pid": 11337,
  
        //Where does the logger function was actually called
        "filename": "/var/www/localhost/index.php",
        "line": 2,
        "called": "/var/www/localhost/index.php:2"
      }

Installing

   go get gopkg.in/vodolaz095/vdlog.v3

Basic example

	package main

	import (
		"fmt"
		"gopkg.in/vodolaz095/vdlog.v3"
	)

	func main(){
		vdlog.SetConsoleVerbosity(vdlog.LevelSilly)
		vdlog.SetConsoleJSON() //for pretty printing json

		vdlog.EmitError("test", fmt.Errorf("test %s", "error"), vdlog.H{"error": "error"})
		vdlog.EmitWarn("test", vdlog.H{"warn": "warn"})
		vdlog.EmitInfo("feedback", vdlog.H{"info": "info"})
		vdlog.EmitVerbose("test", vdlog.H{"verbose": "verbose"})
		vdlog.EmitDebug("test", vdlog.H{"debug": "debug"})
		vdlog.EmitSilly("test", vdlog.H{"silly": "silly"})


		//wait until all events are processed
		vdlog.FlushLogs()
	}

Full example of usage

	package main
	
	import (
	  "fmt"
	  "log"
  	  "gopkg.in/vodolaz095/vdlog.v3"
	)

	func main() {
		/*
		 *  Set Console Verbosity level
		 */
		vdlog.SetConsoleVerbosity(vdlog.LevelSilly) //highest verbosity
		vdlog.SetConsoleVerbosity(vdlog.LevelDebug)
		vdlog.SetConsoleVerbosity(vdlog.LevelInfo)
		vdlog.SetConsoleVerbosity(vdlog.LevelWarn)
		vdlog.SetConsoleVerbosity(vdlog.LevelError) //lowest verbosity
	
		/*
		 * Enable output to local file
		 */

		//LogErrorsToFile outputs errors only
		vdlog.LogErrorsToFile("/var/log/my_vdlog_errors.log")

		//LogNormalToFile outputs events from error to debug levels
		vdlog.LogNormalToFile("/var/log/my_vdlog.log")

		//We can log defined level ranges to file
		vdlog.LogToFile("/var/log/onlyInfoAndWarn.log", vdlog.LevelWarn, vdlog.LevelInfo)


		/*
		 * Logging to Journalctl on local server (works only in linux!)
		 */
		vdlog.LogToLocalJournald()

		/*
		 * Logging to Journalctl on remote server (works only in linux!)
		 */
		vdlog.LogToRemoteJournaldViaTCP("logger.example.org", 514)
		vdlog.LogToRemoteJournaldViaUDP("logger.example.org", 514)

		/*
		 * Logging to Loggly.com
		 */
		vdlog.LogToLoggly("{YOUR LOGGLY TOKEN PLS}", true) //true = https, false = http

	
		/*
		 * Add custom sink for storing events
		 * Currently, this sink outputs to STDOUT only events from `feedback`
		 * facility with level lower and including the `LevelInfo`
		 * If Payload equals to `bad`, error is returned
		 */
		vdlog.AddSink("feedback", func(e vdlog.Event) error {
			// we ignore events not related for feedback facility
			if e.Facility != "feedback" {
				return nil
			}
			//we ignore events of low priority
			if e.Level > vdlog.LevelInfo {
				return nil
			}
			//check if payload is the proper one
			if e.Payload == "bad" {
				return fmt.Errorf("bad event")
			}
	
			//start pretty printing
			fmt.Println("===================")
			fmt.Printf("%v seconds ago event with level %s occured!\n",
				e.Ago().Seconds(),
				e.GetLevelString())
	
			//Output event as string
			fmt.Println(e.String())
			//will output something like
			// Dec 08 23:49:32 TestLoggerLog VERBOSE : verbose verbose
	
			//Output event as string with caller information - i.e.
			//where in source code does the message was called
			fmt.Println(e.StringWithCaller())
			//will output something like
			// Dec 08 23:49:32 TestLoggerLog VERBOSE <File: /home/vodolaz095/projects/go/src/bitbucket.org/vodolaz095/vdlog/vdlog_test.go:61>: verbose verbose
	
			//Output JSON representation of message (slice of bytes converted to string)
			fmt.Println("JSON of event:", string(e.ToJSON()))
	
			fmt.Println("===================")
	
			//Sink processed event properly
			return nil
		})
	
		/*
		 * Add function to report sink misbehaviour - i.e. when it returns error
		 */
		vdlog.BrokenSinkReporter = func(brokenSinkName string, eventThatCloggedIt vdlog.Event, errorRecievedFromSink error) {
			fmt.Printf("Sink %s is broken by event %s with error %s", brokenSinkName, eventThatCloggedIt.String(), errorRecievedFromSink.Error())
			panic("broken sink")
		}
	
		/*
		 * Using global logger
		 */
		vdlog.EmitError("test", fmt.Errorf("test %s", "error"), vdlog.H{"error": "error"})
		vdlog.EmitWarn("test", vdlog.H{"warn": "warn"})
		vdlog.EmitInfo("test", vdlog.H{"info": "info"})
		vdlog.EmitVerbose("test", vdlog.H{"verbose": "verbose"})
		vdlog.EmitDebug("test",vdlog. H{"debug": "debug"})
		vdlog.EmitSilly("test", vdlog.H{"silly": "silly"})

	
		/*
		 * Using custom logger for `feedback` facility
		 */
		feedbackLogger := New("feedback")
		feedbackLogger.EmitError(fmt.Errorf("test %s", "error"), H{"error": "error"})
		feedbackLogger.EmitWarn(H{"warn": "warn"})
		feedbackLogger.EmitInfo(H{"info": "info"})
		feedbackLogger.EmitVerbose(H{"verbose": "verbose"})
		feedbackLogger.EmitDebug(H{"debug": "debug"})
		feedbackLogger.EmitSilly(H{"silly": "silly"})

		/*
		 * Using popular https://godoc.org/log package
		 */
		log.SetOutput(vdlog.CreateIoWriter(vdlog.LevelError, "test"))
		log.Printf("testing %s", "ioWriterLog")

		//wait until all events are processed
		vdlog.FlushLogs()
	}

The MIT License (MIT)

Copyright (c) 2016 Ostroumov Anatolij

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.

vdlog's People

Contributors

vodolaz095 avatar

Watchers

 avatar  avatar  avatar

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.