cli

package
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2020 License: MIT Imports: 6 Imported by: 2

README

Cli log handler

For short running cli and human read

Usage

See example/main.go

TODO: gommon should allow sync file into markdown

package main

import (
	"os"
	"time"

	dlog "github.com/dyweb/gommon/log"
	"github.com/dyweb/gommon/log/handlers/cli"
)

var logReg = dlog.NewRegistry()
var log = logReg.Logger()

func main() {
	dlog.SetHandler(cli.New(os.Stderr, true))

	if len(os.Args) > 1 {
		if os.Args[1] == "nocolor" || os.Args[1] == "no" {
			dlog.SetHandler(cli.NewNoColor(os.Stderr))
		}
	}

	log.Info("hi")
	log.Infof("open file %s", "foo.yml")
	log.InfoF("open",
		dlog.Str("file", "foo.yml"),
		dlog.Int("mode", 0666),
	)
	log.Warn("I am yellow")
	func() {
		defer func() {
			if r := recover(); r != nil {
				log.Info("recovered", r)
			}
		}()
		log.Panic("I just want to panic")
	}()
	dlog.SetLevel(dlog.DebugLevel)
	log.Debug("I will sleep for a while")
	time.Sleep(500 * time.Millisecond)
	log.Fatal("I am red")
}

Result in

INFO 0000 hi
INFO 0000 open file foo.yml
INFO 0000 open file=foo.yml mode=438
WARN 0000 I am yellow
PANI 0000 Panic reason
INFO 0000 recoveredPanic reason
DEBU 0000 I will sleep for a while
FATA 0001 I am red

It has color (only tested on Linux (Ubuntu)) and can be disabled when created using no color

gommon_log_cli_handler

Documentation

Overview

Package cli generates human readable text with color and display time in delta. Color and delta can be disabled and output will be same as default handler It does NOT escape quote so it is not machine readable

func main() {
	var log, logReg = dlog.NewApplicationLoggerAndRegistry("example")
	dlog.SetHandler(logReg, cli.New(os.Stderr, true)) // with color, delta time
}

Index

Constants

View Source
const (
	DefaultTimeStampFormat = time.RFC3339
)

Variables

This section is empty.

Functions

func New

func New(w io.Writer, delta bool) log.Handler

New returns a handler with color on level and field key

func NewNoColor added in v0.0.9

func NewNoColor(w io.Writer) log.Handler

NewNoColor returns a handler without color and use full timestamp

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL