Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: janeczku/stdemuxerhook
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: Superbalist/stdemuxerhook
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 5 commits
  • 5 files changed
  • 1 contributor

Commits on Dec 29, 2020

  1. fix logrus import

    bryanhorstmann committed Dec 29, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    a1a505b View commit details
  2. log warnings to stderr

    bryanhorstmann committed Dec 29, 2020

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    f09b981 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    5c2466c View commit details
  4. create go module

    bryanhorstmann committed Dec 29, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    28cf0db View commit details
  5. update README

    bryanhorstmann committed Dec 29, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    32ec944 View commit details
Showing with 26 additions and 9 deletions.
  1. +5 −5 README.md
  2. +5 −0 go.mod
  3. +10 −0 go.sum
  4. +4 −3 stdemuxerhook.go
  5. +2 −1 stdemuxerhook_bench_test.go
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A stdout/stderr demuxer hook for Logrus

[Logrus](https://github.com/Sirupsen/logrus) loggers always output all log levels to a single common output, e.g. `stderr`.
This logrus hook makes it so that logs with a severity below `Error` are written to `stdout` while all the important stuff goes to `stderr`.
[Logrus](https://github.com/sirupsen/logrus) loggers always output all log levels to a single common output, e.g. `stderr`.
This logrus hook makes it so that logs with a severity below `Warning` are written to `stdout` while all the important stuff goes to `stderr`.

You can also use the hook to demux logs to custom IO writers based on severity. Just override the default outputs using the hook's `SetOutput(infoLevel, errorLevel io.Writer)` method.

@@ -11,7 +11,7 @@ Given you have an application that uses a the logrus standard logger similar to

```go
import (
log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
)

func main() {
@@ -28,8 +28,8 @@ The only change required is adding in the hook. Make sure to configure the paren
```go
import (
log "github.com/Sirupsen/logrus"
"github.com/janeczku/stdemuxerhook"
log "github.com/sirupsen/logrus"
"github.com/Superbalist/stdemuxerhook"
)

func main() {
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/Superbalist/stdemuxerhook

go 1.14

require github.com/sirupsen/logrus v1.7.0
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7 changes: 4 additions & 3 deletions stdemuxerhook.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package stdemuxerhook

import (
"github.com/Sirupsen/logrus"
"io"
"io/ioutil"
"os"

"github.com/sirupsen/logrus"
)

// StdDemuxerHook demuxes logs to io.Writers based on
@@ -52,6 +53,8 @@ func (hook *StdDemuxerHook) Fire(entry *logrus.Entry) error {

switch entry.Level {
// stderr
case logrus.WarnLevel:
hook.stdErrLogger.WithFields(entry.Data).Warn(entry.Message)
case logrus.ErrorLevel:
hook.stdErrLogger.WithFields(entry.Data).Error(entry.Message)
case logrus.PanicLevel:
@@ -63,8 +66,6 @@ func (hook *StdDemuxerHook) Fire(entry *logrus.Entry) error {
hook.stdOutLogger.WithFields(entry.Data).Debug(entry.Message)
case logrus.InfoLevel:
hook.stdOutLogger.WithFields(entry.Data).Info(entry.Message)
case logrus.WarnLevel:
hook.stdOutLogger.WithFields(entry.Data).Warn(entry.Message)
}

return nil
3 changes: 2 additions & 1 deletion stdemuxerhook_bench_test.go
Original file line number Diff line number Diff line change
@@ -2,10 +2,11 @@ package stdemuxerhook

import (
"fmt"
"github.com/Sirupsen/logrus"
"os"
"testing"
"time"

"github.com/sirupsen/logrus"
)

// smallFields is a small size data set for benchmarking