Code Monkey home page Code Monkey logo

noglog's Introduction

noglog Build Status Go Report Card GoDoc

noglog is a replacement for github.com/golang/glog implementation.

This package is a "bring you own" logger for glog replacement. It will replace all the glog calls with any logger that implements noglog.Logger interface.

You can go from this:

ERROR: logging before flag.Parse: I0921 14:49:49.283733       1 leaderelection.go:175] attempting to acquire leader lease example-lock...

to this:

INFO[0000] attempting to acquire leader lease  example-lock...  source=k8s src="leaderelection.go:175"

or whatever format you want (or don't show at all).

Features

  • Replace glog messages with your own logger (implement small interface and done).
  • No flags modification.

Getting Started

For a complete example, check this

Import library

First we need to add noglog as dependency, but it will need to be placed on github.com/golang/glog import path, so github.com/slok/noglog code replaces github.com/golang/glog

Using go modules

require (
    github.com/google/glog master
)

replace (
    github.com/google/glog => github.com/slok/noglog master
)

Using dep

[[override]]
  name = "github.com/golang/glog"
  source = "github.com/slok/noglog"
  branch = "master"

Set our custom logger

Import glog and the first thing that your app should do is instantiate your logger and set as the glog logger. Example:

import (
    "github.com/golang/glog"
)

func main() {
    // Import custom logger that satisfies noglog.Logger interface.
    logger := mylogger.New()

    glog.SetLogger(logger)
}

For security reasons if you don't set a logger, it will use a dummy logger that will disable Kubernetes logs. Sometimes this is useful also. Your custom logger needs to satisfy noglog.Logger interface. You have a helper if you want to create a logger using functions instead of creating a new type, noglog.LoggerFunc.

This helper gives a lot of power to set up loggers easily. Some examples...

Logrus:

import (
    "github.com/google/glog"
    "github.com/sirupsen/logrus"
)

func main() {
    // Our app logger.
    logger := logrus.New()
    logger.SetLevel(logrus.DebugLevel)

    // Set our glog replacement.
    glog.SetLogger(&glog.LoggerFunc{
        DebugfFunc: func(f string, a ...interface{}) { logger.Debugf(f, a...) },
        InfofFunc:  func(f string, a ...interface{}) { logger.Infof(f, a...) },
        WarnfFunc:  func(f string, a ...interface{}) { logger.Warnf(f, a...) },
        ErrorfFunc: func(f string, a ...interface{}) { logger.Errorf(f, a...) },
    })

    glog.Info("I'm batman!")
}

Example for zap:

import (
    "github.com/google/glog"
    "go.uber.org/zap"
)

func main() {
    // Our app logger.
    logger, _ := zap.NewProduction()
    slogger := logger.Sugar()

    // Set our glog replacement.
    glog.SetLogger(&glog.LoggerFunc{
        DebugfFunc: func(f string, a ...interface{}) { slogger.Debugf(f, a...) },
        InfofFunc:  func(f string, a ...interface{}) { slogger.Infof(f, a...) },
        WarnfFunc:  func(f string, a ...interface{}) { slogger.Warnf(f, a...) },
        ErrorfFunc: func(f string, a ...interface{}) { slogger.Errorf(f, a...) },
    })

    glog.Info("I'm batman!")
}

Example for zerolog:

import (
    "os"

    "github.com/google/glog"
    "github.com/rs/zerolog"
)

func main() {
    // Our app logger.
    w := zerolog.ConsoleWriter{Out: os.Stderr}
    logger := zerolog.New(w).With().Timestamp().Logger()

    // Set our glog replacement.
    glog.SetLogger(&glog.LoggerFunc{
        DebugfFunc: func(f string, a ...interface{}) { logger.Debug().Msgf(f, a...) },
        InfofFunc:  func(f string, a ...interface{}) { logger.Info().Msgf(f, a...) },
        WarnfFunc:  func(f string, a ...interface{}) { logger.Warn().Msgf(f, a...) },
        ErrorfFunc: func(f string, a ...interface{}) { logger.Error().Msgf(f, a...) },
    })

    glog.Info("I'm batman!")
}

Why

Glog is a logger used on some important projects like Kubernetes, Glog modifies globals like flag of your program and it can't be done nothing to disable, modify...

For example in Kubernetes glog is around all the source code, they don't use a common app logger interface that it can be reimplement with any log implementation, or pass any logger as dependency injection, so the Kubernetes extensions that use client-go will get flags modification, unstructured logs around it well set log format lines...

When to use noglog

  • Tired of glog logs.
  • Every extension/app of Kubernetes I do has annoying unstructured logs.
  • My app flags have glog flags that I didn't ask for.
  • ERROR: logging before flag.Parse: W0922 WTF when using a custom flag.FlagSet.

Alternatives

Although noglog can use any logger due to the simple interface, there are other alternatives that replace the logger for a specific logger with defaults, if you don't want to bring your own or customize these loggers here are the alternatives:

noglog's People

Contributors

dmamolina avatar erjanmx avatar slok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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