Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

Commit

Permalink
Merge pull request #9 from gleich/multi-writers
Browse files Browse the repository at this point in the history
Multiple Writers
  • Loading branch information
gleich authored Dec 13, 2021
2 parents 3b43ae1 + 95f6cbb commit ddbaab0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ Here are all the variables that can be changed:
| ----------------- | ------------------------------------------------------------------------------------ | ---------------------- | ---------------- |
| `NormalOut` | The output file for Debug, Success, Warning, and Info | `os.Stdout` | `*os.File` |
| `ErrOut` | The output file for Fatal and Error | `os.Stderr` | `*os.File` |
| `ExtraNormalOuts` | Extra normal output destinations (e.g. outputting to a file as well as stdout) | `[]io.Writer{}` | `[]io.Writer` |
| `ExtraErrOuts` | Extra error output destinations (e.g. outputting to a file as well as stderr) | `[]io.Writer{}` | `[]io.Writer` |
| `ExitCode` | Fatal exit code | `1` | `int` |
| `Padding` | If the log should have an extra new line at the bottom | `false` | `bool` |
| `ColoredOutput` | If the output should have color | `true` | `bool` |
Expand Down
23 changes: 14 additions & 9 deletions custom.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package lumber

import (
"io"
"os"
"time"
)

// Custom logger for lumber to use
type Logger struct {
NormalOut *os.File // The output file for Debug, Success, Warning, and Info. Default is os.Stdout
ErrOut *os.File // The output file for Fatal, FatalMsg, Error, and ErrorMsg. Default is os.Stderr
ExitCode int // Fatal exit code. Default is 1
ShowStack bool // If stack trades should be included. Default is true
Timezone *time.Location // Timezone for the time to be outputted in. Default is time.UTC
Padding bool // If the log should have an extra new line at the bottom. Default is true
Multiline bool // If the log should span multiple lines. Default is false
ColoredOutput bool // If the output should have color. Default is true
TrueColor bool // If the output should be true color or basic colors. Default is true if the terminal supports it
NormalOut *os.File // The output file for Debug, Success, Warning, and Info. Default is os.Stdout
ErrOut *os.File // The output file for Fatal, FatalMsg, Error, and ErrorMsg. Default is os.Stderr
ExtraNormalOuts []io.Writer // Extra normal output destinations (e.g. outputting to a file as well)
ExtraErrOuts []io.Writer // Extra error output destinations (e.g. outputting to a file as well)
ExitCode int // Fatal exit code. Default is 1
ShowStack bool // If stack trades should be included. Default is true
Timezone *time.Location // Timezone for the time to be outputted in. Default is time.UTC
Padding bool // If the log should have an extra new line at the bottom. Default is true
Multiline bool // If the log should span multiple lines. Default is false
ColoredOutput bool // If the output should have color. Default is true
TrueColor bool // If the output should be true color or basic colors. Default is true if the terminal supports it
}

// Default value for true color
Expand All @@ -25,6 +28,8 @@ func NewCustomLogger() Logger {
config := Logger{}
config.NormalOut = os.Stdout
config.ErrOut = os.Stderr
config.ExtraNormalOuts = []io.Writer{}
config.ExtraErrOuts = []io.Writer{}
config.ExitCode = 1
config.ShowStack = true
config.Timezone = time.UTC
Expand Down
7 changes: 5 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lumber

import (
"fmt"
"io"
"log"
"os"
"time"
Expand All @@ -24,7 +25,8 @@ var defaultLogger = NewCustomLogger()
// Log a normal status (Debug, Success, Warning, and Info)
func logNormal(config Logger, stat string, t time.Time, ctx ...interface{}) {
out := format(config, stat, t, separateWithSpaces(ctx...))
log.New(config.NormalOut, "", 0).Println(out)
writers := append([]io.Writer{config.NormalOut}, config.ExtraNormalOuts...)
log.New(io.MultiWriter(writers...), "", 0).Println(out)
}

// Log a normal status (Debug, Success, Warning, and Info)
Expand All @@ -48,7 +50,8 @@ func logError(config Logger, stat string, t time.Time, err error, ctx ...interfa
out = format(config, stat, t, separateWithSpaces(ctx...)+"\n\n"+err.Error())
}

log.New(config.ErrOut, "", 0).Println(out)
writers := append([]io.Writer{config.ErrOut}, config.ExtraErrOuts...)
log.New(io.MultiWriter(writers...), "", 0).Println(out)
}

// Output a success log
Expand Down

0 comments on commit ddbaab0

Please sign in to comment.