Skip to content

Commit

Permalink
add SetLevelOutput - customize writer per level
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Sep 6, 2020
1 parent 8b3a576 commit c0e998b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 22 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Su 06 September | v0.1.3

Add `SetLevelOutput(levelName string, w io.Writer)` to customize the writer per level.

## Sa 15 August | v0.1.2

- `Logger.Child` accepts an `interface{}` instead of `string`. This way you can register children for pointers without forcing to naming them. If the key is string or completes the `fmt.Stringer` interface, then it's used as prefix (like always did).
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ Or edit your project's go.mod file and execute $ go build.
```bash
module your_project_name

go 1.14
go 1.15

require (
github.com/kataras/golog v0.1.2
github.com/kataras/golog v0.1.3
)
```

Expand Down Expand Up @@ -250,6 +250,7 @@ func main() {
## Examples

* [basic](_examples/basic/main.go)
* [output per level](_examples/level-output/main.go)
* [child](_examples/child/main.go)
* [add new level](_examples/customize-levels/new-level/main.go)
* [change text and color](_examples/customize-levels/text-and-colors/main.go)
Expand Down
49 changes: 49 additions & 0 deletions _examples/level-output/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Package main shows how you can register a log output per level.
package main

import (
"os"

"github.com/kataras/golog"
)

func simple() {
golog.SetLevelOutput("error", os.Stderr)

golog.Error("an error") // prints to os.Stderr.
golog.Info("an info") // prints to the default output: os.Stdout.
}

func main() {
// use debug.log and info.log files for the example.
debugFile, err := os.OpenFile("debug.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer debugFile.Close()

infoFile, err := os.OpenFile("info.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer infoFile.Close()
//

// initialize a new logger
logger := golog.New()
logger.SetLevel("debug")
//

// set the outputs per log level.
logger.SetLevelOutput("debug", debugFile)
logger.SetLevelOutput("info", infoFile)
//

// write some logs.
logger.Debug("A debug message")
// debug.log contains:
// [DBUG] 2020/09/06 12:01 A debug message
logger.Info("An info message")
// info.log contains:
// [INFO] 2020/09/06 12:01 An info message
}
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Source code and other details for the project are available at GitHub:
Current Version
0.1.2
0.1.3
Installation
Expand Down Expand Up @@ -390,4 +390,4 @@ Examples:
package golog // import "github.com/kataras/golog"

// Version is the version string representation of the "golog" package.
const Version = "0.1.2"
const Version = "0.1.3"
6 changes: 6 additions & 0 deletions golog.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func SetStacktraceLimit(limit int) *Logger {
return Default.SetStacktraceLimit(limit)
}

// SetLevelOutput sets a destination log output for the specific "levelName".
// For multiple writers use the `io.Multiwriter` wrapper.
func SetLevelOutput(levelName string, w io.Writer) *Logger {
return Default.SetLevelOutput(levelName, w)
}

// SetLevel accepts a string representation of
// a `Level` and returns a `Level` value based on that "levelName".
//
Expand Down
51 changes: 33 additions & 18 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ type Logger struct {
// Note that this will not override the time and level prefix,
// if you want to customize the log message please read the examples
// or navigate to: https://github.com/kataras/golog/issues/3#issuecomment-355895870.
NewLine bool
mu sync.Mutex // for logger field changes and printing through pio hijacker.
Printer *pio.Printer
NewLine bool
mu sync.Mutex // for logger field changes and printing through pio hijacker.
Printer *pio.Printer
// The per log level raw writers, optionally.
LevelOutput map[Level]io.Writer

handlers []Handler
once sync.Once
logs sync.Pool
Expand All @@ -53,11 +56,12 @@ type Logger struct {
// and level to `InfoLevel`.
func New() *Logger {
return &Logger{
Level: InfoLevel,
TimeFormat: "2006/01/02 15:04",
NewLine: true,
Printer: pio.NewPrinter("", os.Stdout).EnableDirectOutput().Hijack(logHijacker).SetSync(true),
children: newLoggerMap(),
Level: InfoLevel,
TimeFormat: "2006/01/02 15:04",
NewLine: true,
Printer: pio.NewPrinter("", os.Stdout).EnableDirectOutput().Hijack(logHijacker).SetSync(true),
LevelOutput: make(map[Level]io.Writer),
children: newLoggerMap(),
}
}

Expand Down Expand Up @@ -109,7 +113,10 @@ var logHijacker = func(ctx *pio.Ctx) {
logger.mu.Lock()
defer logger.mu.Unlock()

w := ctx.Printer
w, ok := logger.LevelOutput[l.Level]
if !ok {
w = ctx.Printer
}

if l.Level != DisableLevel {
if level, ok := Levels[l.Level]; ok {
Expand Down Expand Up @@ -207,6 +214,13 @@ func (l *Logger) DisableNewLine() *Logger {
return l
}

// SetLevelOutput sets a destination log output for the specific "levelName".
// For multiple writers use the `io.Multiwriter` wrapper.
func (l *Logger) SetLevelOutput(levelName string, w io.Writer) *Logger {
l.LevelOutput[ParseLevel(levelName)] = w
return l
}

// SetLevel accepts a string representation of
// a `Level` and returns a `Level` value based on that "levelName".
//
Expand Down Expand Up @@ -484,15 +498,16 @@ func (l *Logger) Scan(r io.Reader) (cancel func()) {
// This copy is returned as pointer as well.
func (l *Logger) Clone() *Logger {
return &Logger{
Prefix: l.Prefix,
Level: l.Level,
TimeFormat: l.TimeFormat,
NewLine: l.NewLine,
Printer: l.Printer,
handlers: l.handlers,
children: newLoggerMap(),
mu: sync.Mutex{},
once: sync.Once{},
Prefix: l.Prefix,
Level: l.Level,
TimeFormat: l.TimeFormat,
NewLine: l.NewLine,
Printer: l.Printer,
LevelOutput: l.LevelOutput,
handlers: l.handlers,
children: newLoggerMap(),
mu: sync.Mutex{},
once: sync.Once{},
}
}

Expand Down

0 comments on commit c0e998b

Please sign in to comment.