Git Product home page Git Product logo

gzap's Introduction

gzap - Graylog Integrated Zap Logger

GoDoc CircleCI Go Report Card

Gzap provide fast structured leveled logging using zap, and a TCP/UDP Graylog logsink (TLS supported). Both zap and Graylog librarys are versioned locked within the applications so no other external dependencies required.

Getting Stated

To use gzap, first import it:

import "gopkg.in/dailymuse/gzap.v1"

The Graylog logsink is only enabled for Production and Staging environments. So you'll need to set a GRAYLOG_ENV environment variable with either of the following correlating states.

GRAYLOG_ENV Environment Graylog enabled?
0 Test (no-op logger)
1 Dev
2 Staging
3 Production

Environment Variables

To properly use gzap you'll need to set your configurations via Environment variables. The following are configurable Envs:

Env Name Description
GRAYLOG_ENV A number 0 - 3 describing the Graylog loggin environment you wish to use (Refrence table above)
GRAYLOG_HOST Hostname that your graylog is currently listening on example.graylog.com
ENABLE_DATADOG_JSON_FORMATTER set to "true" to enable json formatted logs.

Internal API

The logger that is publicly exposed is the zap Logger. You can reference what log levels are available for use here). Below are a few examples:

func (log *Logger) DPanic(msg string, fields ...zapcore.Field)
func (log *Logger) Debug(msg string, fields ...zapcore.Field)
func (log *Logger) Error(msg string, fields ...zapcore.Field)
func (log *Logger) Fatal(msg string, fields ...zapcore.Field)
func (log *Logger) Info(msg string, fields ...zapcore.Field)
func (log *Logger) Panic(msg string, fields ...zapcore.Field)
func (log *Logger) Warn(msg string, fields ...zapcore.Field)

All zap fields needed for logging are also exposed by gzap.

gzap.Logger.Error("this is an example Error log",
        gzap.String("variable", "some-variable-here"),
)

For any other information please take a look at the gzap Godoc.

Example Usage

package main

import (
    "time"

    "gopkg.in/dailymuse/gzap.v1"
)

func main() {
    // Instantiate a global logger.
    if err := gzap.InitLogger(); err != nil {
        panic(err)
    }

    // Example Info log.
    gzap.Logger.Info("this is an example Info log",
        gzap.String("process name", "some-fake-name"),
        gzap.Int64("expectedDocs", int64(255)),
        gzap.Int64("docsUploaded", int64(100)),
    )

    // Example Error log.
    gzap.Logger.Error("this is an example Error log",
        gzap.Error(errors.New("example error")),
        gzap.String("index name", "my-full-index-name"),
        gzap.Float64("time elapsed", float64(1002)),
    )

    // Example Debug log.
    gzap.Logger.Debug("this is an example Debug log",
        gzap.String("variable", "some-variable-here"),
    )
}

Important info

Contributing

In order to contribute you'll need to have a valid go environment setup.

If you need to install go, see installation instructions here.

Tests Logs are a no-op

Tests that run application code containing logs will not print those logs by default. The Test logger is a no-op to reduce noise during testing.

gzap's People

Contributors

ian-axelrod avatar shikkic avatar

Watchers

 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.