logger

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2023 License: MIT Imports: 12 Imported by: 0

README

Logger

The logger package implements the core logging functionality.

By default, the logger is initialized:

  • with the time format 2006/01/02 15:04:05.000;
  • with logging format FormatText;
  • at the highest logging level LevelTrace;
  • printing the name of the calling function true.

You can set the required time format, level, log message format and additional output, as in the following example:

package main

import (
	"os"

	"github.com/gromey/proton/logger"
)

func main() {
	file, err := os.OpenFile("logs.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}

	defer logger.Closer(file)

	logger.Info("Hello World!")
	// 2006/01/02 15:04:05.000 INFO [1] /../../main.go:17 main() Hello World!

	logger.SetTimeFormat("2006-01-02 15:04:05")
	logger.SetFormat(logger.FormatJSON)
	logger.SetAdditionalOut(file)
	logger.SetLevel(logger.LevelInfo)
	logger.SetFuncNamePrinting(false)

	logger.Info("Hello World!")
	// {"time":"2006-01-02 15:04:05","level":"INFO","routine":1,"message":"Hello World!"}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Closer

func Closer(c io.Closer)

Closer calls the Close method, if the closure occurred with an error, it prints it to the log.

func Debug

func Debug(v ...any)

func Debugf

func Debugf(format string, v ...any)

func DumpHttpRequest

func DumpHttpRequest(r *http.Request, logLevel Level)

DumpHttpRequest dumps the HTTP request and prints out with logFunc.

func DumpHttpResponse

func DumpHttpResponse(r *http.Response, logLevel Level)

DumpHttpResponse dumps the HTTP response and prints out with logFunc.

func Error

func Error(v ...any)

func Errorf

func Errorf(format string, v ...any)

func Fatal

func Fatal(v ...any)

func Fatalf

func Fatalf(format string, v ...any)

func FunctionInfo

func FunctionInfo(skip int) (name, file string, line int, ok bool)

FunctionInfo returns the name of the function and file, the line number on the calling goroutine's stack. The argument skip is the number of stack frames to ascend.

func InLevel

func InLevel(level Level) bool

InLevel returns true if the given level is less than or equal to the current logger level.

func Info

func Info(v ...any)

func Infof

func Infof(format string, v ...any)

func SetAdditionalOut

func SetAdditionalOut(out io.Writer)

SetAdditionalOut sets an additional logger output.

func SetFormat

func SetFormat(f Format)

SetFormat sets the logger format.

func SetFuncNamePrinting

func SetFuncNamePrinting(on bool)

SetFuncNamePrinting sets whether the logger should print the caller function name.

func SetLevel

func SetLevel(level Level)

SetLevel sets the logger level.

func SetTimeFormat

func SetTimeFormat(format string)

SetTimeFormat sets the logger time format.

func Trace

func Trace(v ...any)

func Tracef

func Tracef(format string, v ...any)

func Warn

func Warn(v ...any)

func Warnf

func Warnf(format string, v ...any)

Types

type Format

type Format uint8
const (
	FormatUnknown Format = iota
	FormatText
	FormatJSON
)

func ParseFormat

func ParseFormat(f string) (Format, error)

ParseFormat takes a string format and returns the logger Format constant.

func (Format) IsValid

func (f Format) IsValid() bool

func (Format) String

func (f Format) String() string

type Level

type Level uint32
const (
	LevelUnknown Level = iota
	LevelFatal
	LevelError
	LevelWarn
	LevelInfo
	LevelDebug
	LevelTrace
)

func ParseLevel

func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the logger Level constant.

func (Level) IsValid

func (lvl Level) IsValid() bool

func (Level) Print

func (lvl Level) Print(v ...any)

Print returns a logger print function for current logger level.

func (Level) Printf

func (lvl Level) Printf(format string, v ...any)

Printf returns a logger printf function for current logger level.

func (Level) String

func (lvl Level) String() string

Jump to

Keyboard shortcuts

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