Coder Social home page Coder Social logo

loki's Introduction

Loki

GitHub release Go Report Card codecov CircleCI

Inspired by a popular NodeJS logger library: debug.

Feature

  • zero configuration for most use cases.
  • Colorful console output
  • Enable logger by environment easily

Install

go get github.com/joway/loki@latest

API

Go Doc

Usage

Use root logger simply

It will not been affected by LOKI_ENV.

loki.SetLevel(loki.INFO)

loki.Info("x: %s", "hi")
loki.Debug("x: %s", "hi")
loki.Error("x: %s", "hi")

Create your logger

logger := loki.New("app:xxx")
logger.Info("x: %s", "hi")
logger.Debug("x: %s", "hi")
logger.Error("x: %s", "hi")

To enable the logger, just add env LOKI_ENV=app:xxx in your command, like: LOKI_ENV=app:xxx ./main.

You can also use LOKI_ENV=app:a,app:b,model:a to enable multiple logger.

LOKI_ENV=* can enable all loggers.

Use file handler

// "02 Jan 06 15:04 MST"
l := loki.New("app:xxx")
fp, err := os.OpenFile("test.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
defer fp.Close()
flushIntervalMs := 1000
l.SetHandler(loki.NewFileHandler(fp, flushIntervalMs))

l.Info("x: %s", "hi")

Change time format

// "02 Jan 06 15:04 MST"
loki.SetTimeFormatter(time.RFC822)

// disable time output
loki.SetTimeFormatter("")

Use your custom logger formatter

type ErrFormatter struct {
	loki.Formatter
}

func (f ErrFormatter) format(a ...interface{}) string {
	err := a[0].(error)
	return fmt.Sprintf("Error %v", err)
}

logger := loki.New("app:xxx")
f := ErrFormatter{}
logger.SetFormatter(f)
logger.Debug(errors.New("test error"))

loki's People

Contributors

joway avatar imgbot[bot] 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.