Coder Social home page Coder Social logo

beaver's Introduction

beaver build status GoDoc

A set of functions which may be useful when dealing with JSON or logger.
For more information please visit Godoc.

Install

go get github.com/Hunsin/beaver

JSON

Example of reading/writing JSON file and GET/POST JSON from http services.

package main

import (
  "net/http"
  bv "github.com/Hunsin/beaver"
)

type example struct {
  Hello string `json:"hello"`
  Year  int    `json:"year"`
  IP    string `json:"ip"`
}

func main() {
  e := example{
    Hello: "Hello World!",
    Year:  2017,
  }

  // writing JSON file; ignore error
  bv.JSON(&e).WriteFile("example.json")

  // reading JSON file; ignore error
  out := example{}
  js  := bv.JSON(&out)
  js.Open("example.json")

  // now you have the values
  println(out.Hello) // Hello World!
  println(out.Year)  // 2017

  // serving JSON data; ignore error
  http.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
    js.Serve(w, http.StatusOK)
  })

  // get values from other service; ignore error
  // http header is nil in this case
  js.Get("http://ip.jsontest.com", nil)
  println(js.IP)

  // send data to another service
  // the second parameter is your custom http.Header
  err := js.Post("https://url.of.service", nil)
  if err != nil {
    println(err.Error)
  }
}

Logger

Logger wraps log.Logger with additional option to set log levels. Let's see the example:

package main

import (
  "log"
  "os"
  bv "github.com/Hunsin/beaver"
)

func main() {
  f, _ := os.Create("file_name.log")
  defer f.Close()

  // setting the output destination
  bv.LogOutput(f)

  // you can decide what level of logs should write, by default all
  // there are five levels available: Fatal, Error, Warn, Info & Debug
  bv.LogLevel(bv.Lerror | bv.Linfo)

  bv.Info("Hello World!") // 2018/02/06 00:31:28 INFO : Hello World!
  
  // you can define your log tag style
  t := bv.LTag{"| Fatal | ", "| Error | ", "| Warn | ", "| Info | ", "| Debug | "}

  // you can chain functions in configuration
  l := bv.NewLogger().Output(f).Tags(t).Flags(log.Lshortfile)
  l.Error("Hello again!!") // main.go:27: | Error | Hello again!!
}

beaver's People

Contributors

hunsin avatar

Stargazers

 avatar

Watchers

James Cloos 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.