package module
Version: v0.1.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2019 License: Apache-2.0 Imports: 17 Imported by: 2



A logr implementation using


Code Example

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 (


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

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

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

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

	sub, err := conn.QueueSubscribe(
		"test", func(msg *stan.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)

  • Finally, run the following code in another window:
package main

import (

	natslogr ""


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

	defer natslogr.Flush()

	opts := natslogr.Options{
		ClusterID: "example-cluster",
		ClientID:  "example-client",
		Subject:   "nats-log-example",
	logger := natslogr.NewLogger(opts).
		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.


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




View Source
const (
	Subject     = "cluster_subject"
	ClusterID   = "cluster_id"
	ClientID    = "client_id"
	NatsURL     = "nats_url"
	ConnectWait = "connect_wait"

Constants for Nats Streaming Server connection


View Source
var Stats struct {
	Info, Warning, Error OutputStats

Stats tracks the number of lines of output and number of bytes per severity level. Values must be read with atomic.LoadInt64.


func Flush

func Flush()

Flush flushes all pending log I/O.

func InitFlags

func InitFlags(flagset *flag.FlagSet)

InitFlags is for explicitly initializing the flags

func NewLogger

func NewLogger(opts Options) logr.Logger

NewLogger returns a logr.Logger which is implemented by nats-logr


type Level

type Level int32

Level specifies a level of verbosity for V logs. *Level implements flag.Value; the -v flag is of type Level and should be modified only through the flag.Value interface.

func (*Level) Get

func (l *Level) Get() interface{}

Get is part of the flag.Value interface.

func (*Level) Set

func (l *Level) Set(value string) error

Set is part of the flag.Value interface.

func (*Level) String

func (l *Level) String() string

String is part of the flag.Value interface.

type Options

type Options struct {
	ClusterID, ClientID, NatsURL, Subject string

The Options contains necessary information about nats-connection

type OutputStats

type OutputStats struct {
	// contains filtered or unexported fields

OutputStats tracks the number of output lines and bytes written.

func (*OutputStats) Bytes

func (s *OutputStats) Bytes() int64

Bytes returns the number of bytes written.

func (*OutputStats) Lines

func (s *OutputStats) Lines() int64

Lines returns the number of lines written.

type Verbose

type Verbose bool

Verbose is a boolean type that implements Infof (like Printf) etc. See the documentation of V for more information.

func V

func V(level Level) Verbose

V reports whether verbosity at the call site is at least the requested level. The returned value is a boolean of type Verbose, which implements Info, Infoln and Infof. These methods will write to the Info log if called. Thus, one may write either

if natslog.V(2) { natslog.Info("log this") }


natslog.V(2).Info("log this")

The second form is shorter but the first is cheaper if logging is off because it does not evaluate its arguments.

Whether an individual call to V generates a log record depends on the setting of the -v and --vmodule flags; both are off by default. If the level in the call to V is at least the value of -v, or of -vmodule for the source file containing the call, the V call will log.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto