Giter Site home page Giter Site logo

nats-logr's Introduction

nats-logr

A logr implementation using https://nats.io

Usage

Code Example

Description

To use nats-logr, you have to do the following:

  • Run the following in a window: $ nats-streaming-server --cluster_id=example-cluster
  • Run the following .go file in second window:
package main

import (
	"fmt"
	"io"
	"os"
	"os/signal"
	"time"

	"github.com/nats-io/stan.go"
)

func processMsg(msg *stan.Msg) {
	fmt.Printf("Received on [%s]: '%s'\n", msg.Subject, msg)
	msg.Ack()
}

func logCloser(c io.Closer) {
	if err := c.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "Close error: %v", err)
		os.Exit(1)
	}
}

func main() {
	conn, err := stan.Connect(
		"example-cluster",
		"example-client-2",
		stan.NatsURL(stan.DefaultNatsURL),
		stan.SetConnectionLostHandler(func(_ stan.Conn, reason error) {
			fmt.Fprintf(os.Stderr, "Connection lost, reason: ", reason)
			os.Exit(1)
		}),
	)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, stan.DefaultNatsURL)
		os.Exit(1)
	}
	defer logCloser(conn)

	fmt.Printf("Connected to %s clusterID: [%s] clientID: [%s]\n", stan.DefaultNatsURL, "example-cluster", "example-client-2")

	sub, err := conn.QueueSubscribe(
		"nats-log-example",
		"test", func(msg *stan.Msg) {
			processMsg(msg)
		}, stan.SetManualAckMode(), stan.DurableName("i-remember"), stan.DeliverAllAvailable(), stan.AckWait(time.Second),
	)
	defer logCloser(sub)
	
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt)
	<-ch
}
  • Finally, run the following code in another window:
package main

import (
	"errors"
	"flag"
	"fmt"

	natslogr "gomodules.xyz/nats-logr"

	"github.com/nats-io/stan.go"
)

func main() {
	flag.Set("v", "4")
	flag.Parse()

	natslogr.InitFlags(nil)
	defer natslogr.Flush()

	opts := natslogr.Options{
		ClusterID: "example-cluster",
		ClientID:  "example-client",
		Subject:   "nats-log-example",
	}
	logger := natslogr.NewLogger(opts).
		WithName("Example").
		WithValues("withKey", "withValue")
	logger.Info("Log Example", "key", "value")

	logger.V(0).Info("Another Log Example", "logr", "nats-logr")

	logger.Error(errors.New("an error has been occured"), "error msg", "logr", "nats-logr")
}

Now, you will see the logs in the second window.

Acknowledgement

The logger parts of this library has been adapted from k8s.io/klog which itself is a fork of golang/glog.

nats-logr's People

Contributors

tamalsaha avatar masudur-rahman avatar 1gtm avatar

Stargazers

Nikita Zhenev avatar Kirill Freiman avatar Bao Nguyen avatar  avatar  avatar

Watchers

 avatar James Cloos avatar

nats-logr's Issues

Simplify codebase

We should remove the flags that does not make sense in this context.

	flagset.BoolVar(&logging.toNatsServer, "logtonatsserver", true, "publish log to nats streaming server")
	flagset.StringVar(&logging.logDir, "log_dir", "", "If non-empty, write log files in this directory")
	flagset.StringVar(&logging.logFile, "log_file", "", "If non-empty, use this log file")
	flagset.BoolVar(&logging.toStderr, "logtostderr", false, "log to standard error instead of nats streaming server")
	flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as nats streaming server")
	flagset.Var(&logging.verbosity, "v", "number for the log level verbosity")
	flagset.BoolVar(&logging.skipHeaders, "skip_headers", false, "If true, avoid header prefixes in the log messages")
	flagset.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr")
	flagset.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging")
	flagset.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace")

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.