Code Monkey home page Code Monkey logo

zadapters's Introduction

zadapters

Some simple adapters for Zerolog so it can be used with other packages that might define their own Logger interface

Couchbase

package main

import(
	"os"
	
	"github.com/couchbase/gocbcore"
	"github.com/dcarbone/zadapters/zgocbcore"
	"github.com/rs/zerolog"
)

func main() {
	// init a logger to use 
	log := zerolog.New(os.Stdout).With().
		Timestamp().
		Fields(map[string]interface{}{"source": "couchbase"}).
		Logger()
	
	// create the adapter.
	adapter := zgocbcore.NewDefault(log)
	
	// set logger
	gocbcore.SetLogger(adapter)
	
	// presumably do something
}

LevelMap

The LevelMap maps a gocb.LogLevel to a zerolog.Level. If you wish a given Couchbase log level to be ignored, you may either simply not set it or set it to zerolog.Disabled.

There is a DefaultLevelMap that will be used if nil is passed as first arg to New() or if the Adapter is constructed using NewDefault()

And that's pretty much it.

Generic

Also included is a generic Adapter that allows a ZeroLog logger to be used with packages expecting a *log.Logger type. This will also satisfy those packages who create their own Logger interface type with the standard Print, Println, and Printf methods on them.

package main

import(
    "os"

    "github.com/dcarbone/zadapters/zstdlog"
    "github.com/rs/zerolog"
)

func main() {
    // init a logger to use 
    log := zerolog.New(os.Stdout)

    // creates a *log.Logger with no level prefix
    stdLogger := zstdlog.NewStdLogger(log)
    stdLogger.Println("hello world")
    // results in {"message":"hello world"}

    // creates a *log.Logger with a level prefix
    stdLeveledLogger := zstdlog.NewStdLoggerWithLevel(log, zerolog.InfoLevel)
    stdLeveledLogger.Println("hello world")
    // results in {"level":"info","message":"hello world"}
}

zadapters's People

Contributors

dcarbone avatar

Watchers

 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.