golog: github.com/vardius/golog Index | Examples | Files

package golog

import "github.com/vardius/golog"

Package golog provides simple logger Basic example:

package main

import (
	"context"

	"github.com/vardius/golog"
)

func main() {
	ctx := context.Background()
	logger := golog.New()

	logger.Debug(ctx context.Context, "Hello %s!", "you")
}

Index

Examples

Package Files

console.go doc.go file.go logger.go

Constants

const (
    CLR_0 = "\x1b[30;1m"
    CLR_R = "\x1b[31;1m"
    CLR_G = "\x1b[32;1m"
    CLR_Y = "\x1b[33;1m"
    CLR_B = "\x1b[34;1m"
    CLR_M = "\x1b[35;1m"
    CLR_C = "\x1b[36;1m"
    CLR_W = "\x1b[37;1m"
    CLR_N = "\x1b[0m"
)

Terminal colours.

const (
    // DebugPrefix text to prefix to each log entry generated by the Logger
    DebugPrefix = "DEBUG: "
    // InfoPrefix text to prefix to each log entry generated by the Logger
    InfoPrefix = "INFO: "
    // WarnPrefix text to prefix to each log entry generated by the Logger
    WarnPrefix = "WARN: "
    // ErrorPrefix text to prefix to each log entry generated by the Logger
    ErrorPrefix = "ERROR: "
    // FatalPrefix text to prefix to each log entry generated by the Logger
    FatalPrefix = "FATAL: "

    // DefaultFlags define default text to prefix to each log entry generated by the Logger
    DefaultFlags = log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC
    // DefaultVerbosity define default verbosity level
    DefaultVerbosity = Critical | Error | Warning | Info | Debug
)

Variables

var New loggerFactory

New returns a Logger.

type Logger Uses

type Logger interface {
    Debug(ctx context.Context, format string, args ...interface{})
    Info(ctx context.Context, format string, args ...interface{})
    Warning(ctx context.Context, format string, args ...interface{})
    Error(ctx context.Context, format string, args ...interface{})
    Critical(ctx context.Context, format string, args ...interface{})

    // SetFlags override DefaultFlags value on a Logger instance https://golang.org/pkg/log/#pkg-constants
    SetFlags(flag int)
    // SetVerbosity override DefaultVerbosity value on a Logger instance
    SetVerbosity(verbosity Verbose)
}

Logger interface

func NewConsoleLogger Uses

func NewConsoleLogger() Logger

NewConsoleLogger returns a Logger that writes to the console.

Code:

ctx := context.Background()
logger := golog.NewConsoleLogger()
logger.SetVerbosity(golog.Warning | golog.Error)

logger.SetFlags(0)

// logger.Info won't print to the console because it is below loglevel "warn"
logger.Info(ctx, "%s", "Info")
// logger.Warning and log.Error will both get printed
logger.Warning(ctx, "%s %d", "Warn", 1)
logger.Error(ctx, "%s %d", "Error", 666)

Output:

WARN: Warn 1
ERROR: Error 666

Code:

ctx := context.Background()
logger := golog.NewConsoleLogger()
logger.SetVerbosity(golog.Warning | golog.Error)

logger.SetFlags(0)

// logger.Info won't print to the console because it is below loglevel "warn"
logger.Info(ctx, "%s", "Info")
// logger.Warning and log.Error will both get printed
logger.Warning(ctx, "%s %d", "Warn", 1)
logger.Error(ctx, "%s %d", "Error", 666)

Output:

WARN: Warn 1
ERROR: Error 666

func NewFileLogger Uses

func NewFileLogger(filePath string) Logger

NewFileLogger returns a Logger that writes to the file.

type Verbose Uses

type Verbose uint8

Verbose levels

const (
    Critical Verbose = 1 << iota // Critical verbose level
    Error                        // Error verbose level
    Warning                      // Warning verbose level
    Info                         // Info verbose level
    Debug                        // Debug verbose level
    Disabled                     // Disabled verbose level disables logger messages. will override all other flags
)

Verbose level flags

Package golog imports 3 packages (graph) and is imported by 18 packages. Updated 2019-09-18. Refresh now. Tools for package owners.