GithubHelp home page GithubHelp logo

fioepq9 / pzlog Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 89 KB

A pretty console writer for zerolog, powered by pterm.

License: MIT License

Go 100.00%
console console-log console-logger pretty pretty-print pretty-printer zerolog pterm

pzlog's Introduction

pzlog

This is a pretty console writer for zerolog, powered by pterm.

Quick Start

package main

import (
  "github.com/rs/zerolog"
  "github.com/fioepq9/pzlog"
)

func main() {
  log := zerolog.New(pzlog.NewPtermWriter()).
    With().
    TimeStamp().
    Caller().
    Stack().
    Logger()
  
  log.Info().Msg("ok")
}

Screenshot

debug

info

warn

error

Examples

All configurations of Writer can be overridden.

Default Config

  pt := PtermWriter{
    MaxWidth: pterm.GetTerminalWidth(),
    Out:      os.Stdout,
    LevelStyles: map[zerolog.Level]*pterm.Style{
      zerolog.TraceLevel: pterm.NewStyle(pterm.Bold, pterm.FgCyan),
      zerolog.DebugLevel: pterm.NewStyle(pterm.Bold, pterm.FgBlue),
      zerolog.InfoLevel:  pterm.NewStyle(pterm.Bold, pterm.FgGreen),
      zerolog.WarnLevel:  pterm.NewStyle(pterm.Bold, pterm.FgYellow),
      zerolog.ErrorLevel: pterm.NewStyle(pterm.Bold, pterm.FgRed),
      zerolog.FatalLevel: pterm.NewStyle(pterm.Bold, pterm.FgRed),
      zerolog.PanicLevel: pterm.NewStyle(pterm.Bold, pterm.FgRed),
      zerolog.NoLevel:    pterm.NewStyle(pterm.Bold, pterm.FgWhite),
    },
    KeyStyles: map[string]*pterm.Style{
      zerolog.MessageFieldName:    pterm.NewStyle(pterm.Bold, pterm.FgWhite),
      zerolog.TimestampFieldName:  pterm.NewStyle(pterm.Bold, pterm.FgGray),
      zerolog.CallerFieldName:     pterm.NewStyle(pterm.Bold, pterm.FgGray),
      zerolog.ErrorFieldName:      pterm.NewStyle(pterm.Bold, pterm.FgRed),
      zerolog.ErrorStackFieldName: pterm.NewStyle(pterm.Bold, pterm.FgRed),
    },
    ValFormatters: map[string]Formatter{},
    KeyOrderFunc: func(k1, k2 string) bool {
      score := func(s string) string {
        s = color.ClearCode(s)
        if s == zerolog.TimestampFieldName {
          return string([]byte{0, 0})
        }
        if s == zerolog.CallerFieldName {
          return string([]byte{0, 1})
        }
        if s == zerolog.ErrorFieldName {
          return string([]byte{math.MaxUint8, 0})
        }
        if s == zerolog.ErrorStackFieldName {
          return string([]byte{math.MaxUint8, 1})
        }
        return s
      }
      return score(k1) < score(k2)
    },
  }

  tmpl := `{{ .Timestamp }} {{ .Level }}  {{ .Message }}
{{- range $i, $field := .Fields }}
{{ space (totalLength 1 $.Timestamp $.Level) }}{{if (last $i $.Fields )}}└{{else}}├{{ end }} {{ .Key }}: {{ .Val }}
{{- end }}
`
  t, err := template.New("event").
    Funcs(template.FuncMap{
      "space": func(n int) string {
        return strings.Repeat(" ", n)
      },
      "totalLength": func(n int, s ...string) int {
        return len(color.ClearCode(strings.Join(s, ""))) - n
      },
      "last": func(x int, a interface{}) bool {
        return x == reflect.ValueOf(a).Len()-1
      },
    }).
    Parse(tmpl)
  if err != nil {
    panic(fmt.Errorf("cannot parse template: %s", err))
  }
  pt.Tmpl = t

  pt.DefaultKeyStyle = func(_ string, lvl zerolog.Level) *pterm.Style {
    return pt.LevelStyles[lvl]
  }

  pt.DefaultValFormatter = func(key string, lvl zerolog.Level) Formatter {
    return func(v interface{}) string {
      return pterm.Sprint(v)
    }
  }

Change Default Key Style

package main

import (
  "github.com/rs/zerolog"
  "github.com/fioepq9/pzlog"
)

func main() {
  log := zerolog.New(NewPtermWriter(func(pw *PtermWriter) {
    pw.DefaultKeyStyle = func(key string, l zerolog.Level) *pterm.Style {
      return pterm.NewStyle(pterm.Bold, pterm.FgGray)
    }
  })).With().
    Timestamp().
    Stack().
    Logger()
  
  log.Info().Msg("ok")
}

pzlog's People

Contributors

fioepq9 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.