Giter VIP home page Giter VIP logo

gslog's Introduction

gslog

A structured log interface library for golang.

gslog.Logger

Structured logger interface.

gslog.Backend

Interface for logging service provider.

Example

package main

import (
	"fmt"
	"os"

	"github.com/hindsights/gslog"
	"github.com/hindsights/gslog-logrus/gslogrus"
	"github.com/hindsights/gslog-zap/gszap"
	"github.com/sirupsen/logrus"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

type logLevelChecker struct {
	level zapcore.Level
}

func (checker logLevelChecker) Enabled(l zapcore.Level) bool {
	return l >= checker.level
}

func main() {
	fmt.Println("test")
	gslog.Info("start")
	logger := gslog.GetSugaredLogger("app")
	flogger := gslog.GetLogger("app")
	logger.Debug("debug", "key1", 1, "key2", "val2")
	logger.Info("info", "int", 1, "str", "val2")
	logger.Warn("warn")
	logger.Error("error", "key1", 1, "key2", "val2")
	flogger.Error("field output")
	flogger.Int("val", 567).Info("field output")
	flogger.Int("key1", 1).Str("key2", "val2").Error("field output")
	flogger.Int("key1", 1).Str("key2", "val2").Int("val", 567).Info("field output")
	gslog.Debugf("debugf %s", "name")
	gslog.Infof("infof %s", "value")
	gslog.Warnf("warnf %d", 20)
	gslog.Errorf("errorf %v", 100)

	logrusLogger := logrus.New()
	logrusLogger.SetFormatter(&logrus.TextFormatter{
		DisableColors:  true,
		FullTimestamp:  true,
		DisableSorting: true,
	})
	gslog.SetBackend(gslogrus.NewBackend(logrusLogger))
	gslog.Info("gs-logrus-hello")
	logger = gslog.GetSugaredLogger("logrus")
	flogger = gslog.GetLogger("logrus")
	logger.Info("output to logrus", 123)
	flogger.Int("val", 123).Info("output to logrus")

	consoleWriter := zapcore.Lock(os.Stdout)
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
	core := zapcore.NewCore(consoleEncoder, consoleWriter, logLevelChecker{level: zapcore.DebugLevel})
	tempLogger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
	gslog.SetBackend(gszap.NewBackend(tempLogger))
	gslog.Info("gs-zap-hello")
	gslog.Warn("zap-start")
	logger = gslog.GetSugaredLogger("zap")
	flogger = gslog.GetLogger("zap")
	logger.Info("output to zap", 123)
	flogger.Int("val", 123).Info("output to zap")
}

gslog's People

Contributors

hindsights 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.