logf

package module
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2021 License: Apache-2.0 Imports: 5 Imported by: 3

README

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

Documentation

Index

Constants

View Source
const (
	Ldate         = 1 << iota              // the date in the local time zone: 2009/01/23
	Ltime                                  // the time in the local time zone: 01:23:23
	Lmicroseconds                          // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                              // full file name and line number: /a/b/c/d.go:23
	Lshortfile                             // final file name element and line number: d.go:23. overrides Llongfile
	LUTC                                   // if Ldate or Ltime is set, use UTC rather than the local time zone
	Llevel                                 // log level of message
	LstdFlags     = Ldate | Ltime | Llevel // initial values for the standard logger
)

These flags define which text to prefix to each log entry generated by the Logger (compatible with log package).

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug calls std.Debug() to print to the logger.

func Debugf

func Debugf(format string, v ...interface{})

Debugf calls std.Debugf() to print to the logger.

func Debugln

func Debugln(v ...interface{})

Debugln calls std.Debugln() to print to the logger.

func Error

func Error(v ...interface{})

Error calls std.Error() to print to the logger.

func Errorf

func Errorf(format string, v ...interface{})

Errorf calls std.Errorf() to print to the logger.

func Errorln

func Errorln(v ...interface{})

Errorln calls std.Errorln() to print to the logger.

func Fatal

func Fatal(v ...interface{})

Fatal calls std.Fatal() to print to the logger.

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf calls std.Fatalf() to print to the logger.

func Fatalln

func Fatalln(v ...interface{})

Fatalln calls std.Fatalln() to print to the logger.

func GetLogger added in v0.2.3

func GetLogger() *log.Logger

GetLogger returns log.Logger instance

func Output

func Output(lv Level, calldepth int, s string) error

Output writes the output for a logging event.

func Panic

func Panic(v ...interface{})

Panic is equivalent() to std.Output() followed by a call to panic().

func Panicf

func Panicf(format string, v ...interface{})

Panicf is equivalent() to std.Output() followed by a call to panic().

func Panicln

func Panicln(v ...interface{})

Panicln is equivalent() to std.Output() followed by a call to panic().

func Print

func Print(v ...interface{})

Print calls std.Print() to print to the logger.

func Printf

func Printf(format string, v ...interface{})

Printf calls std.Printf() to print to the logger.

func Println

func Println(v ...interface{})

Println calls std.Println() to print to the logger.

func SetFlags

func SetFlags(flag int)

SetFlags sets the output flags for the logger.

func SetMinLevel

func SetMinLevel(lv Level)

SetMinLevel sets the minimum level for the logger.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output destination for the logger.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets the output prefix for the logger.

func Trace

func Trace(v ...interface{})

Trace calls std.Trace() to print to the logger.

func Tracef

func Tracef(format string, v ...interface{})

Tracef calls std.Tracef() to print to the logger.

func Traceln

func Traceln(v ...interface{})

Traceln calls std.Traceln() to print to the logger.

func Warn

func Warn(v ...interface{})

Warn calls std.Warn() to print to the logger.

func Warnf

func Warnf(format string, v ...interface{})

Warnf calls std.Warnf() to print to the logger.

func Warnln

func Warnln(v ...interface{})

Warnln calls std.Warnln() to print to the logger.

Types

type Level

type Level int

Level is log level

const (
	TRACE Level = iota
	DEBUG
	INFO
	WARN
	ERROR
	FATAL
)

Values of Level

func MinLevel

func MinLevel() Level

MinLevel returns the minimum level for the logger.

func (Level) String

func (lv Level) String() string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is logger class

func New

func New(opts ...OptFunc) *Logger

New creates a new Logger.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug calls l.lprint() to print to the logger.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf calls l.lprintf() to print to the logger.

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln calls l.lprintln() to print to the logger.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error calls l.lprint() to print to the logger.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf calls l.lprintf() to print to the logger.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Errorln calls l.lprintln() to print to the logger.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal calls l.lprint() to print to the logger.

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Fatalf calls l.lprintf() to print to the logger.

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln calls l.lprintln() to print to the logger.

func (*Logger) GetLogger added in v0.2.2

func (l *Logger) GetLogger() *log.Logger

GetLogger returns log.Logger instance

func (*Logger) MinLevel added in v0.2.3

func (l *Logger) MinLevel() Level

MinLevel returns the minimum level for the logger.

func (*Logger) Output

func (l *Logger) Output(lv Level, calldepth int, s string) error

Output writes the output for a logging event.

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

Panic is equivalent() to l.Output() followed by a call to panic().

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...interface{})

Panicf is equivalent() to l.Output() followed by a call to panic().

func (*Logger) Panicln

func (l *Logger) Panicln(v ...interface{})

Panicln is equivalent() to l.Output() followed by a call to panic().

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print calls l.lprint() to print to the logger.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

Printf calls l.lprintf() to print to the logger.

func (*Logger) Println

func (l *Logger) Println(v ...interface{})

Println calls l.lprintln() to print to the logger.

func (*Logger) SetFlags

func (l *Logger) SetFlags(flag int)

SetFlags sets the output flags for the logger.

func (*Logger) SetMinLevel

func (l *Logger) SetMinLevel(lv Level)

SetMinLevel sets the minimum level for the logger.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets the output destination for the logger.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets the output prefix for the logger.

func (*Logger) Trace

func (l *Logger) Trace(v ...interface{})

Trace calls l.lprint() to print to the logger.

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...interface{})

Tracef calls l.lprintf() to print to the logger.

func (*Logger) Traceln

func (l *Logger) Traceln(v ...interface{})

Traceln calls l.lprintln() to print to the logger.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn calls l.lprint() to print to the logger.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warnf calls l.lprintf() to print to the logger.

func (*Logger) Warnln

func (l *Logger) Warnln(v ...interface{})

Warnln calls l.lprintln() to print to the logger.

type OptFunc

type OptFunc func(*Logger)

OptFunc is self-referential function for functional options pattern

func WithFlags added in v0.2.0

func WithFlags(flag int) OptFunc

WithFlags returns function for setting flags

func WithMinLevel added in v0.2.0

func WithMinLevel(lv Level) OptFunc

WithMinLevel returns function for setting minimum level

func WithPrefix added in v0.2.0

func WithPrefix(prefix string) OptFunc

WithPrefix returns function for setting prefix string

func WithWriter added in v0.2.0

func WithWriter(w io.Writer) OptFunc

WithWriter returns function for setting Writer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL