Code Monkey home page Code Monkey logo

loglevel-go's Introduction

loglevel

A simple standard out logger and formatter that supports log levels, namespaces and coloured output.

Get started

Download the package

go get -t github.com/amimof/loglevel-go

Import it in your code

package main

import "github.com/amimof/loglevel-go"

func main() {

    // Create an instance of loglevel
    loglevel := loglevel.New()
    
    // Set loglevel. 3=Debug, 2=Info, 1=Warn, 0=Error. Defaults to Info.
    loglevel.SetLevel(3)
    
    // Log away
    loglevel.Debug("Debug message")
    loglevel.Info("Info message")
    loglevel.Warn("Warn message")
    loglevel.Error("Error message")

}

Result

2017-01-21T13:56:28+01:00 DEBUG Debug message
2017-01-21T13:56:28+01:00 INFO Info message
2017-01-21T13:56:28+01:00 WARN Warn message
2017-01-21T13:56:28+01:00 ERROR Error message

Formatting

loglevel supports both non-format and format print methods. Formatting is as good as in the native fmt package but with the additional log level functionality.

package main

import "github.com/amimof/loglevel-go"

func main() {
    // Create an instance of loglevel
    loglevel := loglevel.New()
    
    // Set loglevel. 3=Debug, 2=Info, 1=Warn, 0=Error. Defaults to Info.
    loglevel.SetLevel(3)
        
    fname := "Luke"
    lname := "Skywalker"
    loglevel.Infof("The Jedi %s, %s", fname, lname)
}

Result:

2017-01-21T13:53:54+01:00 INFO The Jedi Luke, Skywalker

Namespaces

You can created multiple loglevel instances in your app. Assign different names to them, to make debugging app packages an modules easy.

package main

import "github.com/amimof/loglevel-go"

func main() {
    
    // Create an instance of loglevel
    loglevel := loglevel.New().SetLevel(3)
    
    // Set the namespace of this loglevel instance
    loglevel.Name = "StarWarsNames"    
    loglevel.PrintName =  true

    fname := "Luke"
    lname := "Skywalker"
    loglevel.Infof("The Jedi %s, %s", fname, lname)
}

Result:

2017-01-21T13:53:54+01:00 INFO StarWarsNames The Jedi Luke, Skywalker

Options

If you like vanilla, fmt.Println(), formatting. Disable those fiels.

package main

import "github.com/amimof/loglevel-go"

func main() {
    // Create an instance of loglevel
    loglevel := loglevel.New()
    loglevel.PrintTime = false
    loglevel.PrintLevel = false
    loglevel.PrintName = false
    loglevel.Warn("Achtung, Achtung!")
}

Result

Achtung, Achtung!

loglevel-go's People

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.