Skip to content

Commit

Permalink
[log][doc] Update outdated example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
at15 committed Apr 28, 2019
1 parent 3c94dcd commit 58aae4e
Showing 1 changed file with 36 additions and 52 deletions.
88 changes: 36 additions & 52 deletions log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,57 @@

## Convention

- library/application MUST have a library/application registry.
- every package MUST have a package level logger.
- logger should only register themselves in registry if it's a long lived, i.e. server
- every package has a log registry and its default logger.
- logger should only register themselves in registry if it's a long lived one, i.e. server

## Usage

See [_examples](_examples)

In your project have a `logutil` package to serve as registry, and import this package in all the other packages
to create package logger so you don't need to manually register them

````go
import (
"github.com/dyweb/gommon/log"
)

const Project = "github.com/your/project"

var registry = log.NewLibraryRegistry(Project)

func Registry() *log.Registry {
return &registry
}

func NewPackageLoggerAndRegistry() (*log.Logger, *log.Registry) {
logger, child := log.NewPackageLoggerAndRegistryWithSkip(Project, 1)
registry.AddRegistry(child)
return logger, child
}
````
See examples in [handlers](handlers)

In package, create package level var called `log` this saves import and also avoid people importing standard log.
Use `dlog` as import alias because `log` is already used.

````go
package server
package main

import (
"os"
"time"

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

var log, logReg = logutil.NewPackageLoggerAndRegistry()
var logReg = dlog.NewRegistry()
var log = logReg.Logger()

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

func foo(file string) {
// structual way
log.DebugF("open", dlog.Str("file", file))
// default handler
// debug 20180204 open file=test.yml
// logfmtish handler
// lvl=debug t=20180204 msg=open file=test.yml
// json handler
// {"lvl": "debug", "t": "20180204", "msg": "open", "file": "test.yml"}

// traditional way
log.Debugf("open %s", file)
// debug 20180204 open test.yml

// for expensive operation, check before log
if log.IsDebugEnabled() {
log.Debug("counter". dlog.Int("counter", CountExpensive()))
if len(os.Args) > 1 {
if os.Args[1] == "nocolor" || os.Args[1] == "no" {
dlog.SetHandler(cli.NewNoColor(os.Stderr))
}
}
}

func bar() {
// create a new logger based on package level logger
logger := log.Copy().AddField(dlog.Str("bar", "barbarbar"))
logger.Info("have extra fields")
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")
}
````

0 comments on commit 58aae4e

Please sign in to comment.