GithubHelp home page GithubHelp logo

loggr's Introduction

License: GPL v3 Build Status Coverage Status Documentation

Loggr.go

A minimalist logger that writes sensible messages

Latest release: v0.0.1

Go documentation at godoc.org

Key features

  • messages tagged by type (info, error, debug)
  • optional verbose mode to toggle debug statements
  • timestamps fixed to UTC and printed as ISO 8601 string
  • extensible, accepts io.Writer object for output

Design

Created so that Go programs could follow the existing log structure of urbanriskmap projects, and enable toggling of debug statements at run time to quickly adjust verbosity of output logs. Inspired somewhat by 12factor.net/logs.

Install

go get github.com/talltom/loggr

Use

Example 1. Logging to Stdout

package main

import (
  "github.com/talltom/loggr/loggr"
  "os"
)

func main(){
  // create structure with verbose debug printing enabled
  log := loggr.Log {
    Verbose: true,
    Writer: os.Stdout,
  }

  log.Info("Info statement")
  log.Error("Error statement")
  log.Debug("Debug statement optional, requires verbose mode")
}

Result

2017-08-30T15:12:03Z - info: Info statement
2017-08-30T15:12:03Z - error: Error statement
2017-08-30T15:12:03Z - debug: Debug statement optional, requires verbose mode

Example 2. Logging to File

Log destinations (e.g. files) are managed by the user.

package main

import (
  "bufio"
  "github.com/talltom/loggr/loggr"
  "os"
)

func main(){

  // File
	f, err := os.Create("/tmp/logger.log")
	if err != nil {
		panic(err)
	}
	defer f.Close()

  // Writer
	w:= bufio.NewWriter(f)
	defer w.Flush()

  // Logger
  var log = loggr.Log {
  		Verbose: false,
  		Writer: w,
  }
  log.Info("Now writing to file")
  log.Debug("This will not be written as logger not in verbose mode")
}

Example 3. Error handling

Errors in loggr are returned to the user. Available error types are defined in loggr.go. In this example no Writer object is set so the log cannot be written.

package main

import (
  "github.com/talltom/loggr/loggr"
)

func main(){

  // Logger
  var log = loggr.Log {
  		Verbose: false,
  		// Writer: w, -> forgot to set valid writer object!
  }
  err := log.Info("Now writing to file")
  if err != nil {
    if err == loggr.ErrInvalidWriter {
      // Forgot to set writer
    } else {
      // Some other error occurred
    }
  }
}

License

GNU GPLv3, see LICENSE.txt

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.