Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

spiegel-im-spiegel/logf

Repository files navigation

logf - Simple leveled logging package by Golang

lint status GitHub license GitHub release

Import

import "github.com/spiegel-im-spiegel/emojis"

Usage of logf package

package main

import (
    "os"

    "github.com/spiegel-im-spiegel/logf"
)

func main() {
    logf.SetOutput(os.Stdout)
    for i := 0; i < 6; i++ {
        logf.SetMinLevel(logf.TRACE + logf.Level(i))
        logf.Tracef("Traceing: No. %d\n", i+1)
        logf.Debugf("Debugging: No. %d\n", i+1)
        logf.Printf("Information: No. %d\n", i+1)
        logf.Warnf("Warning: No. %d\n", i+1)
        logf.Errorf("Erroring: No. %d\n", i+1)
        logf.Fatalf("Fatal Erroring: No. %d\n", i+1)
    }
}

Output:

2009/11/10 23:00:00 [TRACE] Traceing: No. 1
2009/11/10 23:00:00 [DEBUG] Debugging: No. 1
2009/11/10 23:00:00 [INFO] Information: No. 1
2009/11/10 23:00:00 [WARN] Warning: No. 1
2009/11/10 23:00:00 [ERROR] Erroring: No. 1
2009/11/10 23:00:00 [FATAL] Fatal Erroring: No. 1
2009/11/10 23:00:00 [DEBUG] Debugging: No. 2
2009/11/10 23:00:00 [INFO] Information: No. 2
2009/11/10 23:00:00 [WARN] Warning: No. 2
2009/11/10 23:00:00 [ERROR] Erroring: No. 2
2009/11/10 23:00:00 [FATAL] Fatal Erroring: No. 2
2009/11/10 23:00:00 [INFO] Information: No. 3
2009/11/10 23:00:00 [WARN] Warning: No. 3
2009/11/10 23:00:00 [ERROR] Erroring: No. 3
2009/11/10 23:00:00 [FATAL] Fatal Erroring: No. 3
2009/11/10 23:00:00 [WARN] Warning: No. 4
2009/11/10 23:00:00 [ERROR] Erroring: No. 4
2009/11/10 23:00:00 [FATAL] Fatal Erroring: No. 4
2009/11/10 23:00:00 [ERROR] Erroring: No. 5
2009/11/10 23:00:00 [FATAL] Fatal Erroring: No. 5
2009/11/10 23:00:00 [FATAL] Fatal Erroring: No. 6

Create logger instance

package main

import (
    rotatelogs "github.com/lestrrat-go/file-rotatelogs"
    "github.com/spiegel-im-spiegel/logf"
)

func main() {
    rl, err := rotatelogs.New("./log.%Y%m%d%H%M.txt")
    if err != nil {
        logf.Fatal(err)
        return
    }
    logger := logf.New(
        logf.WithFlags(logf.LstdFlags|logf.Lshortfile),
        logf.WithPrefix("[Sample] "),
        logf.WithWriter(rl),
        logf.WithMinLevel(logf.INFO),
    )
    logger.Print("Information")
    //Output:
    //[Sample] 2009/11/10 23:00:00 sample.go:20: [INFO] Information
}

Reference