From 299ee952775f50ef63047af52f17a034d7da634b Mon Sep 17 00:00:00 2001 From: Patrick Hemmer Date: Thu, 8 Jan 2015 01:04:07 -0500 Subject: [PATCH 1/3] add io.Writer interface compatability --- exported.go | 4 ++++ input_writer.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 input_writer.go diff --git a/exported.go b/exported.go index d087124..fd092fc 100644 --- a/exported.go +++ b/exported.go @@ -9,6 +9,10 @@ var ( std = New() ) +func StandardLogger() *Logger { + return std +} + // SetOutput sets the standard logger output. func SetOutput(out io.Writer) { std.mu.Lock() diff --git a/input_writer.go b/input_writer.go new file mode 100644 index 0000000..8809915 --- /dev/null +++ b/input_writer.go @@ -0,0 +1,30 @@ +package logrus + +import ( + "bufio" + "io" + "runtime" +) + +func (logger *Logger) InputWriter() (*io.PipeWriter) { + inputReader, inputWriter := io.Pipe() + + go logger.inputWriterScanner(inputReader) + runtime.SetFinalizer(inputWriter, inputWriterFinalizer) + + return inputWriter +} + +func (logger *Logger) inputWriterScanner(inputReader *io.PipeReader) { + scanner := bufio.NewScanner(inputReader) + for scanner.Scan() { + logger.Print(scanner.Text()) + } + if err := scanner.Err(); err != nil { + logger.Fatal(err) + } +} + +func inputWriterFinalizer(inputWriter *io.PipeWriter) { + inputWriter.Close() +} From ed888975b3cc9170f10b256d99f1ec82f81e615b Mon Sep 17 00:00:00 2001 From: Patrick Hemmer Date: Tue, 20 Jan 2015 10:11:57 -0500 Subject: [PATCH 2/3] close inputReader on error instead of using Fatal() --- input_writer.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/input_writer.go b/input_writer.go index 8809915..e6db8e7 100644 --- a/input_writer.go +++ b/input_writer.go @@ -21,8 +21,9 @@ func (logger *Logger) inputWriterScanner(inputReader *io.PipeReader) { logger.Print(scanner.Text()) } if err := scanner.Err(); err != nil { - logger.Fatal(err) + logger.Errorf("Error while reading from InputWriter: %s", err) } + inputReader.Close() } func inputWriterFinalizer(inputWriter *io.PipeWriter) { From 51cbf81dde8386da666d00e393bef92087d28eee Mon Sep 17 00:00:00 2001 From: Patrick Hemmer Date: Mon, 26 Jan 2015 09:59:50 -0500 Subject: [PATCH 3/3] rename InputWriter -> Writer --- input_writer.go | 31 ------------------------------- writer.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 31 deletions(-) delete mode 100644 input_writer.go create mode 100644 writer.go diff --git a/input_writer.go b/input_writer.go deleted file mode 100644 index e6db8e7..0000000 --- a/input_writer.go +++ /dev/null @@ -1,31 +0,0 @@ -package logrus - -import ( - "bufio" - "io" - "runtime" -) - -func (logger *Logger) InputWriter() (*io.PipeWriter) { - inputReader, inputWriter := io.Pipe() - - go logger.inputWriterScanner(inputReader) - runtime.SetFinalizer(inputWriter, inputWriterFinalizer) - - return inputWriter -} - -func (logger *Logger) inputWriterScanner(inputReader *io.PipeReader) { - scanner := bufio.NewScanner(inputReader) - for scanner.Scan() { - logger.Print(scanner.Text()) - } - if err := scanner.Err(); err != nil { - logger.Errorf("Error while reading from InputWriter: %s", err) - } - inputReader.Close() -} - -func inputWriterFinalizer(inputWriter *io.PipeWriter) { - inputWriter.Close() -} diff --git a/writer.go b/writer.go new file mode 100644 index 0000000..90d3e01 --- /dev/null +++ b/writer.go @@ -0,0 +1,31 @@ +package logrus + +import ( + "bufio" + "io" + "runtime" +) + +func (logger *Logger) Writer() (*io.PipeWriter) { + reader, writer := io.Pipe() + + go logger.writerScanner(reader) + runtime.SetFinalizer(writer, writerFinalizer) + + return writer +} + +func (logger *Logger) writerScanner(reader *io.PipeReader) { + scanner := bufio.NewScanner(reader) + for scanner.Scan() { + logger.Print(scanner.Text()) + } + if err := scanner.Err(); err != nil { + logger.Errorf("Error while reading from Writer: %s", err) + } + reader.Close() +} + +func writerFinalizer(writer *io.PipeWriter) { + writer.Close() +}