• Type:

CH Show HN: Simplog – Simple logger with no dependencies written in 170 Lines of go

A simple logger. No dependencies, no special features, just logging.

About

Simplog was created due to the unsuccessful search for a small and simple logging system. I needed a system that’s exactly in the middle between ‘does-what-it-should’ and ‘no-overkill’. Besides that I just wanted to write a logging library myself for fun.

If you like it and/or if it is useful for you, it has already more than fulfilled its purpose. Contributions are still welcome but just remember that this logger is intentionally kept simple.

Install

go get -u github.com/nikoksr/simplog

Example Code

// Open log file
logPath := "simple.log"

f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
	log.Fatalf("could not open log file %s. %v", logPath, err)
}

// Create logger
var log = simplog.New("Core", false, f)
if err != nil {
    log.Fatalf("could not create logger %v", err)
}

// Set log level to warning
log.SetLevel(simplog.Warning)

// fmt.Print style
log.Info("You're awesome!n")

// fmt.Println style
log.Warningln("Coffee is almost empty.")

// fmt.Printf style
log.Debugf("%d should be equal to %d.n", aNumber, anotherNumber)

Example Output

CORE 2020/03/18 17:13:24 main.go:24: INFO You're awesome!
CORE 2020/03/18 17:13:24 main.go:26: WARN Coffee is almost empty.
CORE 2020/03/18 17:13:25 main.go:28: DEBUG 17 should be equal to 9.

Credits

Read More

Previous Post

CH Show HN: Learn Rust building a toy Jira clone

Next Post

CH Show HN: Harness the power of hyperscan on the CLI with this fork of ripgrep

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top