Giter Site home page Giter Site logo

amankapoor / logrus-wrapper Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 5 KB

Logrus wrapper on top of github.com/prometheus/common/log but with features to log to a file and WithFields function

Go 100.00%
logrus wrapper wrappers logrus-wrappers golang-wrapper golang go

logrus-wrapper's Introduction

Logrus Wrapper

This is a wrapper of Logrus and the original work is of github.com/prometheus/common/log authors. I have added two functionalities on top of the existing code.

Features added in short

  1. Log to a file/or basically to any io.Writer
  2. Added WithFields functionality

Details

PS: The code which I changed refers to commit #61f87aac8082fa8c3c5655c7608d7478d46ac2ad.

Feature 1

This is an improvement for github.com/prometheus/common/log package where it lets you write logs to a file. Existing NewLogger function looked like

func NewLogger(w io.Writer) Logger {
	l := logrus.New()
	l.Out = w
	return logger{entry: logrus.NewEntry(l)}
}

But this implementation writes log to console only/stdout. See issue #82, #104 raising the same thing.

I changed the function to this:

func NewLogger() Logger {
	l := logrus.New()
	return logger{entry: logrus.NewEntry(l)}
}

and added a new SetOutput function for the purpose of setting output i.e., writing to a file.

func SetOutput(w io.Writer) {
	origLogger.Out = w
}

Feature 2

I have also added functionality to call WithFields function, so now you can do something like this:

log.WithFields(log.Fields{
    "animal": "walrus",
    "size":   10,
  }).Info("A group of walrus emerges from the ocean")

Usage

To use this package and log to a file, add the following lines in your package:

f, err := os.OpenFile("logs.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
	log.Fatal(err)
}
defer f.Close()
log.NewLogger()
log.SetOutput(f)
log.WithFields(log.Fields{
    "animal": "walrus",
    "size":   10,
  }).Info("A group of walrus emerges from the ocean")

logrus-wrapper's People

Contributors

amankapoor avatar

Stargazers

 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.