Sha256: 1ea944a55a76dbb05d6917134403c0e9fcc4931edfc773151e60bcb27abf318e

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 KB

Contents

package logrus

import (
	"bufio"
	"io"
	"runtime"
)

// Writer at INFO level. See WriterLevel for details.
func (logger *Logger) Writer() *io.PipeWriter {
	return logger.WriterLevel(InfoLevel)
}

// WriterLevel returns an io.Writer that can be used to write arbitrary text to
// the logger at the given log level. Each line written to the writer will be
// printed in the usual way using formatters and hooks. The writer is part of an
// io.Pipe and it is the callers responsibility to close the writer when done.
// This can be used to override the standard library logger easily.
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
	return NewEntry(logger).WriterLevel(level)
}

func (entry *Entry) Writer() *io.PipeWriter {
	return entry.WriterLevel(InfoLevel)
}

func (entry *Entry) WriterLevel(level Level) *io.PipeWriter {
	reader, writer := io.Pipe()

	var printFunc func(args ...interface{})

	switch level {
	case TraceLevel:
		printFunc = entry.Trace
	case DebugLevel:
		printFunc = entry.Debug
	case InfoLevel:
		printFunc = entry.Info
	case WarnLevel:
		printFunc = entry.Warn
	case ErrorLevel:
		printFunc = entry.Error
	case FatalLevel:
		printFunc = entry.Fatal
	case PanicLevel:
		printFunc = entry.Panic
	default:
		printFunc = entry.Print
	}

	go entry.writerScanner(reader, printFunc)
	runtime.SetFinalizer(writer, writerFinalizer)

	return writer
}

func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		printFunc(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		entry.Errorf("Error while reading from Writer: %s", err)
	}
	reader.Close()
}

func writerFinalizer(writer *io.PipeWriter) {
	writer.Close()
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruby_snowflake_client-1.3.7 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.6 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.5 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.4 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.4.pre.debug ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.3.pre.debug ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.2 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.1 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.3.0 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.2.1 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.2.0 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.1.1 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.1.0 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.0.2 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.0.1 ext/vendor/github.com/sirupsen/logrus/writer.go
ruby_snowflake_client-1.0.0 ext/vendor/github.com/sirupsen/logrus/writer.go