An open API service indexing awesome lists of open source software.

https://github.com/tauqeernasir/gcl

A dead-simple go colorful logging library
https://github.com/tauqeernasir/gcl

golang-logger golang-logging logging logging-lib logging-library

Last synced: 6 months ago
JSON representation

A dead-simple go colorful logging library

Awesome Lists containing this project

README

          

# Go colorful logging

A dead simple go logging utility with or without colors.

### Usage

```go

package main

import (
"os"

"github.com/tauqeernasir/gcl/gcl"
)

var Logger *gcl.Logger

func init() {
Logger = gcl.NewLogger(os.Stdout)
}

func main() {
Logger.Info("I am an information message.")
Logger.Error("I am an error message.")
Logger.Success("I am a success message.")
Logger.Warn("I am a warning message.")
Logger.Fatal("I am a fatal message.")
}

```

Above code will generate the following output

Screen Shot 2021-10-14 at 1 49 02 AM

> Every method supports formatted versions as well, such as `Logger.Infof(format string, args ...interface{})` and `Logger.Errorf(format string, args ...interface{})`.

### Configurations

You can turn on/off some features of the logger by calling following methods on `Logger`
- WithColor() or WithoutColor()
- WithTimestamp() or WithoutTimestamp
- WithFileInfo() or WithoutFileInfo()

All methods return reference to the `Logger`, so they can easily be chained. Like `Logger.WithColor().WithoutTimestamp().WithoutFileInfo()`

#### Logging to file

```go

package main

import (
"os"

"github.com/tauqeernasir/gcl/gcl"
)

var Logger *gcl.Logger

func init() {
// create a file
f, err := os.Create("log.txt")
if err != nil {
panic("couldn't create log.txt file")
}

// provide file to logger
Logger = gcl.NewLogger(f).WithoutColor()
}

func main() {
Logger.Info("I am an information message.")
Logger.Error("I am an error message.")
Logger.Success("I am a success message.")
Logger.Warn("I am a warning message.")
Logger.Fatal("I am a fatal message.")
}

```