Skip to content

Commit

Permalink
update to version 0.0.12: add support for per-output log level colori…
Browse files Browse the repository at this point in the history
…zation
  • Loading branch information
kataras committed Apr 27, 2020
1 parent fccaa50 commit 5f92374
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 97 deletions.
6 changes: 4 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Sa 25 April 2020 | v0.0.11
## Tu 28 April 2020 | v0.0.12

- Update the [pio dependency](https://github.com/kataras/pio) to v0.0.3.
This release provides support for colorized log level per registered output. Log's level will be colorful for registered `io.Writer`(via `AddOutput`) that supports colors, even when the rest of the writers (e.g. files) don't.

**Breaking changes on the `Levels` map**. See the corresponding updated example for migration.

## Th 12 December 2019 | v0.0.10

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module your_project_name
go 1.14

require (
github.com/kataras/golog v0.0.11
github.com/kataras/golog v0.0.12
)
```

Expand Down
7 changes: 3 additions & 4 deletions _examples/customize-levels/new-level/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ func main() {
var SuccessLevel golog.Level = 6
// Register our level, just three fields.
golog.Levels[SuccessLevel] = &golog.LevelMetadata{
Name: "success",
RawText: "[SUCC]",
// ColorfulText (Green Color[SUCC])
ColorfulText: "\x1b[32m[SUCC]\x1b[0m",
Name: "success",
Title: "[SUCC]",
ColorCode: 32, // Green
}

// create a new golog logger
Expand Down
15 changes: 6 additions & 9 deletions _examples/customize-levels/text-and-colors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ import (
func main() {

// First argument is the raw text for outputs
// that are not support colors,
// second argument is the full colorful text (yes it can be different if you wish to).
//
// If the second argument is empty then golog will update the colorful text to the
// default color (i.e red on ErrorText) based on the first argument.
// second argument is the color code
// and the last, variadic argument can be any `kataras/pio.RichOption`, e.g. pio.Background, pio.Underline.

// Default is "[ERRO]"
golog.ErrorText("|ERROR|", "")
golog.ErrorText("|ERROR|", 31)
// Default is "[WARN]"
golog.WarnText("|WARN|", "")
golog.WarnText("|WARN|", 32)
// Default is "[INFO]"
golog.InfoText("|INFO|", "")
golog.InfoText("|INFO|", 34)
// Default is "[DBUG]"
golog.DebugText("|DEBUG|", "")
golog.DebugText("|DEBUG|", 33)

// Business as usual...
golog.SetLevel("debug")
Expand Down
26 changes: 11 additions & 15 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.0.11
0.0.12
Installation
Expand Down Expand Up @@ -180,20 +180,17 @@ Example Code:
func main() {
// First argument is the raw text for outputs
// that are not support colors,
// second argument is the full colorful text (yes it can be different if you wish to).
//
// If the second argument is empty then golog will update the colorful text to the
// default color (i.e red on ErrorText) based on the first argument.
// second argument is the color code
// and the last, variadic argument can be any `kataras/pio.RichOption`, e.g. pio.Background, pio.Underline.
// Default is "[ERRO]"
golog.ErrorText("|ERROR|", "")
golog.ErrorText("|ERROR|", 31)
// Default is "[WARN]"
golog.WarnText("|WARN|", "")
golog.WarnText("|WARN|", 32)
// Default is "[INFO]"
golog.InfoText("|INFO|", "")
golog.InfoText("|INFO|", 34)
// Default is "[DBUG]"
golog.DebugText("|DEBUG|", "")
golog.DebugText("|DEBUG|", 33)
// Business as usual...
golog.SetLevel("debug")
Expand Down Expand Up @@ -234,10 +231,9 @@ Example Code:
var SuccessLevel golog.Level = 6
// Register our level, just three fields.
golog.Levels[SuccessLevel] = &golog.LevelMetadata{
Name: "success",
RawText: "[SUCC]",
// ColorfulText (Green Color[SUCC])
ColorfulText: "\x1b[32m[SUCC]\x1b[0m",
Name: "success",
Title: "[SUCC]",
ColorCode: 32, // Green
}
// create a new golog logger
Expand Down Expand Up @@ -394,4 +390,4 @@ Examples:
package golog // import "github.com/kataras/golog"

// Version is the version string representation of the "golog" package.
const Version = "0.0.11"
const Version = "0.0.12"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/kataras/golog

go 1.14

require github.com/kataras/pio v0.0.3
require github.com/kataras/pio v0.0.5
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/kataras/pio v0.0.3 h1:aDJLD7sWzHaLrLUATdDxJbKd9XDb0aeG4Igapf7zIXI=
github.com/kataras/pio v0.0.3/go.mod h1:NFfMp2kVP1rmV4N6gH6qgWpuoDKlrOeYi3VrAIWCGsE=
github.com/kataras/pio v0.0.5 h1:mPgwN2COEQlczTioVElB7RoB0m7eAkbxSc9/fDGaNNo=
github.com/kataras/pio v0.0.5/go.mod h1:NFfMp2kVP1rmV4N6gH6qgWpuoDKlrOeYi3VrAIWCGsE=
82 changes: 31 additions & 51 deletions level.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package golog

import (
"strings"

"github.com/kataras/pio"
)
import "github.com/kataras/pio"

// Level is a number which defines the log level.
type Level uint32
Expand Down Expand Up @@ -35,35 +31,34 @@ var Levels = map[Level]*LevelMetadata{
DisableLevel: {
Name: "disable",
AlternativeNames: []string{"disabled"},
RawText: "",
ColorfulText: "",
Title: "",
},
FatalLevel: {
Name: "fatal",
RawText: "[FTAL]",
// white foreground but red background, it's nice
ColorfulText: pio.Rich("[FTAL]", pio.Red, pio.Background),
Name: "fatal",
Title: "[FTAL]",
ColorCode: pio.Red,
Style: []pio.RichOption{pio.Background},
},
ErrorLevel: {
Name: "error",
RawText: "[ERRO]",
ColorfulText: pio.Rich("[ERRO]", pio.Red),
Name: "error",
Title: "[ERRO]",
ColorCode: pio.Red,
},
WarnLevel: {
Name: "warn",
AlternativeNames: []string{"warning"},
RawText: "[WARN]",
ColorfulText: pio.Rich("[WARN]", pio.Magenta),
Title: "[WARN]",
ColorCode: pio.Magenta,
},
InfoLevel: {
Name: "info",
RawText: "[INFO]",
ColorfulText: pio.Rich("[INFO]", pio.Cyan),
Name: "info",
Title: "[INFO]",
ColorCode: pio.Cyan,
},
DebugLevel: {
Name: "debug",
RawText: "[DBUG]",
ColorfulText: pio.Rich("[DBUG]", pio.Yellow),
Name: "debug",
Title: "[DBUG]",
ColorCode: pio.Yellow,
},
}

Expand Down Expand Up @@ -98,47 +93,32 @@ type LevelMetadata struct {
// AlternativeNames = []string{"warning"}, it's an optional field,
// therefore we keep Name as a simple string and created this new field.
AlternativeNames []string
// Tha RawText will be the prefix of the log level
// when output doesn't supports colors.
//
// When RawText is changed its ColorfulText is also changed
// to a default color, but callers are able to change it too.
RawText string
// The ColorfulText will be the prefix of the log level
// when output supports colors, almost everything except
// os files and putty-based terminals(?).
//
// If ColorfulText is empty then built'n colors
// are being used to wrap the "RawText".
ColorfulText string
// Tha Title is the prefix of the log level.
// See `ColorCode` and `Style` too.
// Both `ColorCode` and `Style` should be respected across writers.
Title string
// ColorCode a color for the `Title`.
ColorCode int
// Style one or more rich options for the `Title`.
Style []pio.RichOption
}

// Text returns the text that should be
// prepended to the log message when a specific
// log level is being written.
func (m *LevelMetadata) Text(enableColor bool) string {
if enableColor {
return m.ColorfulText
return pio.Rich(m.Title, m.ColorCode, m.Style...)
}
return m.RawText
return m.Title
}

// SetText can modify the prefix that will be prepended
// to the output message log when `Error/Errorf` functions are being used.
//
// If "newRawText" is empty then it will just skip the Text set-ing.
// If "newColorfulText" is empty then it will update the text color version using
// the default values by using the new raw text.
func (m *LevelMetadata) SetText(newRawText string, newColorfulText string) {
if newRawText != "" {
oldRawText := m.RawText
m.RawText = newRawText
m.ColorfulText = strings.Replace(m.ColorfulText, oldRawText, newRawText, -1)
}
if newColorfulText != "" {
m.ColorfulText = newColorfulText
}

func (m *LevelMetadata) SetText(title string, colorCode int, style ...pio.RichOption) {
m.Title = title
m.ColorCode = colorCode
m.Style = style
}

var (
Expand Down
32 changes: 20 additions & 12 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func (l *Logger) releaseLog(log *Log) {
l.logs.Put(log)
}

var spaceBytes = []byte(" ")

// we could use marshal inside Log but we don't have access to printer,
// we could also use the .Handle with NopOutput too but
// this way is faster:
Expand All @@ -88,25 +90,31 @@ var logHijacker = func(ctx *pio.Ctx) {
return
}

line := GetTextForLevel(l.Level, ctx.Printer.IsTerminal)
if line != "" {
line += " "
w := ctx.Printer

if prefix := l.Logger.Prefix; len(prefix) > 0 {
fmt.Fprint(w, prefix)
}

if l.Level != DisableLevel {
if level, ok := Levels[l.Level]; ok {
pio.WriteRich(w, level.Title, level.ColorCode, level.Style...)
w.Write(spaceBytes)
}
}

if t := l.FormatTime(); t != "" {
line += t + " "
fmt.Fprint(w, t)
w.Write(spaceBytes)
}
line += l.Message

var b []byte
if pref := l.Logger.Prefix; len(pref) > 0 {
b = append(pref, []byte(line)...)
} else {
b = []byte(line)
fmt.Fprint(w, l.Message)

if l.Logger.NewLine {
fmt.Fprintln(w)
}

ctx.Store(b, nil)
ctx.Next()
ctx.Store(nil, pio.ErrHandled)
}

// NopOutput disables the output.
Expand Down

0 comments on commit 5f92374

Please sign in to comment.